@@ -516,26 +516,52 @@ func Test_ValidateAnnotationsAndWarnings(t *testing.T) {
516
516
// Test_ValidateNamespace_WithConfigMapParams tests a ValidatingAdmissionPolicy that validates creation of a Namespace,
517
517
// using ConfigMap as a param reference.
518
518
func Test_ValidateNamespace_WithConfigMapParams (t * testing.T ) {
519
+ generic .PolicyRefreshInterval = 10 * time .Millisecond
520
+ featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , genericfeatures .ValidatingAdmissionPolicy , true )
521
+ server , err := apiservertesting .StartTestServer (t , nil , []string {
522
+ "--enable-admission-plugins" , "ValidatingAdmissionPolicy" ,
523
+ }, framework .SharedEtcd ())
524
+ if err != nil {
525
+ t .Fatal (err )
526
+ }
527
+ defer server .TearDownFn ()
528
+
529
+ config := server .ClientConfig
530
+ client , err := clientset .NewForConfig (config )
531
+ if err != nil {
532
+ t .Fatal (err )
533
+ }
534
+
535
+ policyBinding := makeBinding ("validate-namespace-suffix-binding" , "validate-namespace-suffix" , "validate-namespace-suffix-param" )
536
+ configMap := makeConfigParams ("validate-namespace-suffix-param" , map [string ]string {
537
+ "namespaceSuffix" : "k8s" ,
538
+ })
539
+ if _ , err := client .CoreV1 ().ConfigMaps ("default" ).Create (context .TODO (), configMap , metav1.CreateOptions {}); err != nil {
540
+ t .Fatal (err )
541
+ }
542
+
543
+ valPolicy := withValidations ([]admissionregistrationv1.Validation {
544
+ {
545
+ Expression : "object.metadata.name.endsWith(params.data.namespaceSuffix)" ,
546
+ },
547
+ }, withFailurePolicy (admissionregistrationv1 .Fail , withParams (configParamKind (), withNamespaceMatch (makePolicy ("validate-namespace-suffix" )))))
548
+ policy := withWaitReadyConstraintAndExpression (valPolicy )
549
+ if _ , err := client .AdmissionregistrationV1 ().ValidatingAdmissionPolicies ().Create (context .TODO (), policy , metav1.CreateOptions {}); err != nil {
550
+ t .Fatal (err )
551
+ }
552
+
553
+ if err := createAndWaitReady (t , client , policyBinding , nil ); err != nil {
554
+ t .Fatal (err )
555
+ }
556
+
519
557
testcases := []struct {
520
558
name string
521
- policy * admissionregistrationv1.ValidatingAdmissionPolicy
522
- policyBinding * admissionregistrationv1.ValidatingAdmissionPolicyBinding
523
- configMap * v1.ConfigMap
524
559
namespace * v1.Namespace
525
560
err string
526
561
failureReason metav1.StatusReason
527
562
}{
528
563
{
529
564
name : "namespace name contains suffix enforced by validating admission policy" ,
530
- policy : withValidations ([]admissionregistrationv1.Validation {
531
- {
532
- Expression : "object.metadata.name.endsWith(params.data.namespaceSuffix)" ,
533
- },
534
- }, withFailurePolicy (admissionregistrationv1 .Fail , withParams (configParamKind (), withNamespaceMatch (makePolicy ("validate-namespace-suffix" ))))),
535
- policyBinding : makeBinding ("validate-namespace-suffix-binding" , "validate-namespace-suffix" , "validate-namespace-suffix-param" ),
536
- configMap : makeConfigParams ("validate-namespace-suffix-param" , map [string ]string {
537
- "namespaceSuffix" : "k8s" ,
538
- }),
539
565
namespace : & v1.Namespace {
540
566
ObjectMeta : metav1.ObjectMeta {
541
567
Name : "test-k8s" ,
@@ -545,15 +571,6 @@ func Test_ValidateNamespace_WithConfigMapParams(t *testing.T) {
545
571
},
546
572
{
547
573
name : "namespace name does NOT contain suffix enforced by validating admission policy" ,
548
- policy : withValidations ([]admissionregistrationv1.Validation {
549
- {
550
- Expression : "object.metadata.name.endsWith(params.data.namespaceSuffix)" ,
551
- },
552
- }, withFailurePolicy (admissionregistrationv1 .Fail , withParams (configParamKind (), withNamespaceMatch (makePolicy ("validate-namespace-suffix" ))))),
553
- policyBinding : makeBinding ("validate-namespace-suffix-binding" , "validate-namespace-suffix" , "validate-namespace-suffix-param" ),
554
- configMap : makeConfigParams ("validate-namespace-suffix-param" , map [string ]string {
555
- "namespaceSuffix" : "k8s" ,
556
- }),
557
574
namespace : & v1.Namespace {
558
575
ObjectMeta : metav1.ObjectMeta {
559
576
Name : "test-foo" ,
@@ -563,39 +580,9 @@ func Test_ValidateNamespace_WithConfigMapParams(t *testing.T) {
563
580
failureReason : metav1 .StatusReasonInvalid ,
564
581
},
565
582
}
566
-
567
583
for _ , testcase := range testcases {
568
584
t .Run (testcase .name , func (t * testing.T ) {
569
- featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , genericfeatures .ValidatingAdmissionPolicy , true )
570
- server , err := apiservertesting .StartTestServer (t , nil , []string {
571
- "--enable-admission-plugins" , "ValidatingAdmissionPolicy" ,
572
- }, framework .SharedEtcd ())
573
- if err != nil {
574
- t .Fatal (err )
575
- }
576
- defer server .TearDownFn ()
577
-
578
- config := server .ClientConfig
579
-
580
- client , err := clientset .NewForConfig (config )
581
- if err != nil {
582
- t .Fatal (err )
583
- }
584
-
585
- if _ , err := client .CoreV1 ().ConfigMaps ("default" ).Create (context .TODO (), testcase .configMap , metav1.CreateOptions {}); err != nil {
586
- t .Fatal (err )
587
- }
588
-
589
- policy := withWaitReadyConstraintAndExpression (testcase .policy )
590
- if _ , err := client .AdmissionregistrationV1 ().ValidatingAdmissionPolicies ().Create (context .TODO (), policy , metav1.CreateOptions {}); err != nil {
591
- t .Fatal (err )
592
- }
593
- if err := createAndWaitReady (t , client , testcase .policyBinding , nil ); err != nil {
594
- t .Fatal (err )
595
- }
596
-
597
585
_ , err = client .CoreV1 ().Namespaces ().Create (context .TODO (), testcase .namespace , metav1.CreateOptions {})
598
-
599
586
checkExpectedError (t , err , testcase .err )
600
587
checkFailureReason (t , err , testcase .failureReason )
601
588
})
0 commit comments