@@ -30,9 +30,12 @@ import (
30
30
. "github.com/onsi/ginkgo/v2"
31
31
. "github.com/onsi/gomega"
32
32
"github.com/onsi/gomega/gbytes"
33
+ "gomodules.xyz/jsonpatch/v2"
34
+ admissionv1 "k8s.io/api/admission/v1"
33
35
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34
36
"k8s.io/apimachinery/pkg/runtime"
35
37
"k8s.io/apimachinery/pkg/runtime/schema"
38
+ "k8s.io/utils/ptr"
36
39
37
40
logf "sigs.k8s.io/controller-runtime/pkg/log"
38
41
"sigs.k8s.io/controller-runtime/pkg/log/zap"
@@ -229,8 +232,8 @@ func runTests(admissionReviewVersion string) {
229
232
ExpectWithOffset (1 , err ).NotTo (HaveOccurred ())
230
233
231
234
err = WebhookManagedBy (m ).
232
- WithMutatorFactory (mutatorFactoryForTestDefaulter (m .GetScheme ())).
233
235
For (& TestDefaulter {}).
236
+ WithMutationHandler (& TestMutationHandler {}).
234
237
WithLogConstructor (func (base logr.Logger , req * admission.Request ) logr.Logger {
235
238
return admission .DefaultLogConstructor (testingLogger , req )
236
239
}).
@@ -280,7 +283,7 @@ func runTests(admissionReviewVersion string) {
280
283
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"allowed":true` ))
281
284
ExpectWithOffset (1 , w .Body ).To (ContainSubstring (`"patch":` ))
282
285
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"` ))
286
+ EventuallyWithOffset (1 , logBuffer ).Should (gbytes .Say (`"msg":"Mutating 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
287
285
288
By ("sending a request to a validating webhook path that doesn't exist" )
286
289
path = generateValidatePath (testDefaulterGVK )
@@ -646,7 +649,8 @@ func (*TestDefaultValidatorList) DeepCopyObject() runtime.Object { return nil
646
649
type TestCustomDefaulter struct {}
647
650
648
651
func (* TestCustomDefaulter ) Default (ctx context.Context , obj runtime.Object ) error {
649
- logf .FromContext (ctx ).Info ("Defaulting object" )
652
+ log := logf .FromContext (ctx )
653
+ log .Info ("Defaulting object" )
650
654
req , err := admission .RequestFromContext (ctx )
651
655
if err != nil {
652
656
return fmt .Errorf ("expected admission.Request in ctx: %w" , err )
@@ -668,12 +672,27 @@ func (*TestCustomDefaulter) Default(ctx context.Context, obj runtime.Object) err
668
672
669
673
var _ admission.CustomDefaulter = & TestCustomDefaulter {}
670
674
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
675
+ // TestMutationHandler
676
+ type TestMutationHandler struct {}
677
+
678
+ func (* TestMutationHandler ) Handle (ctx context.Context , req admission.Request ) admission.Response {
679
+ log := logf .FromContext (ctx )
680
+ log .Info ("Mutating object" )
681
+ return admission.Response {
682
+ AdmissionResponse : admissionv1.AdmissionResponse {
683
+ Allowed : true ,
684
+ PatchType : ptr .To (admissionv1 .PatchTypeJSONPatch ),
685
+ },
686
+ Patches : []jsonpatch.Operation {{
687
+ Operation : "replace" ,
688
+ Path : "/replica" ,
689
+ Value : "2" ,
690
+ }},
674
691
}
675
692
}
676
693
694
+ var _ admission.Handler = & TestMutationHandler {}
695
+
677
696
// TestCustomValidator.
678
697
679
698
type TestCustomValidator struct {}
0 commit comments