@@ -269,6 +269,103 @@ var _ = Describe("application", func() {
269
269
Expect (w .Body ).To (ContainSubstring (`"allowed":true` ))
270
270
Expect (w .Body ).To (ContainSubstring (`"code":200` ))
271
271
})
272
+
273
+ It ("should scaffold a validating webhook if the type implements the Validator interface to validate deletes" , func () {
274
+ By ("creating a controller manager" )
275
+ m , err := manager .New (cfg , manager.Options {})
276
+ Expect (err ).NotTo (HaveOccurred ())
277
+
278
+ By ("registering the type in the Scheme" )
279
+ builder := scheme.Builder {GroupVersion : testValidatorGVK .GroupVersion ()}
280
+ builder .Register (& TestValidator {}, & TestValidatorList {})
281
+ err = builder .AddToScheme (m .GetScheme ())
282
+ Expect (err ).NotTo (HaveOccurred ())
283
+
284
+ err = WebhookManagedBy (m ).
285
+ For (& TestValidator {}).
286
+ Complete ()
287
+ Expect (err ).NotTo (HaveOccurred ())
288
+ svr := m .GetWebhookServer ()
289
+ Expect (svr ).NotTo (BeNil ())
290
+
291
+ reader := strings .NewReader (`{
292
+ "kind":"AdmissionReview",
293
+ "apiVersion":"admission.k8s.io/v1beta1",
294
+ "request":{
295
+ "uid":"07e52e8d-4513-11e9-a716-42010a800270",
296
+ "kind":{
297
+ "group":"",
298
+ "version":"v1",
299
+ "kind":"TestValidator"
300
+ },
301
+ "resource":{
302
+ "group":"",
303
+ "version":"v1",
304
+ "resource":"testvalidator"
305
+ },
306
+ "namespace":"default",
307
+ "operation":"DELETE",
308
+ "object":null,
309
+ "oldObject":{
310
+ "replica":1
311
+ }
312
+ }
313
+ }` )
314
+ stopCh := make (chan struct {})
315
+ close (stopCh )
316
+ // TODO: we may want to improve it to make it be able to inject dependencies,
317
+ // but not always try to load certs and return not found error.
318
+ err = svr .Start (stopCh )
319
+ if err != nil && ! os .IsNotExist (err ) {
320
+ Expect (err ).NotTo (HaveOccurred ())
321
+ }
322
+
323
+ By ("sending a request to a validating webhook path to check for failed delete" )
324
+ path := generateValidatePath (testValidatorGVK )
325
+ req := httptest .NewRequest ("POST" , "http://svc-name.svc-ns.svc" + path , reader )
326
+ req .Header .Add (http .CanonicalHeaderKey ("Content-Type" ), "application/json" )
327
+ w := httptest .NewRecorder ()
328
+ svr .WebhookMux .ServeHTTP (w , req )
329
+ Expect (w .Code ).To (Equal (http .StatusOK ))
330
+ By ("sanity checking the response contains reasonable field" )
331
+ Expect (w .Body ).To (ContainSubstring (`"allowed":false` ))
332
+ Expect (w .Body ).To (ContainSubstring (`"code":403` ))
333
+
334
+ reader = strings .NewReader (`{
335
+ "kind":"AdmissionReview",
336
+ "apiVersion":"admission.k8s.io/v1beta1",
337
+ "request":{
338
+ "uid":"07e52e8d-4513-11e9-a716-42010a800270",
339
+ "kind":{
340
+ "group":"",
341
+ "version":"v1",
342
+ "kind":"TestValidator"
343
+ },
344
+ "resource":{
345
+ "group":"",
346
+ "version":"v1",
347
+ "resource":"testvalidator"
348
+ },
349
+ "namespace":"default",
350
+ "operation":"DELETE",
351
+ "object":null,
352
+ "oldObject":{
353
+ "replica":0
354
+ }
355
+ }
356
+ }` )
357
+ By ("sending a request to a validating webhook path with correct request" )
358
+ path = generateValidatePath (testValidatorGVK )
359
+ req = httptest .NewRequest ("POST" , "http://svc-name.svc-ns.svc" + path , reader )
360
+ req .Header .Add (http .CanonicalHeaderKey ("Content-Type" ), "application/json" )
361
+ w = httptest .NewRecorder ()
362
+ svr .WebhookMux .ServeHTTP (w , req )
363
+ Expect (w .Code ).To (Equal (http .StatusOK ))
364
+ By ("sanity checking the response contains reasonable field" )
365
+ Expect (w .Body ).To (ContainSubstring (`"allowed":true` ))
366
+ Expect (w .Body ).To (ContainSubstring (`"code":200` ))
367
+
368
+ })
272
369
})
273
370
})
274
371
@@ -357,6 +454,13 @@ func (v *TestValidator) ValidateUpdate(old runtime.Object) error {
357
454
return nil
358
455
}
359
456
457
+ func (v * TestValidator ) ValidateDelete () error {
458
+ if v .Replica > 0 {
459
+ return errors .New ("number of replica should be less than or equal to 0 to delete" )
460
+ }
461
+ return nil
462
+ }
463
+
360
464
// TestDefaultValidator
361
465
var _ runtime.Object = & TestDefaultValidator {}
362
466
@@ -407,3 +511,10 @@ func (dv *TestDefaultValidator) ValidateUpdate(old runtime.Object) error {
407
511
}
408
512
return nil
409
513
}
514
+
515
+ func (dv * TestDefaultValidator ) ValidateDelete () error {
516
+ if dv .Replica > 0 {
517
+ return errors .New ("number of replica should be less than or equal to 0 to delete" )
518
+ }
519
+ return nil
520
+ }
0 commit comments