From 6fc0a412d328695cf2c4194ca26d89c0727b6930 Mon Sep 17 00:00:00 2001 From: Yauheni Kaliuta Date: Wed, 9 Oct 2024 10:30:13 +0300 Subject: [PATCH] webhook: move initialization inside of the module Add webhook.Init() function and hide webhook setup inside of the module. It will make it possible to replace Init with a NOP (no operation) with conditional compilation for no-webhook build. Signed-off-by: Yauheni Kaliuta --- controllers/webhook/webhook.go | 9 +++++++++ main.go | 8 +------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/controllers/webhook/webhook.go b/controllers/webhook/webhook.go index 9455cfd08ba..29739e2af00 100644 --- a/controllers/webhook/webhook.go +++ b/controllers/webhook/webhook.go @@ -46,6 +46,15 @@ type OpenDataHubValidatingWebhook struct { Decoder *admission.Decoder } +func Init(mgr ctrl.Manager) { + (&OpenDataHubValidatingWebhook{ + Client: mgr.GetClient(), + Decoder: admission.NewDecoder(mgr.GetScheme()), + }).SetupWithManager(mgr) + + (&DSCDefaulter{}).SetupWithManager(mgr) +} + func (w *OpenDataHubValidatingWebhook) SetupWithManager(mgr ctrl.Manager) { hookServer := mgr.GetWebhookServer() odhWebhook := &webhook.Admission{ diff --git a/main.go b/main.go index 7d33d59a997..778a829f5ee 100644 --- a/main.go +++ b/main.go @@ -55,7 +55,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/manager" ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics/server" ctrlwebhook "sigs.k8s.io/controller-runtime/pkg/webhook" - "sigs.k8s.io/controller-runtime/pkg/webhook/admission" dscv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/datasciencecluster/v1" dsciv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1" @@ -235,12 +234,7 @@ func main() { //nolint:funlen,maintidx os.Exit(1) } - (&webhook.OpenDataHubValidatingWebhook{ - Client: mgr.GetClient(), - Decoder: admission.NewDecoder(mgr.GetScheme()), - }).SetupWithManager(mgr) - - (&webhook.DSCDefaulter{}).SetupWithManager(mgr) + webhook.Init(mgr) if err = (&dscictrl.DSCInitializationReconciler{ Client: mgr.GetClient(),