@@ -2341,33 +2341,64 @@ func generateValidationsWithAuthzCheck(num int, exp string) []admissionregistrat
2341
2341
2342
2342
// TestCRDParams tests that a CustomResource can be used as a param resource for a ValidatingAdmissionPolicy.
2343
2343
func TestCRDParams (t * testing.T ) {
2344
+ generic .PolicyRefreshInterval = 10 * time .Millisecond
2345
+ featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , genericfeatures .ValidatingAdmissionPolicy , true )
2346
+ server , err := apiservertesting .StartTestServer (t , nil , []string {
2347
+ "--enable-admission-plugins" , "ValidatingAdmissionPolicy" ,
2348
+ }, framework .SharedEtcd ())
2349
+ if err != nil {
2350
+ t .Fatal (err )
2351
+ }
2352
+ defer server .TearDownFn ()
2353
+
2354
+ config := server .ClientConfig
2355
+ client , err := clientset .NewForConfig (config )
2356
+ if err != nil {
2357
+ t .Fatal (err )
2358
+ }
2359
+
2360
+ crd := versionedCustomResourceDefinition ()
2361
+ etcd .CreateTestCRDs (t , apiextensionsclientset .NewForConfigOrDie (server .ClientConfig ), false , crd )
2362
+ dynamicClient , err := dynamic .NewForConfig (config )
2363
+ if err != nil {
2364
+ t .Fatal (err )
2365
+ }
2366
+ gvr := schema.GroupVersionResource {
2367
+ Group : crd .Spec .Group ,
2368
+ Version : crd .Spec .Versions [0 ].Name ,
2369
+ Resource : crd .Spec .Names .Plural ,
2370
+ }
2371
+ crClient := dynamicClient .Resource (gvr )
2372
+
2373
+ resource := & unstructured.Unstructured {Object : map [string ]interface {}{
2374
+ "apiVersion" : "awesome.bears.com/v1" ,
2375
+ "kind" : "Panda" ,
2376
+ "metadata" : map [string ]interface {}{
2377
+ "name" : "config-obj" ,
2378
+ },
2379
+ "spec" : map [string ]interface {}{
2380
+ "nameCheck" : "crd-test-k8s" ,
2381
+ },
2382
+ }}
2383
+ _ , err = crClient .Create (context .TODO (), resource , metav1.CreateOptions {})
2384
+ if err != nil {
2385
+ t .Fatalf ("error creating %s: %s" , gvr , err )
2386
+ }
2387
+
2344
2388
testcases := []struct {
2345
2389
name string
2346
- resource * unstructured.Unstructured
2347
2390
policy * admissionregistrationv1.ValidatingAdmissionPolicy
2348
- policyBinding * admissionregistrationv1.ValidatingAdmissionPolicyBinding
2349
2391
namespace * v1.Namespace
2350
2392
err string
2351
2393
failureReason metav1.StatusReason
2352
2394
}{
2353
2395
{
2354
2396
name : "a rule that uses data from a CRD param resource does NOT pass" ,
2355
- resource : & unstructured.Unstructured {Object : map [string ]interface {}{
2356
- "apiVersion" : "awesome.bears.com/v1" ,
2357
- "kind" : "Panda" ,
2358
- "metadata" : map [string ]interface {}{
2359
- "name" : "config-obj" ,
2360
- },
2361
- "spec" : map [string ]interface {}{
2362
- "nameCheck" : "crd-test-k8s" ,
2363
- },
2364
- }},
2365
2397
policy : withValidations ([]admissionregistrationv1.Validation {
2366
2398
{
2367
2399
Expression : "params.spec.nameCheck == object.metadata.name" ,
2368
2400
},
2369
2401
}, withNamespaceMatch (withParams (withCRDParamKind ("Panda" , "awesome.bears.com" , "v1" ), withFailurePolicy (admissionregistrationv1 .Fail , makePolicy ("test-policy" ))))),
2370
- policyBinding : makeBinding ("crd-policy-binding" , "test-policy" , "config-obj" ),
2371
2402
namespace : & v1.Namespace {
2372
2403
ObjectMeta : metav1.ObjectMeta {
2373
2404
Name : "incorrect-name" ,
@@ -2378,22 +2409,11 @@ func TestCRDParams(t *testing.T) {
2378
2409
},
2379
2410
{
2380
2411
name : "a rule that uses data from a CRD param resource that does pass" ,
2381
- resource : & unstructured.Unstructured {Object : map [string ]interface {}{
2382
- "apiVersion" : "awesome.bears.com/v1" ,
2383
- "kind" : "Panda" ,
2384
- "metadata" : map [string ]interface {}{
2385
- "name" : "config-obj" ,
2386
- },
2387
- "spec" : map [string ]interface {}{
2388
- "nameCheck" : "crd-test-k8s" ,
2389
- },
2390
- }},
2391
2412
policy : withValidations ([]admissionregistrationv1.Validation {
2392
2413
{
2393
2414
Expression : "params.spec.nameCheck == object.metadata.name" ,
2394
2415
},
2395
2416
}, withNamespaceMatch (withParams (withCRDParamKind ("Panda" , "awesome.bears.com" , "v1" ), withFailurePolicy (admissionregistrationv1 .Fail , makePolicy ("test-policy" ))))),
2396
- policyBinding : makeBinding ("crd-policy-binding" , "test-policy" , "config-obj" ),
2397
2417
namespace : & v1.Namespace {
2398
2418
ObjectMeta : metav1.ObjectMeta {
2399
2419
Name : "crd-test-k8s" ,
@@ -2405,53 +2425,25 @@ func TestCRDParams(t *testing.T) {
2405
2425
2406
2426
for _ , testcase := range testcases {
2407
2427
t .Run (testcase .name , func (t * testing.T ) {
2408
- featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , genericfeatures .ValidatingAdmissionPolicy , true )
2409
- server , err := apiservertesting .StartTestServer (t , nil , []string {
2410
- "--enable-admission-plugins" , "ValidatingAdmissionPolicy" ,
2411
- }, framework .SharedEtcd ())
2412
- if err != nil {
2413
- t .Fatal (err )
2414
- }
2415
- defer server .TearDownFn ()
2416
-
2417
- config := server .ClientConfig
2418
-
2419
- client , err := clientset .NewForConfig (config )
2420
- if err != nil {
2421
- t .Fatal (err )
2422
- }
2423
-
2424
- crd := versionedCustomResourceDefinition ()
2425
- etcd .CreateTestCRDs (t , apiextensionsclientset .NewForConfigOrDie (server .ClientConfig ), false , crd )
2426
- dynamicClient , err := dynamic .NewForConfig (config )
2427
- if err != nil {
2428
- t .Fatal (err )
2429
- }
2430
- gvr := schema.GroupVersionResource {
2431
- Group : crd .Spec .Group ,
2432
- Version : crd .Spec .Versions [0 ].Name ,
2433
- Resource : crd .Spec .Names .Plural ,
2434
- }
2435
- crClient := dynamicClient .Resource (gvr )
2436
- _ , err = crClient .Create (context .TODO (), testcase .resource , metav1.CreateOptions {})
2437
- if err != nil {
2438
- t .Fatalf ("error creating %s: %s" , gvr , err )
2439
- }
2440
-
2441
2428
policy := withWaitReadyConstraintAndExpression (testcase .policy )
2442
2429
if _ , err := client .AdmissionregistrationV1 ().ValidatingAdmissionPolicies ().Create (context .TODO (), policy , metav1.CreateOptions {}); err != nil {
2443
2430
t .Fatal (err )
2444
2431
}
2445
2432
// remove default namespace since the CRD is cluster-scoped
2446
- testcase .policyBinding .Spec .ParamRef .Namespace = ""
2447
- if err := createAndWaitReady (t , client , testcase .policyBinding , nil ); err != nil {
2433
+ policyBinding := makeBinding ("crd-policy-binding" , "test-policy" , "config-obj" )
2434
+ policyBinding .Spec .ParamRef .Namespace = ""
2435
+ if err := createAndWaitReady (t , client , policyBinding , nil ); err != nil {
2448
2436
t .Fatal (err )
2449
2437
}
2450
2438
2451
2439
_ , err = client .CoreV1 ().Namespaces ().Create (context .TODO (), testcase .namespace , metav1.CreateOptions {})
2452
2440
2453
2441
checkExpectedError (t , err , testcase .err )
2454
2442
checkFailureReason (t , err , testcase .failureReason )
2443
+ if err := cleanupPolicy (t , client , policy , policyBinding ); err != nil {
2444
+ t .Fatalf ("error while cleaning up policy and its bindings: %v" , err )
2445
+ }
2446
+
2455
2447
})
2456
2448
}
2457
2449
}
0 commit comments