From 1f3d839789315dbbb420d6ef4b5a29a849ada056 Mon Sep 17 00:00:00 2001 From: jsui Date: Wed, 24 Apr 2024 16:29:29 +0800 Subject: [PATCH] Fix webhook panic error caused by bumping controller-runtime The controller-manager won't automatically inject decoder to webhook handler in new implementation, which will cause the panic error of webhook. Use admission.NewDecoder(scheme) instead as it is now the only way to instantiate default decoder. https://github.com/kubernetes-sigs/controller-runtime/pull/2736 --- pkg/controllers/subnetset/subnetset_controller.go | 7 +++++-- pkg/controllers/subnetset/subnetset_webhook.go | 7 ------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/pkg/controllers/subnetset/subnetset_controller.go b/pkg/controllers/subnetset/subnetset_controller.go index 78c027d50..625d76bb6 100644 --- a/pkg/controllers/subnetset/subnetset_controller.go +++ b/pkg/controllers/subnetset/subnetset_controller.go @@ -19,9 +19,9 @@ import ( "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" "sigs.k8s.io/controller-runtime/pkg/webhook" + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" "github.com/vmware-tanzu/nsx-operator/pkg/apis/v1alpha1" - "github.com/vmware-tanzu/nsx-operator/pkg/config" "github.com/vmware-tanzu/nsx-operator/pkg/controllers/common" "github.com/vmware-tanzu/nsx-operator/pkg/logger" @@ -343,7 +343,10 @@ func (r *SubnetSetReconciler) Start(mgr ctrl.Manager, enableWebhook bool) error } hookServer.Register("/validate-nsx-vmware-com-v1alpha1-subnetset", &webhook.Admission{ - Handler: &SubnetSetValidator{Client: mgr.GetClient()}, + Handler: &SubnetSetValidator{ + Client: mgr.GetClient(), + decoder: admission.NewDecoder(mgr.GetScheme()), + }, }) } go r.GarbageCollector(make(chan bool), servicecommon.GCInterval) diff --git a/pkg/controllers/subnetset/subnetset_webhook.go b/pkg/controllers/subnetset/subnetset_webhook.go index 618a9e35d..1597799c9 100644 --- a/pkg/controllers/subnetset/subnetset_webhook.go +++ b/pkg/controllers/subnetset/subnetset_webhook.go @@ -103,10 +103,3 @@ func (v *SubnetSetValidator) Handle(ctx context.Context, req admission.Request) } return admission.Allowed("") } - -// InjectDecoder injects the decoder into a validator. -// A decoder will be automatically injected by controller-manager. -func (v *SubnetSetValidator) InjectDecoder(d *admission.Decoder) error { - v.decoder = d - return nil -}