service.go 2.8 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. "sync"
  16. "k8s.io/kubernetes/pkg/api"
  17. "k8s.io/kubernetes/pkg/api/unversioned"
  18. "k8s.io/kubernetes/pkg/watch"
  19. )
  20. func NewServiceRegistry() *ServiceRegistry {
  21. return &ServiceRegistry{}
  22. }
  23. type ServiceRegistry struct {
  24. mu sync.Mutex
  25. List api.ServiceList
  26. Service *api.Service
  27. Updates []api.Service
  28. Err error
  29. DeletedID string
  30. GottenID string
  31. UpdatedID string
  32. }
  33. func (r *ServiceRegistry) SetError(err error) {
  34. r.mu.Lock()
  35. defer r.mu.Unlock()
  36. r.Err = err
  37. }
  38. func (r *ServiceRegistry) ListServices(ctx api.Context, options *api.ListOptions) (*api.ServiceList, error) {
  39. r.mu.Lock()
  40. defer r.mu.Unlock()
  41. ns, _ := api.NamespaceFrom(ctx)
  42. // Copy metadata from internal list into result
  43. res := new(api.ServiceList)
  44. res.TypeMeta = r.List.TypeMeta
  45. res.ListMeta = r.List.ListMeta
  46. if ns != api.NamespaceAll {
  47. for _, service := range r.List.Items {
  48. if ns == service.Namespace {
  49. res.Items = append(res.Items, service)
  50. }
  51. }
  52. } else {
  53. res.Items = append([]api.Service{}, r.List.Items...)
  54. }
  55. return res, r.Err
  56. }
  57. func (r *ServiceRegistry) CreateService(ctx api.Context, svc *api.Service) (*api.Service, error) {
  58. r.mu.Lock()
  59. defer r.mu.Unlock()
  60. r.Service = new(api.Service)
  61. clone, err := api.Scheme.DeepCopy(svc)
  62. if err != nil {
  63. return nil, err
  64. }
  65. r.Service = clone.(*api.Service)
  66. r.List.Items = append(r.List.Items, *svc)
  67. return svc, r.Err
  68. }
  69. func (r *ServiceRegistry) GetService(ctx api.Context, id string) (*api.Service, error) {
  70. r.mu.Lock()
  71. defer r.mu.Unlock()
  72. r.GottenID = id
  73. return r.Service, r.Err
  74. }
  75. func (r *ServiceRegistry) DeleteService(ctx api.Context, id string) error {
  76. r.mu.Lock()
  77. defer r.mu.Unlock()
  78. r.DeletedID = id
  79. r.Service = nil
  80. return r.Err
  81. }
  82. func (r *ServiceRegistry) UpdateService(ctx api.Context, svc *api.Service) (*api.Service, error) {
  83. r.mu.Lock()
  84. defer r.mu.Unlock()
  85. r.UpdatedID = svc.Name
  86. *r.Service = *svc
  87. r.Updates = append(r.Updates, *svc)
  88. return svc, r.Err
  89. }
  90. func (r *ServiceRegistry) WatchServices(ctx api.Context, options *api.ListOptions) (watch.Interface, error) {
  91. r.mu.Lock()
  92. defer r.mu.Unlock()
  93. return nil, r.Err
  94. }
  95. func (r *ServiceRegistry) ExportService(ctx api.Context, name string, options unversioned.ExportOptions) (*api.Service, error) {
  96. r.mu.Lock()
  97. defer r.mu.Lock()
  98. return r.Service, r.Err
  99. }