endpoint.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. "fmt"
  17. "sync"
  18. "k8s.io/apimachinery/pkg/api/errors"
  19. metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. runtime "k8s.io/apimachinery/pkg/runtime"
  22. "k8s.io/apimachinery/pkg/watch"
  23. "k8s.io/apiserver/pkg/registry/rest"
  24. api "k8s.io/kubernetes/pkg/apis/core"
  25. )
  26. // Registry is an interface for things that know how to store endpoints.
  27. type EndpointRegistry struct {
  28. Endpoints *api.EndpointsList
  29. Updates []api.Endpoints
  30. Err error
  31. lock sync.Mutex
  32. }
  33. func (e *EndpointRegistry) List(ctx context.Context, options *metainternalversion.ListOptions) (runtime.Object, error) {
  34. // TODO: support namespaces in this mock
  35. e.lock.Lock()
  36. defer e.lock.Unlock()
  37. return e.Endpoints, e.Err
  38. }
  39. func (e *EndpointRegistry) New() runtime.Object {
  40. return &api.Endpoints{}
  41. }
  42. func (e *EndpointRegistry) NewList() runtime.Object {
  43. return &api.EndpointsList{}
  44. }
  45. func (e *EndpointRegistry) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
  46. // TODO: support namespaces in this mock
  47. e.lock.Lock()
  48. defer e.lock.Unlock()
  49. if e.Err != nil {
  50. return nil, e.Err
  51. }
  52. if e.Endpoints != nil {
  53. for _, endpoint := range e.Endpoints.Items {
  54. if endpoint.Name == name {
  55. return &endpoint, nil
  56. }
  57. }
  58. }
  59. return nil, errors.NewNotFound(api.Resource("endpoints"), name)
  60. }
  61. func (e *EndpointRegistry) Watch(ctx context.Context, options *metainternalversion.ListOptions) (watch.Interface, error) {
  62. return nil, fmt.Errorf("unimplemented!")
  63. }
  64. func (e *EndpointRegistry) Create(ctx context.Context, endpoints runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
  65. return nil, fmt.Errorf("unimplemented!")
  66. }
  67. func (e *EndpointRegistry) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreateOnUpdate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
  68. obj, err := objInfo.UpdatedObject(ctx, nil)
  69. if err != nil {
  70. return nil, false, err
  71. }
  72. endpoints := obj.(*api.Endpoints)
  73. // TODO: support namespaces in this mock
  74. e.lock.Lock()
  75. defer e.lock.Unlock()
  76. e.Updates = append(e.Updates, *endpoints)
  77. if e.Err != nil {
  78. return nil, false, e.Err
  79. }
  80. if e.Endpoints == nil {
  81. e.Endpoints = &api.EndpointsList{
  82. Items: []api.Endpoints{
  83. *endpoints,
  84. },
  85. }
  86. return endpoints, false, nil
  87. }
  88. for ix := range e.Endpoints.Items {
  89. if e.Endpoints.Items[ix].Name == endpoints.Name {
  90. e.Endpoints.Items[ix] = *endpoints
  91. }
  92. }
  93. e.Endpoints.Items = append(e.Endpoints.Items, *endpoints)
  94. return endpoints, false, nil
  95. }
  96. func (e *EndpointRegistry) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
  97. // TODO: support namespaces in this mock
  98. e.lock.Lock()
  99. defer e.lock.Unlock()
  100. if e.Err != nil {
  101. return nil, false, e.Err
  102. }
  103. if e.Endpoints != nil {
  104. var newList []api.Endpoints
  105. for _, endpoint := range e.Endpoints.Items {
  106. if endpoint.Name != name {
  107. newList = append(newList, endpoint)
  108. }
  109. }
  110. e.Endpoints.Items = newList
  111. }
  112. return nil, true, nil
  113. }
  114. func (e *EndpointRegistry) DeleteCollection(ctx context.Context, _ rest.ValidateObjectFunc, _ *metav1.DeleteOptions, _ *metainternalversion.ListOptions) (runtime.Object, error) {
  115. return nil, fmt.Errorf("unimplemented!")
  116. }