endpoint.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. "fmt"
  16. "sync"
  17. "k8s.io/kubernetes/pkg/api"
  18. "k8s.io/kubernetes/pkg/api/errors"
  19. "k8s.io/kubernetes/pkg/watch"
  20. )
  21. // Registry is an interface for things that know how to store endpoints.
  22. type EndpointRegistry struct {
  23. Endpoints *api.EndpointsList
  24. Updates []api.Endpoints
  25. Err error
  26. lock sync.Mutex
  27. }
  28. func (e *EndpointRegistry) ListEndpoints(ctx api.Context, options *api.ListOptions) (*api.EndpointsList, error) {
  29. // TODO: support namespaces in this mock
  30. e.lock.Lock()
  31. defer e.lock.Unlock()
  32. return e.Endpoints, e.Err
  33. }
  34. func (e *EndpointRegistry) GetEndpoints(ctx api.Context, name string) (*api.Endpoints, error) {
  35. // TODO: support namespaces in this mock
  36. e.lock.Lock()
  37. defer e.lock.Unlock()
  38. if e.Err != nil {
  39. return nil, e.Err
  40. }
  41. if e.Endpoints != nil {
  42. for _, endpoint := range e.Endpoints.Items {
  43. if endpoint.Name == name {
  44. return &endpoint, nil
  45. }
  46. }
  47. }
  48. return nil, errors.NewNotFound(api.Resource("endpoints"), name)
  49. }
  50. func (e *EndpointRegistry) WatchEndpoints(ctx api.Context, options *api.ListOptions) (watch.Interface, error) {
  51. return nil, fmt.Errorf("unimplemented!")
  52. }
  53. func (e *EndpointRegistry) UpdateEndpoints(ctx api.Context, endpoints *api.Endpoints) error {
  54. // TODO: support namespaces in this mock
  55. e.lock.Lock()
  56. defer e.lock.Unlock()
  57. e.Updates = append(e.Updates, *endpoints)
  58. if e.Err != nil {
  59. return e.Err
  60. }
  61. if e.Endpoints == nil {
  62. e.Endpoints = &api.EndpointsList{
  63. Items: []api.Endpoints{
  64. *endpoints,
  65. },
  66. }
  67. return nil
  68. }
  69. for ix := range e.Endpoints.Items {
  70. if e.Endpoints.Items[ix].Name == endpoints.Name {
  71. e.Endpoints.Items[ix] = *endpoints
  72. }
  73. }
  74. e.Endpoints.Items = append(e.Endpoints.Items, *endpoints)
  75. return nil
  76. }
  77. func (e *EndpointRegistry) DeleteEndpoints(ctx api.Context, name string) error {
  78. // TODO: support namespaces in this mock
  79. e.lock.Lock()
  80. defer e.lock.Unlock()
  81. if e.Err != nil {
  82. return e.Err
  83. }
  84. if e.Endpoints != nil {
  85. var newList []api.Endpoints
  86. for _, endpoint := range e.Endpoints.Items {
  87. if endpoint.Name != name {
  88. newList = append(newList, endpoint)
  89. }
  90. }
  91. e.Endpoints.Items = newList
  92. }
  93. return nil
  94. }