k8s.io/kubernetes@v1.29.3/pkg/controlplane/controller/crdregistration/crdregistration_controller_test.go (about) 1 /* 2 Copyright 2017 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 crdregistration 18 19 import ( 20 "reflect" 21 "testing" 22 23 apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" 24 crdlisters "k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/v1" 25 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 26 "k8s.io/apimachinery/pkg/runtime/schema" 27 "k8s.io/client-go/tools/cache" 28 apiregistration "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1" 29 ) 30 31 func TestHandleVersionUpdate(t *testing.T) { 32 tests := []struct { 33 name string 34 startingCRDs []*apiextensionsv1.CustomResourceDefinition 35 version schema.GroupVersion 36 37 expectedAdded []*apiregistration.APIService 38 expectedRemoved []string 39 }{ 40 { 41 name: "simple add crd", 42 startingCRDs: []*apiextensionsv1.CustomResourceDefinition{ 43 { 44 Spec: apiextensionsv1.CustomResourceDefinitionSpec{ 45 Group: "group.com", 46 // Version field is deprecated and crd registration won't rely on it at all. 47 // defaulting route will fill up Versions field if user only provided version field. 48 Versions: []apiextensionsv1.CustomResourceDefinitionVersion{ 49 { 50 Name: "v1", 51 Served: true, 52 Storage: true, 53 }, 54 }, 55 }, 56 }, 57 }, 58 version: schema.GroupVersion{Group: "group.com", Version: "v1"}, 59 60 expectedAdded: []*apiregistration.APIService{ 61 { 62 ObjectMeta: metav1.ObjectMeta{Name: "v1.group.com"}, 63 Spec: apiregistration.APIServiceSpec{ 64 Group: "group.com", 65 Version: "v1", 66 GroupPriorityMinimum: 1000, 67 VersionPriority: 100, 68 }, 69 }, 70 }, 71 }, 72 { 73 name: "simple remove crd", 74 startingCRDs: []*apiextensionsv1.CustomResourceDefinition{ 75 { 76 Spec: apiextensionsv1.CustomResourceDefinitionSpec{ 77 Group: "group.com", 78 Versions: []apiextensionsv1.CustomResourceDefinitionVersion{ 79 { 80 Name: "v1", 81 Served: true, 82 Storage: true, 83 }, 84 }, 85 }, 86 }, 87 }, 88 version: schema.GroupVersion{Group: "group.com", Version: "v2"}, 89 90 expectedRemoved: []string{"v2.group.com"}, 91 }, 92 } 93 94 for _, test := range tests { 95 t.Run(test.name, func(t *testing.T) { 96 registration := &fakeAPIServiceRegistration{} 97 crdCache := cache.NewIndexer(cache.DeletionHandlingMetaNamespaceKeyFunc, cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc}) 98 crdLister := crdlisters.NewCustomResourceDefinitionLister(crdCache) 99 c := crdRegistrationController{ 100 crdLister: crdLister, 101 apiServiceRegistration: registration, 102 } 103 for i := range test.startingCRDs { 104 crdCache.Add(test.startingCRDs[i]) 105 } 106 107 c.handleVersionUpdate(test.version) 108 109 if !reflect.DeepEqual(test.expectedAdded, registration.added) { 110 t.Errorf("%s expected %v, got %v", test.name, test.expectedAdded, registration.added) 111 } 112 if !reflect.DeepEqual(test.expectedRemoved, registration.removed) { 113 t.Errorf("%s expected %v, got %v", test.name, test.expectedRemoved, registration.removed) 114 } 115 }) 116 } 117 } 118 119 type fakeAPIServiceRegistration struct { 120 added []*apiregistration.APIService 121 removed []string 122 } 123 124 func (a *fakeAPIServiceRegistration) AddAPIServiceToSync(in *apiregistration.APIService) { 125 a.added = append(a.added, in) 126 } 127 func (a *fakeAPIServiceRegistration) RemoveAPIServiceToSync(name string) { 128 a.removed = append(a.removed, name) 129 }