@@ -22,9 +22,11 @@ import (
22
22
"testing"
23
23
"time"
24
24
25
+ runtimeClient "github.com/fluxcd/pkg/runtime/client"
25
26
. "github.com/onsi/gomega"
26
27
apierrors "k8s.io/apimachinery/pkg/api/errors"
27
28
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
+ "k8s.io/apimachinery/pkg/runtime/schema"
28
30
"k8s.io/apimachinery/pkg/types"
29
31
"sigs.k8s.io/controller-runtime/pkg/client"
30
32
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -368,3 +370,79 @@ data: {}
368
370
g .Expect (msg ).
369
371
To (ContainSubstring ("failed to evaluate the CEL expression 'has(data.foo.bar)': no such attribute(s): data.foo.bar" ))
370
372
}
373
+
374
+ func TestKustomizationReconciler_RESTMapper (t * testing.T ) {
375
+ g := NewWithT (t )
376
+ id := "rm-" + randStringRunes (5 )
377
+ resultK := & kustomizev1.Kustomization {}
378
+
379
+ restMapper , err := runtimeClient .NewDynamicRESTMapper (testEnv .Config )
380
+ g .Expect (err ).NotTo (HaveOccurred ())
381
+
382
+ err = createNamespace (id )
383
+ g .Expect (err ).NotTo (HaveOccurred (), "failed to create test namespace" )
384
+
385
+ err = createKubeConfigSecret (id )
386
+ g .Expect (err ).NotTo (HaveOccurred (), "failed to create kubeconfig secret" )
387
+
388
+ artifactName := "val-" + randStringRunes (5 )
389
+ artifactChecksum , err := testServer .ArtifactFromDir ("testdata/restmapper" , artifactName )
390
+ g .Expect (err ).ToNot (HaveOccurred ())
391
+
392
+ repositoryName := types.NamespacedName {
393
+ Name : fmt .Sprintf ("val-%s" , randStringRunes (5 )),
394
+ Namespace : id ,
395
+ }
396
+
397
+ err = applyGitRepository (repositoryName , artifactName , "main/" + artifactChecksum )
398
+ g .Expect (err ).NotTo (HaveOccurred ())
399
+
400
+ kustomization := & kustomizev1.Kustomization {}
401
+ kustomization .Name = id
402
+ kustomization .Namespace = id
403
+ kustomization .Spec = kustomizev1.KustomizationSpec {
404
+ Interval : metav1.Duration {Duration : 10 * time .Minute },
405
+ Prune : true ,
406
+ Path : "./" ,
407
+ Wait : true ,
408
+ SourceRef : kustomizev1.CrossNamespaceSourceReference {
409
+ Name : repositoryName .Name ,
410
+ Namespace : repositoryName .Namespace ,
411
+ Kind : sourcev1 .GitRepositoryKind ,
412
+ },
413
+ KubeConfig : & meta.KubeConfigReference {
414
+ SecretRef : meta.SecretKeyReference {
415
+ Name : "kubeconfig" ,
416
+ },
417
+ },
418
+ }
419
+
420
+ g .Expect (k8sClient .Create (context .Background (), kustomization )).To (Succeed ())
421
+
422
+ g .Eventually (func () bool {
423
+ _ = k8sClient .Get (context .Background (), client .ObjectKeyFromObject (kustomization ), resultK )
424
+ return isReconcileSuccess (resultK ) && resultK .Status .LastAttemptedRevision == "main/" + artifactChecksum
425
+ }, timeout , time .Second ).Should (BeTrue ())
426
+
427
+ t .Run ("discovers newly registered CRD and preferred version" , func (t * testing.T ) {
428
+ mapping , err := restMapper .RESTMapping (schema.GroupKind {Kind : "ClusterCleanupPolicy" , Group : "kyverno.io" })
429
+ g .Expect (err ).NotTo (HaveOccurred ())
430
+ g .Expect (mapping .Resource .Version ).To (Equal ("v2" ))
431
+ })
432
+
433
+ t .Run ("finalizes object" , func (t * testing.T ) {
434
+ g .Expect (k8sClient .Delete (context .Background (), resultK )).To (Succeed ())
435
+
436
+ g .Eventually (func () bool {
437
+ err = k8sClient .Get (context .Background (), client .ObjectKeyFromObject (kustomization ), resultK )
438
+ return apierrors .IsNotFound (err )
439
+ }, timeout , time .Second ).Should (BeTrue ())
440
+ })
441
+
442
+ t .Run ("discovery fails for deleted CRD" , func (t * testing.T ) {
443
+ newMapper , err := runtimeClient .NewDynamicRESTMapper (testEnv .Config )
444
+ g .Expect (err ).NotTo (HaveOccurred ())
445
+ _ , err = newMapper .RESTMapping (schema.GroupKind {Kind : "ClusterCleanupPolicy" , Group : "kyverno.io" })
446
+ g .Expect (err ).To (HaveOccurred ())
447
+ })
448
+ }
0 commit comments