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