@@ -56,6 +56,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition",
56
56
By ("setting up the parser" )
57
57
reg := & markers.Registry {}
58
58
Expect (reg .Register (webhook .ConfigDefinition )).To (Succeed ())
59
+ Expect (reg .Register (webhook .WebhookConfigDefinition )).To (Succeed ())
59
60
60
61
By ("requesting that the manifest be generated" )
61
62
outputDir , err := os .MkdirTemp ("" , "webhook-integration-test" )
@@ -85,6 +86,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition",
85
86
By ("setting up the parser" )
86
87
reg := & markers.Registry {}
87
88
Expect (reg .Register (webhook .ConfigDefinition )).To (Succeed ())
89
+ Expect (reg .Register (webhook .WebhookConfigDefinition )).To (Succeed ())
88
90
89
91
By ("requesting that the manifest be generated" )
90
92
outputDir , err := os .MkdirTemp ("" , "webhook-integration-test" )
@@ -116,6 +118,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition",
116
118
By ("setting up the parser" )
117
119
reg := & markers.Registry {}
118
120
Expect (reg .Register (webhook .ConfigDefinition )).To (Succeed ())
121
+ Expect (reg .Register (webhook .WebhookConfigDefinition )).To (Succeed ())
119
122
120
123
By ("requesting that the manifest be generated" )
121
124
outputDir , err := os .MkdirTemp ("" , "webhook-integration-test" )
@@ -145,6 +148,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition",
145
148
By ("setting up the parser" )
146
149
reg := & markers.Registry {}
147
150
Expect (reg .Register (webhook .ConfigDefinition )).To (Succeed ())
151
+ Expect (reg .Register (webhook .WebhookConfigDefinition )).To (Succeed ())
148
152
149
153
By ("requesting that the manifest be generated" )
150
154
outputDir , err := os .MkdirTemp ("" , "webhook-integration-test" )
@@ -174,6 +178,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition",
174
178
By ("setting up the parser" )
175
179
reg := & markers.Registry {}
176
180
Expect (reg .Register (webhook .ConfigDefinition )).To (Succeed ())
181
+ Expect (reg .Register (webhook .WebhookConfigDefinition )).To (Succeed ())
177
182
178
183
By ("requesting that the manifest be generated" )
179
184
outputDir , err := os .MkdirTemp ("" , "webhook-integration-test" )
@@ -219,6 +224,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition",
219
224
By ("setting up the parser" )
220
225
reg := & markers.Registry {}
221
226
Expect (reg .Register (webhook .ConfigDefinition )).To (Succeed ())
227
+ Expect (reg .Register (webhook .WebhookConfigDefinition )).To (Succeed ())
222
228
223
229
By ("requesting that the manifest be generated" )
224
230
outputDir , err := os .MkdirTemp ("" , "webhook-integration-test" )
@@ -268,6 +274,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition",
268
274
By ("setting up the parser" )
269
275
reg := & markers.Registry {}
270
276
Expect (reg .Register (webhook .ConfigDefinition )).To (Succeed ())
277
+ Expect (reg .Register (webhook .WebhookConfigDefinition )).To (Succeed ())
271
278
272
279
By ("requesting that the manifest be generated" )
273
280
outputDir , err := os .MkdirTemp ("" , "webhook-integration-test" )
@@ -313,6 +320,7 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition",
313
320
By ("setting up the parser" )
314
321
reg := & markers.Registry {}
315
322
Expect (reg .Register (webhook .ConfigDefinition )).To (Succeed ())
323
+ Expect (reg .Register (webhook .WebhookConfigDefinition )).To (Succeed ())
316
324
317
325
By ("requesting that the manifest be generated" )
318
326
outputDir , err := os .MkdirTemp ("" , "webhook-integration-test" )
@@ -326,6 +334,83 @@ var _ = Describe("Webhook Generation From Parsing to CustomResourceDefinition",
326
334
err = webhook.Generator {}.Generate (genCtx )
327
335
Expect (err ).To (HaveOccurred ())
328
336
})
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
+
329
414
})
330
415
331
416
func unmarshalBothV1 (in []byte ) (mutating admissionregv1.MutatingWebhookConfiguration , validating admissionregv1.ValidatingWebhookConfiguration ) {
0 commit comments