@@ -22,7 +22,10 @@ import (
22
22
"testing"
23
23
24
24
"k8s.io/api/admissionregistration/v1"
25
+ corev1 "k8s.io/api/core/v1"
26
+ "k8s.io/apimachinery/pkg/api/errors"
25
27
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28
+ "k8s.io/apimachinery/pkg/labels"
26
29
"k8s.io/apimachinery/pkg/runtime"
27
30
"k8s.io/apimachinery/pkg/runtime/schema"
28
31
"k8s.io/apiserver/pkg/admission"
@@ -75,7 +78,7 @@ func TestShouldCallHook(t *testing.T) {
75
78
}{
76
79
{
77
80
name : "no rules (just write)" ,
78
- webhook : & v1.ValidatingWebhook {Rules : []v1.RuleWithOperations {}},
81
+ webhook : & v1.ValidatingWebhook {NamespaceSelector : & metav1. LabelSelector {}, Rules : []v1.RuleWithOperations {}},
79
82
attrs : admission .NewAttributesRecord (nil , nil , schema.GroupVersionKind {"apps" , "v1" , "Deployment" }, "ns" , "name" , schema.GroupVersionResource {"apps" , "v1" , "deployments" }, "" , admission .Create , & metav1.CreateOptions {}, false , nil ),
80
83
expectCall : false ,
81
84
},
@@ -329,3 +332,82 @@ func TestShouldCallHook(t *testing.T) {
329
332
})
330
333
}
331
334
}
335
+
336
+ type fakeNamespaceLister struct {
337
+ namespaces map [string ]* corev1.Namespace
338
+ }
339
+
340
+ func (f fakeNamespaceLister ) List (selector labels.Selector ) (ret []* corev1.Namespace , err error ) {
341
+ return nil , nil
342
+ }
343
+ func (f fakeNamespaceLister ) Get (name string ) (* corev1.Namespace , error ) {
344
+ ns , ok := f .namespaces [name ]
345
+ if ok {
346
+ return ns , nil
347
+ }
348
+ return nil , errors .NewNotFound (corev1 .Resource ("namespaces" ), name )
349
+ }
350
+
351
+ func BenchmarkShouldCallHook (b * testing.B ) {
352
+ allScopes := v1 .AllScopes
353
+ equivalentMatch := v1 .Equivalent
354
+
355
+ namespace1Labels := map [string ]string {"ns" : "ns1" }
356
+ namespace1 := corev1.Namespace {
357
+ ObjectMeta : metav1.ObjectMeta {
358
+ Name : "ns1" ,
359
+ Labels : namespace1Labels ,
360
+ },
361
+ }
362
+ namespaceLister := fakeNamespaceLister {map [string ]* corev1.Namespace {"ns" : & namespace1 }}
363
+
364
+ mapper := runtime .NewEquivalentResourceRegistryWithIdentity (func (resource schema.GroupResource ) string {
365
+ if resource .Resource == "deployments" {
366
+ // co-locate deployments in all API groups
367
+ return "/deployments"
368
+ }
369
+ return ""
370
+ })
371
+ mapper .RegisterKindFor (schema.GroupVersionResource {"extensions" , "v1beta1" , "deployments" }, "" , schema.GroupVersionKind {"extensions" , "v1beta1" , "Deployment" })
372
+ mapper .RegisterKindFor (schema.GroupVersionResource {"apps" , "v1" , "deployments" }, "" , schema.GroupVersionKind {"apps" , "v1" , "Deployment" })
373
+ mapper .RegisterKindFor (schema.GroupVersionResource {"apps" , "v1beta1" , "deployments" }, "" , schema.GroupVersionKind {"apps" , "v1beta1" , "Deployment" })
374
+ mapper .RegisterKindFor (schema.GroupVersionResource {"apps" , "v1alpha1" , "deployments" }, "" , schema.GroupVersionKind {"apps" , "v1alpha1" , "Deployment" })
375
+
376
+ mapper .RegisterKindFor (schema.GroupVersionResource {"extensions" , "v1beta1" , "deployments" }, "scale" , schema.GroupVersionKind {"extensions" , "v1beta1" , "Scale" })
377
+ mapper .RegisterKindFor (schema.GroupVersionResource {"apps" , "v1" , "deployments" }, "scale" , schema.GroupVersionKind {"autoscaling" , "v1" , "Scale" })
378
+ mapper .RegisterKindFor (schema.GroupVersionResource {"apps" , "v1beta1" , "deployments" }, "scale" , schema.GroupVersionKind {"apps" , "v1beta1" , "Scale" })
379
+ mapper .RegisterKindFor (schema.GroupVersionResource {"apps" , "v1alpha1" , "deployments" }, "scale" , schema.GroupVersionKind {"apps" , "v1alpha1" , "Scale" })
380
+
381
+ mapper .RegisterKindFor (schema.GroupVersionResource {"apps" , "v1" , "statefulset" }, "" , schema.GroupVersionKind {"apps" , "v1" , "StatefulSet" })
382
+ mapper .RegisterKindFor (schema.GroupVersionResource {"apps" , "v1beta1" , "statefulset" }, "" , schema.GroupVersionKind {"apps" , "v1beta1" , "StatefulSet" })
383
+ mapper .RegisterKindFor (schema.GroupVersionResource {"apps" , "v1beta2" , "statefulset" }, "" , schema.GroupVersionKind {"apps" , "v1beta2" , "StatefulSet" })
384
+
385
+ mapper .RegisterKindFor (schema.GroupVersionResource {"apps" , "v1" , "statefulset" }, "scale" , schema.GroupVersionKind {"apps" , "v1" , "Scale" })
386
+ mapper .RegisterKindFor (schema.GroupVersionResource {"apps" , "v1beta1" , "statefulset" }, "scale" , schema.GroupVersionKind {"apps" , "v1beta1" , "Scale" })
387
+ mapper .RegisterKindFor (schema.GroupVersionResource {"apps" , "v1alpha2" , "statefulset" }, "scale" , schema.GroupVersionKind {"apps" , "v1beta2" , "Scale" })
388
+
389
+ wb := & v1.ValidatingWebhook {
390
+ MatchPolicy : & equivalentMatch ,
391
+ NamespaceSelector : & metav1.LabelSelector {MatchLabels : map [string ]string {"a" : "b" }},
392
+ ObjectSelector : & metav1.LabelSelector {},
393
+ Rules : []v1.RuleWithOperations {
394
+ {
395
+ Operations : []v1.OperationType {"*" },
396
+ Rule : v1.Rule {APIGroups : []string {"apps" }, APIVersions : []string {"v1beta1" }, Resources : []string {"deployments" , "deployments/scale" }, Scope : & allScopes },
397
+ },
398
+ {
399
+ Operations : []v1.OperationType {"*" },
400
+ Rule : v1.Rule {APIGroups : []string {"extensions" }, APIVersions : []string {"v1beta1" }, Resources : []string {"deployments" , "deployments/scale" }, Scope : & allScopes },
401
+ },
402
+ },
403
+ }
404
+
405
+ wbAccessor := webhook .NewValidatingWebhookAccessor ("webhook" , "webhook-cfg" , wb )
406
+ attrs := admission .NewAttributesRecord (nil , nil , schema.GroupVersionKind {"autoscaling" , "v1" , "Scale" }, "ns" , "name" , schema.GroupVersionResource {"apps" , "v1" , "deployments" }, "scale" , admission .Create , & metav1.CreateOptions {}, false , nil )
407
+ interfaces := & admission.RuntimeObjectInterfaces {EquivalentResourceMapper : mapper }
408
+ a := & Webhook {namespaceMatcher : & namespace.Matcher {NamespaceLister : namespaceLister }, objectMatcher : & object.Matcher {}}
409
+
410
+ for i := 0 ; i < b .N ; i ++ {
411
+ a .ShouldCallHook (wbAccessor , attrs , interfaces )
412
+ }
413
+ }
0 commit comments