@@ -77,7 +77,7 @@ func runTests(admissionReviewVersion string) {
77
77
close (stop )
78
78
})
79
79
80
- It ("should scaffold a custom defaulting webhook if the type implements the CustomDefaulter interface " , func () {
80
+ It ("should scaffold a custom defaulting webhook" , func () {
81
81
By ("creating a controller manager" )
82
82
m , err := manager .New (cfg , manager.Options {})
83
83
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
@@ -91,6 +91,9 @@ func runTests(admissionReviewVersion string) {
91
91
err = WebhookManagedBy (m ).
92
92
For (& TestDefaulter {}).
93
93
WithDefaulter (& TestCustomDefaulter {}).
94
+ WithLogConstructor (func (base logr.Logger , req * admission.Request ) logr.Logger {
95
+ return admission .DefaultLogConstructor (testingLogger , req )
96
+ }).
94
97
Complete ()
95
98
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
96
99
svr := m .GetWebhookServer ()
@@ -100,16 +103,17 @@ func runTests(admissionReviewVersion string) {
100
103
"request":{
101
104
"uid":"07e52e8d-4513-11e9-a716-42010a800270",
102
105
"kind":{
103
- "group":"",
106
+ "group":"foo.test.org ",
104
107
"version":"v1",
105
108
"kind":"TestDefaulter"
106
109
},
107
110
"resource":{
108
- "group":"",
111
+ "group":"foo.test.org ",
109
112
"version":"v1",
110
113
"resource":"testdefaulter"
111
114
},
112
115
"namespace":"default",
116
+ "name":"foo",
113
117
"operation":"CREATE",
114
118
"object":{
115
119
"replica":1
@@ -136,6 +140,7 @@ func runTests(admissionReviewVersion string) {
136
140
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":true` ))
137
141
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"patch":` ))
138
142
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":200` ))
143
+ EventuallyWithOffset (1 , logBuffer ).Should (gbytes .Say (`"msg":"Defaulting object","object":{"name":"foo","namespace":"default"},"namespace":"default","name":"foo","resource":{"group":"foo.test.org","version":"v1","resource":"testdefaulter"},"user":"","requestID":"07e52e8d-4513-11e9-a716-42010a800270"` ))
139
144
140
145
By ("sending a request to a validating webhook path that doesn't exist" )
141
146
path = generateValidatePath (testDefaulterGVK )
@@ -212,83 +217,7 @@ func runTests(admissionReviewVersion string) {
212
217
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"message":"panic: fake panic test [recovered]` ))
213
218
})
214
219
215
- It ("should scaffold a defaulting webhook with a custom defaulter" , func () {
216
- By ("creating a controller manager" )
217
- m , err := manager .New (cfg , manager.Options {})
218
- ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
219
-
220
- By ("registering the type in the Scheme" )
221
- builder := scheme.Builder {GroupVersion : testDefaulterGVK .GroupVersion ()}
222
- builder .Register (& TestDefaulter {}, & TestDefaulterList {})
223
- err = builder .AddToScheme (m .GetScheme ())
224
- ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
225
-
226
- err = WebhookManagedBy (m ).
227
- WithDefaulter (& TestCustomDefaulter {}).
228
- For (& TestDefaulter {}).
229
- WithLogConstructor (func (base logr.Logger , req * admission.Request ) logr.Logger {
230
- return admission .DefaultLogConstructor (testingLogger , req )
231
- }).
232
- Complete ()
233
- ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
234
- svr := m .GetWebhookServer ()
235
- ExpectWithOffset (1 , svr ).NotTo (BeNil ())
236
-
237
- reader := strings .NewReader (admissionReviewGV + admissionReviewVersion + `",
238
- "request":{
239
- "uid":"07e52e8d-4513-11e9-a716-42010a800270",
240
- "kind":{
241
- "group":"foo.test.org",
242
- "version":"v1",
243
- "kind":"TestDefaulter"
244
- },
245
- "resource":{
246
- "group":"foo.test.org",
247
- "version":"v1",
248
- "resource":"testdefaulter"
249
- },
250
- "namespace":"default",
251
- "name":"foo",
252
- "operation":"CREATE",
253
- "object":{
254
- "replica":1
255
- },
256
- "oldObject":null
257
- }
258
- }` )
259
-
260
- ctx , cancel := context .WithCancel (context .Background ())
261
- cancel ()
262
- err = svr .Start (ctx )
263
- if err != nil && ! os .IsNotExist (err ) {
264
- ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
265
- }
266
-
267
- By ("sending a request to a mutating webhook path" )
268
- path := generateMutatePath (testDefaulterGVK )
269
- req := httptest .NewRequest ("POST" , svcBaseAddr + path , reader )
270
- req .Header .Add ("Content-Type" , "application/json" )
271
- w := httptest .NewRecorder ()
272
- svr .WebhookMux ().ServeHTTP (w , req )
273
- ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusOK ))
274
- By ("sanity checking the response contains reasonable fields" )
275
- ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":true` ))
276
- ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"patch":` ))
277
- ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":200` ))
278
- EventuallyWithOffset (1 , logBuffer ).Should (gbytes .Say (`"msg":"Defaulting object","object":{"name":"foo","namespace":"default"},"namespace":"default","name":"foo","resource":{"group":"foo.test.org","version":"v1","resource":"testdefaulter"},"user":"","requestID":"07e52e8d-4513-11e9-a716-42010a800270"` ))
279
-
280
- By ("sending a request to a validating webhook path that doesn't exist" )
281
- path = generateValidatePath (testDefaulterGVK )
282
- _ , err = reader .Seek (0 , 0 )
283
- ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
284
- req = httptest .NewRequest ("POST" , svcBaseAddr + path , reader )
285
- req .Header .Add ("Content-Type" , "application/json" )
286
- w = httptest .NewRecorder ()
287
- svr .WebhookMux ().ServeHTTP (w , req )
288
- ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusNotFound ))
289
- })
290
-
291
- It ("should scaffold a custom validating webhook if the type implements the CustomValidator interface" , func () {
220
+ It ("should scaffold a custom validating webhook" , func () {
292
221
By ("creating a controller manager" )
293
222
m , err := manager .New (cfg , manager.Options {})
294
223
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
@@ -302,6 +231,9 @@ func runTests(admissionReviewVersion string) {
302
231
err = WebhookManagedBy (m ).
303
232
For (& TestValidator {}).
304
233
WithValidator (& TestCustomValidator {}).
234
+ WithLogConstructor (func (base logr.Logger , req * admission.Request ) logr.Logger {
235
+ return admission .DefaultLogConstructor (testingLogger , req )
236
+ }).
305
237
Complete ()
306
238
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
307
239
svr := m .GetWebhookServer ()
@@ -311,16 +243,17 @@ func runTests(admissionReviewVersion string) {
311
243
"request":{
312
244
"uid":"07e52e8d-4513-11e9-a716-42010a800270",
313
245
"kind":{
314
- "group":"",
246
+ "group":"foo.test.org ",
315
247
"version":"v1",
316
248
"kind":"TestValidator"
317
249
},
318
250
"resource":{
319
- "group":"",
251
+ "group":"foo.test.org ",
320
252
"version":"v1",
321
253
"resource":"testvalidator"
322
254
},
323
255
"namespace":"default",
256
+ "name":"foo",
324
257
"operation":"UPDATE",
325
258
"object":{
326
259
"replica":1
@@ -358,6 +291,7 @@ func runTests(admissionReviewVersion string) {
358
291
By ("sanity checking the response contains reasonable field" )
359
292
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":false` ))
360
293
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":403` ))
294
+ 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"` ))
361
295
})
362
296
363
297
It ("should scaffold a custom validating webhook which recovers from panics" , func () {
@@ -424,84 +358,7 @@ func runTests(admissionReviewVersion string) {
424
358
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"message":"panic: fake panic test [recovered]` ))
425
359
})
426
360
427
- It ("should scaffold a validating webhook with a custom validator" , func () {
428
- By ("creating a controller manager" )
429
- m , err := manager .New (cfg , manager.Options {})
430
- ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
431
-
432
- By ("registering the type in the Scheme" )
433
- builder := scheme.Builder {GroupVersion : testValidatorGVK .GroupVersion ()}
434
- builder .Register (& TestValidator {}, & TestValidatorList {})
435
- err = builder .AddToScheme (m .GetScheme ())
436
- ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
437
-
438
- err = WebhookManagedBy (m ).
439
- WithValidator (& TestCustomValidator {}).
440
- For (& TestValidator {}).
441
- WithLogConstructor (func (base logr.Logger , req * admission.Request ) logr.Logger {
442
- return admission .DefaultLogConstructor (testingLogger , req )
443
- }).
444
- Complete ()
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"` ))
502
- })
503
-
504
- It ("should scaffold a custom validating webhook if the type implements the CustomValidator interface to validate deletes" , func () {
361
+ It ("should scaffold a custom validating webhook to validate deletes" , func () {
505
362
By ("creating a controller manager" )
506
363
ctx , cancel := context .WithCancel (context .Background ())
507
364
0 commit comments