Skip to content

Commit

Permalink
Fix webhook panic error caused by bumping controller-runtime
Browse files Browse the repository at this point in the history
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.

kubernetes-sigs/controller-runtime#2736
  • Loading branch information
jwsui committed Apr 24, 2024
1 parent 1040748 commit 1f3d839
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 9 deletions.
7 changes: 5 additions & 2 deletions pkg/controllers/subnetset/subnetset_controller.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 0 additions & 7 deletions pkg/controllers/subnetset/subnetset_webhook.go
Expand Up @@ -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
}

0 comments on commit 1f3d839

Please sign in to comment.