@@ -26,9 +26,12 @@ import (
26
26
"testing"
27
27
"time"
28
28
29
- "k8s.io/api/admission/v1beta1 "
29
+ v1 "k8s.io/api/admission/v1 "
30
30
corev1 "k8s.io/api/core/v1"
31
31
apiextensionsclientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
32
+ "k8s.io/apimachinery/pkg/util/version"
33
+ utilfeature "k8s.io/apiserver/pkg/util/feature"
34
+ featuregatetesting "k8s.io/component-base/featuregate/testing"
32
35
apiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
33
36
"k8s.io/kubernetes/pkg/apis/admissionregistration"
34
37
admissionregistrationv1apis "k8s.io/kubernetes/pkg/apis/admissionregistration/v1"
@@ -398,15 +401,40 @@ func createV1ValidatingPolicyAndBinding(client clientset.Interface, convertedRul
398
401
return nil
399
402
}
400
403
404
+ // This test shows that policy intercepts all requests for all resources,
405
+ // subresources, verbs, and input versions of policy/binding.
406
+ // The test emulates v1.33 as that was the last version before v1beta1 resource was removed.
407
+ // Remove this test once v1.33 cannot be emulated in v1.37.
408
+ //
409
+ // This test tries to mirror very closely the same test for webhook admission
410
+ // test/integration/apiserver/admissionwebhook/admission_test.go testWebhookAdmission
411
+ func TestPolicyAdmissionV1beta1 (t * testing.T ) {
412
+ featuregatetesting .SetFeatureGateEmulationVersionDuringTest (t , utilfeature .DefaultFeatureGate , version .MustParse (vapV1beta1LastEmulatableVersion ))
413
+ testPolicyAdmission (t , true )
414
+ }
415
+
401
416
// This test shows that policy intercepts all requests for all resources,
402
417
// subresources, verbs, and input versions of policy/binding.
403
418
//
404
419
// This test tries to mirror very closely the same test for webhook admission
405
420
// test/integration/apiserver/admissionwebhook/admission_test.go testWebhookAdmission
406
421
func TestPolicyAdmission (t * testing.T ) {
422
+ testPolicyAdmission (t , false )
423
+ }
424
+
425
+ func testPolicyAdmission (t * testing.T , supportV1Beta1 bool ) {
426
+ supportedVersions := []string {}
427
+ if supportV1Beta1 {
428
+ supportedVersions = append (supportedVersions , "v1beta1" )
429
+ } else {
430
+ supportedVersions = append (supportedVersions , "v1" )
431
+ }
432
+
407
433
holder := & policyExpectationHolder {
434
+ supportedVersions : supportedVersions ,
408
435
holder : holder {
409
436
t : t ,
437
+ supportedVersions : supportedVersions ,
410
438
gvrToConvertedGVR : map [metav1.GroupVersionResource ]metav1.GroupVersionResource {},
411
439
gvrToConvertedGVK : map [metav1.GroupVersionResource ]schema.GroupVersionKind {},
412
440
},
@@ -530,11 +558,14 @@ func TestPolicyAdmission(t *testing.T) {
530
558
holder .gvrToConvertedGVK [metaGVR ] = schema.GroupVersionKind {Group : resourcesByGVR [convertedGVR ].Group , Version : resourcesByGVR [convertedGVR ].Version , Kind : resourcesByGVR [convertedGVR ].Kind }
531
559
}
532
560
533
- if err := createV1beta1ValidatingPolicyAndBinding (client , convertedV1beta1Rules ); err != nil {
534
- t .Fatal (err )
535
- }
536
- if err := createV1ValidatingPolicyAndBinding (client , convertedV1Rules ); err != nil {
537
- t .Fatal (err )
561
+ if supportV1Beta1 {
562
+ if err := createV1beta1ValidatingPolicyAndBinding (client , convertedV1beta1Rules ); err != nil {
563
+ t .Fatal (err )
564
+ }
565
+ } else {
566
+ if err := createV1ValidatingPolicyAndBinding (client , convertedV1Rules ); err != nil {
567
+ t .Fatal (err )
568
+ }
538
569
}
539
570
540
571
// Allow the policy & binding to establish
@@ -554,14 +585,15 @@ func TestPolicyAdmission(t *testing.T) {
554
585
holder .reset (t )
555
586
testFunc := getTestFunc (gvr , verb )
556
587
testFunc (& testContext {
557
- t : t ,
558
- admissionHolder : holder ,
559
- client : dynamicClient ,
560
- clientset : client ,
561
- verb : verb ,
562
- gvr : gvr ,
563
- resource : resource ,
564
- resources : resourcesByGVR ,
588
+ t : t ,
589
+ emulateV1beta1Version : supportV1Beta1 ,
590
+ admissionHolder : holder ,
591
+ client : dynamicClient ,
592
+ clientset : client ,
593
+ verb : verb ,
594
+ gvr : gvr ,
595
+ resource : resource ,
596
+ resources : resourcesByGVR ,
565
597
})
566
598
holder .verify (t )
567
599
})
@@ -583,8 +615,9 @@ func TestPolicyAdmission(t *testing.T) {
583
615
584
616
type policyExpectationHolder struct {
585
617
holder
586
- warningLock sync.Mutex
587
- warnings []string
618
+ supportedVersions []string
619
+ warningLock sync.Mutex
620
+ warnings []string
588
621
}
589
622
590
623
func (p * policyExpectationHolder ) reset (t * testing.T ) {
@@ -595,7 +628,7 @@ func (p *policyExpectationHolder) reset(t *testing.T) {
595
628
p .holder .reset (t )
596
629
597
630
}
598
- func (p * policyExpectationHolder ) expect (gvr schema.GroupVersionResource , gvk , optionsGVK schema.GroupVersionKind , operation v1beta1 .Operation , name , namespace string , object , oldObject , options bool ) {
631
+ func (p * policyExpectationHolder ) expect (gvr schema.GroupVersionResource , gvk , optionsGVK schema.GroupVersionKind , operation v1 .Operation , name , namespace string , object , oldObject , options bool ) {
599
632
p .holder .expect (gvr , gvk , optionsGVK , operation , name , namespace , object , oldObject , options )
600
633
601
634
p .lock .Lock ()
@@ -604,7 +637,7 @@ func (p *policyExpectationHolder) expect(gvr schema.GroupVersionResource, gvk, o
604
637
p .recorded = map [webhookOptions ]* admissionRequest {}
605
638
for _ , phase := range []string {validation } {
606
639
for _ , converted := range []bool {true , false } {
607
- for _ , version := range [] string { "v1beta1" , "v1" } {
640
+ for _ , version := range p . supportedVersions {
608
641
p .recorded [webhookOptions {version : version , phase : phase , converted : converted }] = nil
609
642
}
610
643
}
0 commit comments