@@ -526,6 +526,146 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition",
526526 Expect (err ).To (HaveOccurred ())
527527 })
528528
529+ It ("should properly generate webhook definition with namespaceSelector" , func () {
530+ By ("switching into testdata to appease go modules" )
531+ cwd , err := os .Getwd ()
532+ Expect (err ).NotTo (HaveOccurred ())
533+ Expect (os .Chdir ("./testdata/valid-namespaceselector" )).To (Succeed ())
534+ defer func () { Expect (os .Chdir (cwd )).To (Succeed ()) }()
535+
536+ By ("loading the roots" )
537+ pkgs , err := loader .LoadRoots ("." )
538+ Expect (err ).NotTo (HaveOccurred ())
539+ Expect (pkgs ).To (HaveLen (1 ))
540+
541+ By ("setting up the parser" )
542+ reg := & markers.Registry {}
543+ Expect (reg .Register (webhook .ConfigDefinition )).To (Succeed ())
544+ Expect (reg .Register (webhook .WebhookConfigDefinition )).To (Succeed ())
545+
546+ By ("requesting that the manifest be generated" )
547+ outputDir , err := os .MkdirTemp ("" , "webhook-integration-test" )
548+ Expect (err ).NotTo (HaveOccurred ())
549+ defer os .RemoveAll (outputDir )
550+ genCtx := & genall.GenerationContext {
551+ Collector : & markers.Collector {Registry : reg },
552+ Roots : pkgs ,
553+ OutputRule : genall .OutputToDirectory (outputDir ),
554+ }
555+ Expect (webhook.Generator {}.Generate (genCtx )).To (Succeed ())
556+ for _ , r := range genCtx .Roots {
557+ Expect (r .Errors ).To (HaveLen (0 ))
558+ }
559+
560+ By ("loading the generated v1 YAML" )
561+ actualFile , err := os .ReadFile (path .Join (outputDir , "manifests.yaml" ))
562+ Expect (err ).NotTo (HaveOccurred ())
563+ actualManifest := & admissionregv1.ValidatingWebhookConfiguration {}
564+ Expect (yaml .UnmarshalStrict (actualFile , actualManifest )).To (Succeed ())
565+
566+ By ("loading the desired v1 YAML" )
567+ expectedFile , err := os .ReadFile ("manifests.yaml" )
568+ Expect (err ).NotTo (HaveOccurred ())
569+ expectedManifest := & admissionregv1.ValidatingWebhookConfiguration {}
570+ Expect (yaml .UnmarshalStrict (expectedFile , expectedManifest )).To (Succeed ())
571+
572+ By ("comparing the two" )
573+ assertSame (actualManifest , expectedManifest )
574+ })
575+
576+ It ("should properly generate webhook definition with objectSelector" , func () {
577+ By ("switching into testdata to appease go modules" )
578+ cwd , err := os .Getwd ()
579+ Expect (err ).NotTo (HaveOccurred ())
580+ Expect (os .Chdir ("./testdata/valid-objectselector" )).To (Succeed ())
581+ defer func () { Expect (os .Chdir (cwd )).To (Succeed ()) }()
582+
583+ By ("loading the roots" )
584+ pkgs , err := loader .LoadRoots ("." )
585+ Expect (err ).NotTo (HaveOccurred ())
586+ Expect (pkgs ).To (HaveLen (1 ))
587+
588+ By ("setting up the parser" )
589+ reg := & markers.Registry {}
590+ Expect (reg .Register (webhook .ConfigDefinition )).To (Succeed ())
591+ Expect (reg .Register (webhook .WebhookConfigDefinition )).To (Succeed ())
592+
593+ By ("requesting that the manifest be generated" )
594+ outputDir , err := os .MkdirTemp ("" , "webhook-integration-test" )
595+ Expect (err ).NotTo (HaveOccurred ())
596+ defer os .RemoveAll (outputDir )
597+ genCtx := & genall.GenerationContext {
598+ Collector : & markers.Collector {Registry : reg },
599+ Roots : pkgs ,
600+ OutputRule : genall .OutputToDirectory (outputDir ),
601+ }
602+ Expect (webhook.Generator {}.Generate (genCtx )).To (Succeed ())
603+ for _ , r := range genCtx .Roots {
604+ Expect (r .Errors ).To (HaveLen (0 ))
605+ }
606+
607+ By ("loading the generated v1 YAML" )
608+ actualFile , err := os .ReadFile (path .Join (outputDir , "manifests.yaml" ))
609+ Expect (err ).NotTo (HaveOccurred ())
610+ actualManifest := & admissionregv1.MutatingWebhookConfiguration {}
611+ Expect (yaml .UnmarshalStrict (actualFile , actualManifest )).To (Succeed ())
612+
613+ By ("loading the desired v1 YAML" )
614+ expectedFile , err := os .ReadFile ("manifests.yaml" )
615+ Expect (err ).NotTo (HaveOccurred ())
616+ expectedManifest := & admissionregv1.MutatingWebhookConfiguration {}
617+ Expect (yaml .UnmarshalStrict (expectedFile , expectedManifest )).To (Succeed ())
618+
619+ By ("comparing the two" )
620+ assertSame (actualManifest , expectedManifest )
621+ })
622+
623+ It ("should properly generate webhook definition with matchExpressions in selectors" , func () {
624+ By ("switching into testdata to appease go modules" )
625+ cwd , err := os .Getwd ()
626+ Expect (err ).NotTo (HaveOccurred ())
627+ Expect (os .Chdir ("./testdata/valid-selectors-matchexpressions" )).To (Succeed ())
628+ defer func () { Expect (os .Chdir (cwd )).To (Succeed ()) }()
629+
630+ By ("loading the roots" )
631+ pkgs , err := loader .LoadRoots ("." )
632+ Expect (err ).NotTo (HaveOccurred ())
633+ Expect (pkgs ).To (HaveLen (1 ))
634+
635+ By ("setting up the parser" )
636+ reg := & markers.Registry {}
637+ Expect (reg .Register (webhook .ConfigDefinition )).To (Succeed ())
638+ Expect (reg .Register (webhook .WebhookConfigDefinition )).To (Succeed ())
639+
640+ By ("requesting that the manifest be generated" )
641+ outputDir , err := os .MkdirTemp ("" , "webhook-integration-test" )
642+ Expect (err ).NotTo (HaveOccurred ())
643+ defer os .RemoveAll (outputDir )
644+ genCtx := & genall.GenerationContext {
645+ Collector : & markers.Collector {Registry : reg },
646+ Roots : pkgs ,
647+ OutputRule : genall .OutputToDirectory (outputDir ),
648+ }
649+ Expect (webhook.Generator {}.Generate (genCtx )).To (Succeed ())
650+ for _ , r := range genCtx .Roots {
651+ Expect (r .Errors ).To (HaveLen (0 ))
652+ }
653+
654+ By ("loading the generated v1 YAML" )
655+ actualFile , err := os .ReadFile (path .Join (outputDir , "manifests.yaml" ))
656+ Expect (err ).NotTo (HaveOccurred ())
657+ actualMutating , actualValidating := unmarshalBothV1 (actualFile )
658+
659+ By ("loading the desired v1 YAML" )
660+ expectedFile , err := os .ReadFile ("manifests.yaml" )
661+ Expect (err ).NotTo (HaveOccurred ())
662+ expectedMutating , expectedValidating := unmarshalBothV1 (expectedFile )
663+
664+ By ("comparing the two" )
665+ assertSame (actualMutating , expectedMutating )
666+ assertSame (actualValidating , expectedValidating )
667+ })
668+
529669})
530670
531671func unmarshalBothV1 (in []byte ) (mutating admissionregv1.MutatingWebhookConfiguration , validating admissionregv1.ValidatingWebhookConfiguration ) {
0 commit comments