@@ -77,7 +77,78 @@ func runTests(admissionReviewVersion string) {
77
77
close (stop )
78
78
})
79
79
80
- It ("should scaffold a custom defaulter webhook which recovers from panics" , func () {
80
+ It ("should scaffold a custom defaulting webhook if the type implements the CustomDefaulter interface" , func () {
81
+ By ("creating a controller manager" )
82
+ m , err := manager .New (cfg , manager.Options {})
83
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
84
+
85
+ By ("registering the type in the Scheme" )
86
+ builder := scheme.Builder {GroupVersion : testDefaulterGVK .GroupVersion ()}
87
+ builder .Register (& TestDefaulter {}, & TestDefaulterList {})
88
+ err = builder .AddToScheme (m .GetScheme ())
89
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
90
+
91
+ err = WebhookManagedBy (m ).
92
+ For (& TestDefaulter {}).
93
+ WithDefaulter (& TestCustomDefaulter {}).
94
+ Complete ()
95
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
96
+ svr := m .GetWebhookServer ()
97
+ ExpectWithOffset (1 , svr ).NotTo (BeNil ())
98
+
99
+ reader := strings .NewReader (admissionReviewGV + admissionReviewVersion + `",
100
+ "request":{
101
+ "uid":"07e52e8d-4513-11e9-a716-42010a800270",
102
+ "kind":{
103
+ "group":"",
104
+ "version":"v1",
105
+ "kind":"TestDefaulter"
106
+ },
107
+ "resource":{
108
+ "group":"",
109
+ "version":"v1",
110
+ "resource":"testdefaulter"
111
+ },
112
+ "namespace":"default",
113
+ "operation":"CREATE",
114
+ "object":{
115
+ "replica":1
116
+ },
117
+ "oldObject":null
118
+ }
119
+ }` )
120
+
121
+ ctx , cancel := context .WithCancel (context .Background ())
122
+ cancel ()
123
+ err = svr .Start (ctx )
124
+ if err != nil && ! os .IsNotExist (err ) {
125
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
126
+ }
127
+
128
+ By ("sending a request to a mutating webhook path" )
129
+ path := generateMutatePath (testDefaulterGVK )
130
+ req := httptest .NewRequest ("POST" , svcBaseAddr + path , reader )
131
+ req .Header .Add ("Content-Type" , "application/json" )
132
+ w := httptest .NewRecorder ()
133
+ svr .WebhookMux ().ServeHTTP (w , req )
134
+ ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusOK ))
135
+ By ("sanity checking the response contains reasonable fields" )
136
+ ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":true` ))
137
+ ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"patch":` ))
138
+ ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":200` ))
139
+
140
+ By ("sending a request to a validating webhook path that doesn't exist" )
141
+ path = generateValidatePath (testDefaulterGVK )
142
+ _ , err = reader .Seek (0 , 0 )
143
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
144
+ req = httptest .NewRequest ("POST" , svcBaseAddr + path , reader )
145
+ req .Header .Add ("Content-Type" , "application/json" )
146
+ w = httptest .NewRecorder ()
147
+ svr .WebhookMux ().ServeHTTP (w , req )
148
+ ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusNotFound ))
149
+ })
150
+
151
+ It ("should scaffold a custom defaulting webhook which recovers from panics" , func () {
81
152
By ("creating a controller manager" )
82
153
m , err := manager .New (cfg , manager.Options {})
83
154
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
@@ -91,7 +162,6 @@ func runTests(admissionReviewVersion string) {
91
162
err = WebhookManagedBy (m ).
92
163
For (& TestDefaulter {}).
93
164
WithDefaulter (& TestCustomDefaulter {}).
94
- RecoverPanic (true ).
95
165
// RecoverPanic defaults to true.
96
166
Complete ()
97
167
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
@@ -114,6 +184,7 @@ func runTests(admissionReviewVersion string) {
114
184
"namespace":"default",
115
185
"operation":"CREATE",
116
186
"object":{
187
+ "replica":1,
117
188
"panic":true
118
189
},
119
190
"oldObject":null
@@ -216,7 +287,7 @@ func runTests(admissionReviewVersion string) {
216
287
ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusNotFound ))
217
288
})
218
289
219
- It ("should scaffold a validating webhook with a custom validator " , func () {
290
+ It ("should scaffold a custom validating webhook if the type implements the CustomValidator interface " , func () {
220
291
By ("creating a controller manager" )
221
292
m , err := manager .New (cfg , manager.Options {})
222
293
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
@@ -228,11 +299,8 @@ func runTests(admissionReviewVersion string) {
228
299
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
229
300
230
301
err = WebhookManagedBy (m ).
231
- WithValidator (& TestCustomValidator {}).
232
302
For (& TestValidator {}).
233
- WithLogConstructor (func (base logr.Logger , req * admission.Request ) logr.Logger {
234
- return admission .DefaultLogConstructor (testingLogger , req )
235
- }).
303
+ WithValidator (& TestCustomValidator {}).
236
304
Complete ()
237
305
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
238
306
svr := m .GetWebhookServer ()
@@ -242,17 +310,16 @@ func runTests(admissionReviewVersion string) {
242
310
"request":{
243
311
"uid":"07e52e8d-4513-11e9-a716-42010a800270",
244
312
"kind":{
245
- "group":"foo.test.org ",
313
+ "group":"",
246
314
"version":"v1",
247
315
"kind":"TestValidator"
248
316
},
249
317
"resource":{
250
- "group":"foo.test.org ",
318
+ "group":"",
251
319
"version":"v1",
252
320
"resource":"testvalidator"
253
321
},
254
322
"namespace":"default",
255
- "name":"foo",
256
323
"operation":"UPDATE",
257
324
"object":{
258
325
"replica":1
@@ -290,13 +357,11 @@ func runTests(admissionReviewVersion string) {
290
357
By ("sanity checking the response contains reasonable field" )
291
358
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":false` ))
292
359
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":403` ))
293
- EventuallyWithOffset (1 , logBuffer ).Should (gbytes .Say (`"msg":"Validating object","object":{"name":"foo","namespace":"default"},"namespace":"default","name":"foo","resource":{"group":"foo.test.org","version":"v1","resource":"testvalidator"},"user":"","requestID":"07e52e8d-4513-11e9-a716-42010a800270"` ))
294
360
})
295
361
296
- It ("should scaffold a customValidator webhook which recovers from panics" , func () {
362
+ It ("should scaffold a custom validating webhook which recovers from panics" , func () {
297
363
By ("creating a controller manager" )
298
364
m , err := manager .New (cfg , manager.Options {})
299
-
300
365
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
301
366
302
367
By ("registering the type in the Scheme" )
@@ -306,7 +371,7 @@ func runTests(admissionReviewVersion string) {
306
371
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
307
372
308
373
err = WebhookManagedBy (m ).
309
- For (& TestValidator {Panic : true }).
374
+ For (& TestValidator {}).
310
375
WithValidator (& TestCustomValidator {}).
311
376
RecoverPanic (true ).
312
377
Complete ()
@@ -322,16 +387,13 @@ func runTests(admissionReviewVersion string) {
322
387
"version":"v1",
323
388
"kind":"TestValidator"
324
389
},
325
-
326
390
"resource":{
327
391
"group":"",
328
-
329
392
"version":"v1",
330
393
"resource":"testvalidator"
331
394
},
332
395
"namespace":"default",
333
396
"operation":"CREATE",
334
-
335
397
"object":{
336
398
"replica":2,
337
399
"panic":true
@@ -361,25 +423,84 @@ func runTests(admissionReviewVersion string) {
361
423
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"message":"panic: fake panic test [recovered]` ))
362
424
})
363
425
364
- It ("should send an error when trying to register a webhook with more than one For " , func () {
426
+ It ("should scaffold a validating webhook with a custom validator " , func () {
365
427
By ("creating a controller manager" )
366
428
m , err := manager .New (cfg , manager.Options {})
367
429
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
368
430
369
431
By ("registering the type in the Scheme" )
370
- builder := scheme.Builder {GroupVersion : testDefaulterGVK .GroupVersion ()}
371
- builder .Register (& TestDefaulter {}, & TestDefaulterList {})
432
+ builder := scheme.Builder {GroupVersion : testValidatorGVK .GroupVersion ()}
433
+ builder .Register (& TestValidator {}, & TestValidatorList {})
372
434
err = builder .AddToScheme (m .GetScheme ())
373
435
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
374
436
375
437
err = WebhookManagedBy (m ).
376
- For (& TestDefaulter {}).
377
- For (& TestDefaulter {}).
438
+ WithValidator (& TestCustomValidator {}).
439
+ For (& TestValidator {}).
440
+ WithLogConstructor (func (base logr.Logger , req * admission.Request ) logr.Logger {
441
+ return admission .DefaultLogConstructor (testingLogger , req )
442
+ }).
378
443
Complete ()
379
- Expect (err ).To (HaveOccurred ())
444
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
445
+ svr := m .GetWebhookServer ()
446
+ ExpectWithOffset (1 , svr ).NotTo (BeNil ())
447
+
448
+ reader := strings .NewReader (admissionReviewGV + admissionReviewVersion + `",
449
+ "request":{
450
+ "uid":"07e52e8d-4513-11e9-a716-42010a800270",
451
+ "kind":{
452
+ "group":"foo.test.org",
453
+ "version":"v1",
454
+ "kind":"TestValidator"
455
+ },
456
+ "resource":{
457
+ "group":"foo.test.org",
458
+ "version":"v1",
459
+ "resource":"testvalidator"
460
+ },
461
+ "namespace":"default",
462
+ "name":"foo",
463
+ "operation":"UPDATE",
464
+ "object":{
465
+ "replica":1
466
+ },
467
+ "oldObject":{
468
+ "replica":2
469
+ }
470
+ }
471
+ }` )
472
+
473
+ ctx , cancel := context .WithCancel (context .Background ())
474
+ cancel ()
475
+ err = svr .Start (ctx )
476
+ if err != nil && ! os .IsNotExist (err ) {
477
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
478
+ }
479
+
480
+ By ("sending a request to a mutating webhook path that doesn't exist" )
481
+ path := generateMutatePath (testValidatorGVK )
482
+ req := httptest .NewRequest ("POST" , svcBaseAddr + path , reader )
483
+ req .Header .Add ("Content-Type" , "application/json" )
484
+ w := httptest .NewRecorder ()
485
+ svr .WebhookMux ().ServeHTTP (w , req )
486
+ ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusNotFound ))
487
+
488
+ By ("sending a request to a validating webhook path" )
489
+ path = generateValidatePath (testValidatorGVK )
490
+ _ , err = reader .Seek (0 , 0 )
491
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
492
+ req = httptest .NewRequest ("POST" , svcBaseAddr + path , reader )
493
+ req .Header .Add ("Content-Type" , "application/json" )
494
+ w = httptest .NewRecorder ()
495
+ svr .WebhookMux ().ServeHTTP (w , req )
496
+ ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusOK ))
497
+ By ("sanity checking the response contains reasonable field" )
498
+ ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":false` ))
499
+ ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":403` ))
500
+ EventuallyWithOffset (1 , logBuffer ).Should (gbytes .Say (`"msg":"Validating object","object":{"name":"foo","namespace":"default"},"namespace":"default","name":"foo","resource":{"group":"foo.test.org","version":"v1","resource":"testvalidator"},"user":"","requestID":"07e52e8d-4513-11e9-a716-42010a800270"` ))
380
501
})
381
502
382
- It ("should scaffold a custom validator webhook if the type implements the CustomValidator interface to validate deletes" , func () {
503
+ It ("should scaffold a custom validating webhook if the type implements the CustomValidator interface to validate deletes" , func () {
383
504
By ("creating a controller manager" )
384
505
ctx , cancel := context .WithCancel (context .Background ())
385
506
@@ -407,7 +528,6 @@ func runTests(admissionReviewVersion string) {
407
528
"group":"",
408
529
"version":"v1",
409
530
"kind":"TestValidator"
410
-
411
531
},
412
532
"resource":{
413
533
"group":"",
@@ -443,14 +563,12 @@ func runTests(admissionReviewVersion string) {
443
563
reader = strings .NewReader (admissionReviewGV + admissionReviewVersion + `",
444
564
"request":{
445
565
"uid":"07e52e8d-4513-11e9-a716-42010a800270",
446
-
447
566
"kind":{
448
567
"group":"",
449
568
"version":"v1",
450
569
"kind":"TestValidator"
451
570
},
452
571
"resource":{
453
-
454
572
"group":"",
455
573
"version":"v1",
456
574
"resource":"testvalidator"
@@ -474,6 +592,24 @@ func runTests(admissionReviewVersion string) {
474
592
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":true` ))
475
593
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":200` ))
476
594
})
595
+
596
+ It ("should send an error when trying to register a webhook with more than one For" , func () {
597
+ By ("creating a controller manager" )
598
+ m , err := manager .New (cfg , manager.Options {})
599
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
600
+
601
+ By ("registering the type in the Scheme" )
602
+ builder := scheme.Builder {GroupVersion : testDefaulterGVK .GroupVersion ()}
603
+ builder .Register (& TestDefaulter {}, & TestDefaulterList {})
604
+ err = builder .AddToScheme (m .GetScheme ())
605
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
606
+
607
+ err = WebhookManagedBy (m ).
608
+ For (& TestDefaulter {}).
609
+ For (& TestDefaulter {}).
610
+ Complete ()
611
+ Expect (err ).To (HaveOccurred ())
612
+ })
477
613
}
478
614
479
615
// TestDefaulter.
@@ -586,13 +722,13 @@ func (*TestCustomDefaulter) Default(ctx context.Context, obj runtime.Object) err
586
722
}
587
723
588
724
d := obj .(* TestDefaulter ) //nolint:ifshort
725
+ if d .Panic {
726
+ panic ("fake panic test" )
727
+ }
589
728
590
729
if d .Replica < 2 {
591
730
d .Replica = 2
592
731
}
593
- if d .Panic {
594
- panic ("fake panic test" )
595
- }
596
732
return nil
597
733
}
598
734
0 commit comments