knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/webhook/testing/listers.go (about) 1 /* 2 Copyright 2019 The Knative 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 testing 18 19 import ( 20 admissionregistrationv1 "k8s.io/api/admissionregistration/v1" 21 appsv1 "k8s.io/api/apps/v1" 22 autoscalingv2 "k8s.io/api/autoscaling/v2" 23 corev1 "k8s.io/api/core/v1" 24 apixv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" 25 apixlisters "k8s.io/apiextensions-apiserver/pkg/client/listers/apiextensions/v1" 26 "k8s.io/apimachinery/pkg/runtime" 27 fakekubeclientset "k8s.io/client-go/kubernetes/fake" 28 admissionlisters "k8s.io/client-go/listers/admissionregistration/v1" 29 30 fakeapix "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/fake" 31 appsv1listers "k8s.io/client-go/listers/apps/v1" 32 autoscalingv2listers "k8s.io/client-go/listers/autoscaling/v2" 33 corev1listers "k8s.io/client-go/listers/core/v1" 34 "k8s.io/client-go/tools/cache" 35 "knative.dev/pkg/reconciler/testing" 36 pkgtesting "knative.dev/pkg/testing" 37 pkgducktesting "knative.dev/pkg/testing/duck" 38 ) 39 40 var clientSetSchemes = []func(*runtime.Scheme) error{ 41 fakekubeclientset.AddToScheme, 42 autoscalingv2.AddToScheme, 43 pkgtesting.AddToScheme, 44 pkgducktesting.AddToScheme, 45 fakeapix.AddToScheme, 46 } 47 48 // Listers is used to synthesize informer-style Listers from fixed lists of resources in tests. 49 type Listers struct { 50 sorter testing.ObjectSorter 51 } 52 53 // NewListers constructs a Listers from a collection of objects. 54 func NewListers(objs []runtime.Object) Listers { 55 scheme := NewScheme() 56 57 ls := Listers{ 58 sorter: testing.NewObjectSorter(scheme), 59 } 60 61 ls.sorter.AddObjects(objs...) 62 63 return ls 64 } 65 66 // NewScheme constructs a scheme from the set of client schemes supported by this package. 67 func NewScheme() *runtime.Scheme { 68 scheme := runtime.NewScheme() 69 70 for _, addTo := range clientSetSchemes { 71 addTo(scheme) 72 } 73 return scheme 74 } 75 76 // NewScheme constructs a scheme from the set of client schemes supported by this lister. 77 func (*Listers) NewScheme() *runtime.Scheme { 78 return NewScheme() 79 } 80 81 // IndexerFor returns the indexer for the given object. 82 func (l *Listers) IndexerFor(obj runtime.Object) cache.Indexer { 83 return l.sorter.IndexerForObjectType(obj) 84 } 85 86 // GetKubeObjects filters the Listers initial list of objects to built-in types 87 func (l *Listers) GetKubeObjects() []runtime.Object { 88 return l.sorter.ObjectsForSchemeFunc(fakekubeclientset.AddToScheme) 89 } 90 91 // GetTestObjects filters the Lister's initial list of objects to types defined in knative/pkg/testing 92 func (l *Listers) GetTestObjects() []runtime.Object { 93 return l.sorter.ObjectsForSchemeFunc(pkgtesting.AddToScheme) 94 } 95 96 // GetDuckObjects filters the Listers initial list of objects to types defined in knative/pkg 97 func (l *Listers) GetDuckObjects() []runtime.Object { 98 return l.sorter.ObjectsForSchemeFunc(pkgducktesting.AddToScheme) 99 } 100 101 // GetAPIExtensionsObjects filters the Listers initial list of objects to types definite in k8s.io/apiextensions 102 func (l *Listers) GetAPIExtensionsObjects() []runtime.Object { 103 return l.sorter.ObjectsForSchemeFunc(fakeapix.AddToScheme) 104 } 105 106 // GetHorizontalPodAutoscalerLister gets lister for HorizontalPodAutoscaler resources. 107 func (l *Listers) GetHorizontalPodAutoscalerLister() autoscalingv2listers.HorizontalPodAutoscalerLister { 108 return autoscalingv2listers.NewHorizontalPodAutoscalerLister(l.IndexerFor(&autoscalingv2.HorizontalPodAutoscaler{})) 109 } 110 111 // GetDeploymentLister gets lister for K8s Deployment resource. 112 func (l *Listers) GetDeploymentLister() appsv1listers.DeploymentLister { 113 return appsv1listers.NewDeploymentLister(l.IndexerFor(&appsv1.Deployment{})) 114 } 115 116 // GetK8sServiceLister gets lister for K8s Service resource. 117 func (l *Listers) GetK8sServiceLister() corev1listers.ServiceLister { 118 return corev1listers.NewServiceLister(l.IndexerFor(&corev1.Service{})) 119 } 120 121 // GetEndpointsLister gets lister for K8s Endpoints resource. 122 func (l *Listers) GetEndpointsLister() corev1listers.EndpointsLister { 123 return corev1listers.NewEndpointsLister(l.IndexerFor(&corev1.Endpoints{})) 124 } 125 126 // GetSecretLister gets lister for K8s Secret resource. 127 func (l *Listers) GetSecretLister() corev1listers.SecretLister { 128 return corev1listers.NewSecretLister(l.IndexerFor(&corev1.Secret{})) 129 } 130 131 // GetConfigMapLister gets lister for K8s ConfigMap resource. 132 func (l *Listers) GetConfigMapLister() corev1listers.ConfigMapLister { 133 return corev1listers.NewConfigMapLister(l.IndexerFor(&corev1.ConfigMap{})) 134 } 135 136 // GetNamespaceLister gets lister for Namespace resource. 137 func (l *Listers) GetNamespaceLister() corev1listers.NamespaceLister { 138 return corev1listers.NewNamespaceLister(l.IndexerFor(&corev1.Namespace{})) 139 } 140 141 // GetMutatingWebhookConfigurationLister gets lister for K8s MutatingWebhookConfiguration resource. 142 func (l *Listers) GetMutatingWebhookConfigurationLister() admissionlisters.MutatingWebhookConfigurationLister { 143 return admissionlisters.NewMutatingWebhookConfigurationLister(l.IndexerFor(&admissionregistrationv1.MutatingWebhookConfiguration{})) 144 } 145 146 // GetValidatingWebhookConfigurationLister gets lister for K8s ValidatingWebhookConfiguration resource. 147 func (l *Listers) GetValidatingWebhookConfigurationLister() admissionlisters.ValidatingWebhookConfigurationLister { 148 return admissionlisters.NewValidatingWebhookConfigurationLister(l.IndexerFor(&admissionregistrationv1.ValidatingWebhookConfiguration{})) 149 } 150 151 func (l *Listers) GetCustomResourceDefinitionLister() apixlisters.CustomResourceDefinitionLister { 152 return apixlisters.NewCustomResourceDefinitionLister(l.IndexerFor(&apixv1.CustomResourceDefinition{})) 153 }