service.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package registrytest
  14. import (
  15. "context"
  16. "sync"
  17. metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/watch"
  20. genericapirequest "k8s.io/apiserver/pkg/endpoints/request"
  21. "k8s.io/apiserver/pkg/registry/rest"
  22. api "k8s.io/kubernetes/pkg/apis/core"
  23. )
  24. func NewServiceRegistry() *ServiceRegistry {
  25. return &ServiceRegistry{}
  26. }
  27. type ServiceRegistry struct {
  28. mu sync.Mutex
  29. List api.ServiceList
  30. Service *api.Service
  31. Updates []api.Service
  32. Err error
  33. DeletedID string
  34. GottenID string
  35. UpdatedID string
  36. }
  37. func (r *ServiceRegistry) SetError(err error) {
  38. r.mu.Lock()
  39. defer r.mu.Unlock()
  40. r.Err = err
  41. }
  42. func (r *ServiceRegistry) ListServices(ctx context.Context, options *metainternalversion.ListOptions) (*api.ServiceList, error) {
  43. r.mu.Lock()
  44. defer r.mu.Unlock()
  45. ns, _ := genericapirequest.NamespaceFrom(ctx)
  46. // Copy metadata from internal list into result
  47. res := new(api.ServiceList)
  48. res.TypeMeta = r.List.TypeMeta
  49. res.ListMeta = r.List.ListMeta
  50. if ns != metav1.NamespaceAll {
  51. for _, service := range r.List.Items {
  52. if ns == service.Namespace {
  53. res.Items = append(res.Items, service)
  54. }
  55. }
  56. } else {
  57. res.Items = append([]api.Service{}, r.List.Items...)
  58. }
  59. return res, r.Err
  60. }
  61. func (r *ServiceRegistry) CreateService(ctx context.Context, svc *api.Service, createValidation rest.ValidateObjectFunc) (*api.Service, error) {
  62. r.mu.Lock()
  63. defer r.mu.Unlock()
  64. r.Service = svc.DeepCopy()
  65. r.List.Items = append(r.List.Items, *svc)
  66. return svc, r.Err
  67. }
  68. func (r *ServiceRegistry) GetService(ctx context.Context, id string, options *metav1.GetOptions) (*api.Service, error) {
  69. r.mu.Lock()
  70. defer r.mu.Unlock()
  71. r.GottenID = id
  72. return r.Service, r.Err
  73. }
  74. func (r *ServiceRegistry) DeleteService(ctx context.Context, id string) error {
  75. r.mu.Lock()
  76. defer r.mu.Unlock()
  77. r.DeletedID = id
  78. r.Service = nil
  79. return r.Err
  80. }
  81. func (r *ServiceRegistry) UpdateService(ctx context.Context, svc *api.Service, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc) (*api.Service, error) {
  82. r.mu.Lock()
  83. defer r.mu.Unlock()
  84. r.UpdatedID = svc.Name
  85. *r.Service = *svc
  86. r.Updates = append(r.Updates, *svc)
  87. return svc, r.Err
  88. }
  89. func (r *ServiceRegistry) WatchServices(ctx context.Context, options *metainternalversion.ListOptions) (watch.Interface, error) {
  90. r.mu.Lock()
  91. defer r.mu.Unlock()
  92. return nil, r.Err
  93. }
  94. func (r *ServiceRegistry) ExportService(ctx context.Context, name string, options metav1.ExportOptions) (*api.Service, error) {
  95. r.mu.Lock()
  96. defer r.mu.Unlock()
  97. return r.Service, r.Err
  98. }