@@ -56,6 +56,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition",
5656 By ("setting up the parser" )
5757 reg := & markers.Registry {}
5858 Expect (reg .Register (webhook .ConfigDefinition )).To (Succeed ())
59+ Expect (reg .Register (webhook .WebhookConfigDefinition )).To (Succeed ())
5960
6061 By ("requesting that the manifest be generated" )
6162 outputDir , err := os .MkdirTemp ("" , "webhook-integration-test" )
@@ -85,6 +86,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition",
8586 By ("setting up the parser" )
8687 reg := & markers.Registry {}
8788 Expect (reg .Register (webhook .ConfigDefinition )).To (Succeed ())
89+ Expect (reg .Register (webhook .WebhookConfigDefinition )).To (Succeed ())
8890
8991 By ("requesting that the manifest be generated" )
9092 outputDir , err := os .MkdirTemp ("" , "webhook-integration-test" )
@@ -116,6 +118,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition",
116118 By ("setting up the parser" )
117119 reg := & markers.Registry {}
118120 Expect (reg .Register (webhook .ConfigDefinition )).To (Succeed ())
121+ Expect (reg .Register (webhook .WebhookConfigDefinition )).To (Succeed ())
119122
120123 By ("requesting that the manifest be generated" )
121124 outputDir , err := os .MkdirTemp ("" , "webhook-integration-test" )
@@ -145,6 +148,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition",
145148 By ("setting up the parser" )
146149 reg := & markers.Registry {}
147150 Expect (reg .Register (webhook .ConfigDefinition )).To (Succeed ())
151+ Expect (reg .Register (webhook .WebhookConfigDefinition )).To (Succeed ())
148152
149153 By ("requesting that the manifest be generated" )
150154 outputDir , err := os .MkdirTemp ("" , "webhook-integration-test" )
@@ -174,6 +178,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition",
174178 By ("setting up the parser" )
175179 reg := & markers.Registry {}
176180 Expect (reg .Register (webhook .ConfigDefinition )).To (Succeed ())
181+ Expect (reg .Register (webhook .WebhookConfigDefinition )).To (Succeed ())
177182
178183 By ("requesting that the manifest be generated" )
179184 outputDir , err := os .MkdirTemp ("" , "webhook-integration-test" )
@@ -219,6 +224,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition",
219224 By ("setting up the parser" )
220225 reg := & markers.Registry {}
221226 Expect (reg .Register (webhook .ConfigDefinition )).To (Succeed ())
227+ Expect (reg .Register (webhook .WebhookConfigDefinition )).To (Succeed ())
222228
223229 By ("requesting that the manifest be generated" )
224230 outputDir , err := os .MkdirTemp ("" , "webhook-integration-test" )
@@ -268,6 +274,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition",
268274 By ("setting up the parser" )
269275 reg := & markers.Registry {}
270276 Expect (reg .Register (webhook .ConfigDefinition )).To (Succeed ())
277+ Expect (reg .Register (webhook .WebhookConfigDefinition )).To (Succeed ())
271278
272279 By ("requesting that the manifest be generated" )
273280 outputDir , err := os .MkdirTemp ("" , "webhook-integration-test" )
@@ -313,6 +320,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition",
313320 By ("setting up the parser" )
314321 reg := & markers.Registry {}
315322 Expect (reg .Register (webhook .ConfigDefinition )).To (Succeed ())
323+ Expect (reg .Register (webhook .WebhookConfigDefinition )).To (Succeed ())
316324
317325 By ("requesting that the manifest be generated" )
318326 outputDir , err := os .MkdirTemp ("" , "webhook-integration-test" )
@@ -326,6 +334,83 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition",
326334 err = webhook.Generator {}.Generate (genCtx )
327335 Expect (err ).To (HaveOccurred ())
328336 })
337+
338+ It ("should properly generate the webhook definition when a name is specified with the `kubebuilder:webhookconfiguration` marker" , func () {
339+ By ("switching into testdata to appease go modules" )
340+ cwd , err := os .Getwd ()
341+ Expect (err ).NotTo (HaveOccurred ())
342+ Expect (os .Chdir ("./testdata/valid-custom-name" )).To (Succeed ()) // go modules are directory-sensitive
343+ defer func () { Expect (os .Chdir (cwd )).To (Succeed ()) }()
344+
345+ By ("loading the roots" )
346+ pkgs , err := loader .LoadRoots ("." )
347+ Expect (err ).NotTo (HaveOccurred ())
348+ Expect (pkgs ).To (HaveLen (1 ))
349+
350+ By ("setting up the parser" )
351+ reg := & markers.Registry {}
352+ Expect (reg .Register (webhook .ConfigDefinition )).To (Succeed ())
353+ Expect (reg .Register (webhook .WebhookConfigDefinition )).To (Succeed ())
354+
355+ By ("requesting that the manifest be generated" )
356+ outputDir , err := os .MkdirTemp ("" , "webhook-integration-test" )
357+ Expect (err ).NotTo (HaveOccurred ())
358+ defer os .RemoveAll (outputDir )
359+ genCtx := & genall.GenerationContext {
360+ Collector : & markers.Collector {Registry : reg },
361+ Roots : pkgs ,
362+ OutputRule : genall .OutputToDirectory (outputDir ),
363+ }
364+ Expect (webhook.Generator {}.Generate (genCtx )).To (Succeed ())
365+ for _ , r := range genCtx .Roots {
366+ Expect (r .Errors ).To (HaveLen (0 ))
367+ }
368+
369+ By ("loading the generated v1 YAML" )
370+ actualFile , err := os .ReadFile (path .Join (outputDir , "manifests.yaml" ))
371+ Expect (err ).NotTo (HaveOccurred ())
372+ actualMutating , actualValidating := unmarshalBothV1 (actualFile )
373+
374+ By ("loading the desired v1 YAML" )
375+ expectedFile , err := os .ReadFile ("manifests.yaml" )
376+ Expect (err ).NotTo (HaveOccurred ())
377+ expectedMutating , expectedValidating := unmarshalBothV1 (expectedFile )
378+
379+ By ("comparing the two" )
380+ assertSame (actualMutating , expectedMutating )
381+ assertSame (actualValidating , expectedValidating )
382+ })
383+
384+ It ("should fail to generate when there are multiple `kubebuilder:webhookconfiguration` markers of the same mutation type" , func () {
385+ By ("switching into testdata to appease go modules" )
386+ cwd , err := os .Getwd ()
387+ Expect (err ).NotTo (HaveOccurred ())
388+ Expect (os .Chdir ("./testdata/invalid-multiple-webhookconfigurations" )).To (Succeed ()) // go modules are directory-sensitive
389+ defer func () { Expect (os .Chdir (cwd )).To (Succeed ()) }()
390+
391+ By ("loading the roots" )
392+ pkgs , err := loader .LoadRoots ("." )
393+ Expect (err ).NotTo (HaveOccurred ())
394+ Expect (pkgs ).To (HaveLen (1 ))
395+
396+ By ("setting up the parser" )
397+ reg := & markers.Registry {}
398+ Expect (reg .Register (webhook .ConfigDefinition )).To (Succeed ())
399+ Expect (reg .Register (webhook .WebhookConfigDefinition )).To (Succeed ())
400+
401+ By ("requesting that the manifest be generated" )
402+ outputDir , err := os .MkdirTemp ("" , "webhook-integration-test" )
403+ Expect (err ).NotTo (HaveOccurred ())
404+ defer os .RemoveAll (outputDir )
405+ genCtx := & genall.GenerationContext {
406+ Collector : & markers.Collector {Registry : reg },
407+ Roots : pkgs ,
408+ OutputRule : genall .OutputToDirectory (outputDir ),
409+ }
410+ err = webhook.Generator {}.Generate (genCtx )
411+ Expect (err ).To (HaveOccurred ())
412+ })
413+
329414})
330415
331416func unmarshalBothV1 (in []byte ) (mutating admissionregv1.MutatingWebhookConfiguration , validating admissionregv1.ValidatingWebhookConfiguration ) {
0 commit comments