@@ -217,6 +217,82 @@ func runTests(admissionReviewVersion string) {
217
217
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"message":"panic: fake panic test [recovered]` ))
218
218
})
219
219
220
+ It ("should scaffold a mutating webhook with a mutator" , func () {
221
+ By ("creating a controller manager" )
222
+ m , err := manager .New (cfg , manager.Options {})
223
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
224
+
225
+ By ("registering the type in the Scheme" )
226
+ builder := scheme.Builder {GroupVersion : testDefaulterGVK .GroupVersion ()}
227
+ builder .Register (& TestDefaulter {}, & TestDefaulterList {})
228
+ err = builder .AddToScheme (m .GetScheme ())
229
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
230
+
231
+ err = WebhookManagedBy (m ).
232
+ WithMutatorFactory (mutatorFactoryForTestDefaulter (m .GetScheme ())).
233
+ For (& TestDefaulter {}).
234
+ WithLogConstructor (func (base logr.Logger , req * admission.Request ) logr.Logger {
235
+ return admission .DefaultLogConstructor (testingLogger , req )
236
+ }).
237
+ Complete ()
238
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
239
+ svr := m .GetWebhookServer ()
240
+ ExpectWithOffset (1 , svr ).NotTo (BeNil ())
241
+
242
+ reader := strings .NewReader (admissionReviewGV + admissionReviewVersion + `",
243
+ "request":{
244
+ "uid":"07e52e8d-4513-11e9-a716-42010a800270",
245
+ "kind":{
246
+ "group":"foo.test.org",
247
+ "version":"v1",
248
+ "kind":"TestDefaulter"
249
+ },
250
+ "resource":{
251
+ "group":"foo.test.org",
252
+ "version":"v1",
253
+ "resource":"testdefaulter"
254
+ },
255
+ "namespace":"default",
256
+ "name":"foo",
257
+ "operation":"CREATE",
258
+ "object":{
259
+ "replica":1
260
+ },
261
+ "oldObject":null
262
+ }
263
+ }` )
264
+
265
+ ctx , cancel := context .WithCancel (context .Background ())
266
+ cancel ()
267
+ err = svr .Start (ctx )
268
+ if err != nil && ! os .IsNotExist (err ) {
269
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
270
+ }
271
+
272
+ By ("sending a request to a mutating webhook path" )
273
+ path := generateMutatePath (testDefaulterGVK )
274
+ req := httptest .NewRequest ("POST" , svcBaseAddr + path , reader )
275
+ req .Header .Add ("Content-Type" , "application/json" )
276
+ w := httptest .NewRecorder ()
277
+ svr .WebhookMux ().ServeHTTP (w , req )
278
+ ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusOK ))
279
+ By ("sanity checking the response contains reasonable fields" )
280
+ ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":true` ))
281
+ ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"patch":` ))
282
+ ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"code":200` ))
283
+ 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"` ))
284
+
285
+ By ("sending a request to a validating webhook path that doesn't exist" )
286
+ path = generateValidatePath (testDefaulterGVK )
287
+ _ , err = reader .Seek (0 , 0 )
288
+ ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
289
+ req = httptest .NewRequest ("POST" , svcBaseAddr + path , reader )
290
+ req .Header .Add ("Content-Type" , "application/json" )
291
+ w = httptest .NewRecorder ()
292
+ svr .WebhookMux ().ServeHTTP (w , req )
293
+ ExpectWithOffset (1 , w .Code ).To (Equal (http .StatusNotFound ))
294
+ })
295
+
220
296
It ("should scaffold a custom validating webhook" , func () {
221
297
By ("creating a controller manager" )
222
298
m , err := manager .New (cfg , manager.Options {})
@@ -592,6 +668,12 @@ func (*TestCustomDefaulter) Default(ctx context.Context, obj runtime.Object) err
592
668
593
669
var _ admission.CustomDefaulter = & TestCustomDefaulter {}
594
670
671
+ func mutatorFactoryForTestDefaulter (scheme * runtime.Scheme ) admission.HandlerFactory {
672
+ return func (obj runtime.Object , _ admission.Decoder ) admission.Handler {
673
+ return admission .WithCustomDefaulter (scheme , obj , & TestCustomDefaulter {}).Handler
674
+ }
675
+ }
676
+
595
677
// TestCustomValidator.
596
678
597
679
type TestCustomValidator struct {}
0 commit comments