@@ -153,6 +153,85 @@ func runTests(admissionReviewVersion string) {
153153 ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusNotFound ))
154154 })
155155
156+ It ("should scaffold a custom defaulting webhook with a custom path" , func () {
157+ By ("creating a controller manager" )
158+ m , err := manager .New (cfg , manager.Options {})
159+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
160+
161+ By ("registering the type in the Scheme" )
162+ builder := scheme.Builder {GroupVersion : testDefaulterGVK .GroupVersion ()}
163+ builder .Register (& TestDefaulter {}, & TestDefaulterList {})
164+ err = builder .AddToScheme (m .GetScheme ())
165+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
166+
167+ customPath := "/custom-defaulting-path"
168+ err = WebhookManagedBy (m ).
169+ For (& TestDefaulter {}).
170+ WithDefaulter (& TestCustomDefaulter {}).
171+ WithLogConstructor (func (base logr.Logger , req * admission.Request ) logr.Logger {
172+ return admission .DefaultLogConstructor (testingLogger , req )
173+ }).
174+ WithCustomPath (customPath ).
175+ Complete ()
176+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
177+ svr := m .GetWebhookServer ()
178+ ExpectWithOffset (1 , svr ).NotTo (BeNil ())
179+
180+ reader := strings .NewReader (admissionReviewGV + admissionReviewVersion + `",
181+ "request":{
182+ "uid":"07e52e8d-4513-11e9-a716-42010a800270",
183+ "kind":{
184+ "group":"foo.test.org",
185+ "version":"v1",
186+ "kind":"TestDefaulter"
187+ },
188+ "resource":{
189+ "group":"foo.test.org",
190+ "version":"v1",
191+ "resource":"testdefaulter"
192+ },
193+ "namespace":"default",
194+ "name":"foo",
195+ "operation":"CREATE",
196+ "object":{
197+ "replica":1
198+ },
199+ "oldObject":null
200+ }
201+ }` )
202+
203+ ctx , cancel := context .WithCancel (context .Background ())
204+ cancel ()
205+ err = svr .Start (ctx )
206+ if err != nil && ! os .IsNotExist (err ) {
207+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
208+ }
209+
210+ By ("sending a request to a mutating webhook path" )
211+ path , err := generateCustomPath (customPath )
212+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
213+ req := httptest .NewRequest ("POST" , svcBaseAddr + path , reader )
214+ req .Header .Add ("Content-Type" , "application/json" )
215+ w := httptest .NewRecorder ()
216+ svr .WebhookMux ().ServeHTTP (w , req )
217+ ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusOK ))
218+ By ("sanity checking the response contains reasonable fields" )
219+ ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":true` ))
220+ ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"patch":` ))
221+ ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":200` ))
222+ 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"` ))
223+
224+ By ("sending a request to a mutating webhook path that have been overrided by the custom path" )
225+ path = generateMutatePath (testDefaulterGVK )
226+ _ , err = reader .Seek (0 , 0 )
227+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
228+ req = httptest .NewRequest ("POST" , svcBaseAddr + path , reader )
229+ req .Header .Add ("Content-Type" , "application/json" )
230+ w = httptest .NewRecorder ()
231+ svr .WebhookMux ().ServeHTTP (w , req )
232+ ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusNotFound ))
233+ })
234+
156235 It ("should scaffold a custom defaulting webhook which recovers from panics" , func () {
157236 By ("creating a controller manager" )
158237 m , err := manager .New (cfg , manager.Options {})
@@ -294,6 +373,86 @@ func runTests(admissionReviewVersion string) {
294373 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"` ))
295374 })
296375
376+ It ("should scaffold a custom validating webhook with a custom path" , func () {
377+ By ("creating a controller manager" )
378+ m , err := manager .New (cfg , manager.Options {})
379+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
380+
381+ By ("registering the type in the Scheme" )
382+ builder := scheme.Builder {GroupVersion : testValidatorGVK .GroupVersion ()}
383+ builder .Register (& TestValidator {}, & TestValidatorList {})
384+ err = builder .AddToScheme (m .GetScheme ())
385+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
386+
387+ customPath := "/custom-validating-path"
388+ err = WebhookManagedBy (m ).
389+ For (& TestValidator {}).
390+ WithValidator (& TestCustomValidator {}).
391+ WithLogConstructor (func (base logr.Logger , req * admission.Request ) logr.Logger {
392+ return admission .DefaultLogConstructor (testingLogger , req )
393+ }).
394+ WithCustomPath (customPath ).
395+ Complete ()
396+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
397+ svr := m .GetWebhookServer ()
398+ ExpectWithOffset (1 , svr ).NotTo (BeNil ())
399+
400+ reader := strings .NewReader (admissionReviewGV + admissionReviewVersion + `",
401+ "request":{
402+ "uid":"07e52e8d-4513-11e9-a716-42010a800270",
403+ "kind":{
404+ "group":"foo.test.org",
405+ "version":"v1",
406+ "kind":"TestValidator"
407+ },
408+ "resource":{
409+ "group":"foo.test.org",
410+ "version":"v1",
411+ "resource":"testvalidator"
412+ },
413+ "namespace":"default",
414+ "name":"foo",
415+ "operation":"UPDATE",
416+ "object":{
417+ "replica":1
418+ },
419+ "oldObject":{
420+ "replica":2
421+ }
422+ }
423+ }` )
424+
425+ ctx , cancel := context .WithCancel (context .Background ())
426+ cancel ()
427+ err = svr .Start (ctx )
428+ if err != nil && ! os .IsNotExist (err ) {
429+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
430+ }
431+
432+ By ("sending a request to a mutating webhook path that have been overrided by a custom path" )
433+ path := generateValidatePath (testValidatorGVK )
434+ req := httptest .NewRequest ("POST" , svcBaseAddr + path , reader )
435+ req .Header .Add ("Content-Type" , "application/json" )
436+ w := httptest .NewRecorder ()
437+ svr .WebhookMux ().ServeHTTP (w , req )
438+ ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusNotFound ))
439+
440+ By ("sending a request to a validating webhook path" )
441+ path , err = generateCustomPath (customPath )
442+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
443+ _ , err = reader .Seek (0 , 0 )
444+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
445+ req = httptest .NewRequest ("POST" , svcBaseAddr + path , reader )
446+ req .Header .Add ("Content-Type" , "application/json" )
447+ w = httptest .NewRecorder ()
448+ svr .WebhookMux ().ServeHTTP (w , req )
449+ ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusOK ))
450+ By ("sanity checking the response contains reasonable field" )
451+ ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":false` ))
452+ ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":403` ))
453+ 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"` ))
454+ })
455+
297456 It ("should scaffold a custom validating webhook which recovers from panics" , func () {
298457 By ("creating a controller manager" )
299458 m , err := manager .New (cfg , manager.Options {})
0 commit comments