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