@@ -288,6 +288,82 @@ func runTests(admissionReviewVersion string) {
288288ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusNotFound ))
289289})
290290
291+ It ("should scaffold a mutating webhook with a mutator" , func () {
292+ By ("creating a controller manager" )
293+ m , err := manager .New (cfg , manager.Options {})
294+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
295+
296+ By ("registering the type in the Scheme" )
297+ builder := scheme.Builder {GroupVersion : testDefaulterGVK .GroupVersion ()}
298+ builder .Register (& TestDefaulter {}, & TestDefaulterList {})
299+ err = builder .AddToScheme (m .GetScheme ())
300+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
301+
302+ err = WebhookManagedBy (m ).
303+ WithMutatorFactory (mutatorFactoryForTestDefaulter (m .GetScheme ())).
304+ For (& TestDefaulter {}).
305+ WithLogConstructor (func (base logr.Logger , req * admission.Request ) logr.Logger {
306+ return admission .DefaultLogConstructor (testingLogger , req )
307+ }).
308+ Complete ()
309+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
310+ svr := m .GetWebhookServer ()
311+ ExpectWithOffset (1 , svr ).NotTo (BeNil ())
312+
313+ reader := strings .NewReader (admissionReviewGV + admissionReviewVersion + `",
314+ "request":{
315+ "uid":"07e52e8d-4513-11e9-a716-42010a800270",
316+ "kind":{
317+ "group":"foo.test.org",
318+ "version":"v1",
319+ "kind":"TestDefaulter"
320+ },
321+ "resource":{
322+ "group":"foo.test.org",
323+ "version":"v1",
324+ "resource":"testdefaulter"
325+ },
326+ "namespace":"default",
327+ "name":"foo",
328+ "operation":"CREATE",
329+ "object":{
330+ "replica":1
331+ },
332+ "oldObject":null
333+ }
334+ }` )
335+
336+ ctx , cancel := context .WithCancel (context .Background ())
337+ cancel ()
338+ err = svr .Start (ctx )
339+ if err != nil && ! os .IsNotExist (err ) {
340+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
341+ }
342+
343+ By ("sending a request to a mutating webhook path" )
344+ path := generateMutatePath (testDefaulterGVK )
345+ req := httptest .NewRequest ("POST" , svcBaseAddr + path , reader )
346+ req .Header .Add ("Content-Type" , "application/json" )
347+ w := httptest .NewRecorder ()
348+ svr .WebhookMux ().ServeHTTP (w , req )
349+ ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusOK ))
350+ By ("sanity checking the response contains reasonable fields" )
351+ ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":true` ))
352+ ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"patch":` ))
353+ ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":200` ))
354+ 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"` ))
355+
356+ By ("sending a request to a validating webhook path that doesn't exist" )
357+ path = generateValidatePath (testDefaulterGVK )
358+ _ , err = reader .Seek (0 , 0 )
359+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
360+ req = httptest .NewRequest ("POST" , svcBaseAddr + path , reader )
361+ req .Header .Add ("Content-Type" , "application/json" )
362+ w = httptest .NewRecorder ()
363+ svr .WebhookMux ().ServeHTTP (w , req )
364+ ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusNotFound ))
365+ })
366+
291367It ("should scaffold a custom validating webhook if the type implements the CustomValidator interface" , func () {
292368By ("creating a controller manager" )
293369m , err := manager .New (cfg , manager.Options {})
@@ -735,6 +811,12 @@ func (*TestCustomDefaulter) Default(ctx context.Context, obj runtime.Object) err
735811
736812var _ admission.CustomDefaulter = & TestCustomDefaulter {}
737813
814+ func mutatorFactoryForTestDefaulter (scheme * runtime.Scheme ) admission.HandlerFactory {
815+ return func (obj runtime.Object , _ admission.Decoder ) admission.Handler {
816+ return admission .WithCustomDefaulter (scheme , obj , & TestCustomDefaulter {}).Handler
817+ }
818+ }
819+
738820// TestCustomValidator.
739821
740822type TestCustomValidator struct {}
0 commit comments