From 3822b449284b78a17d45ecb30c298b93236ebd04 Mon Sep 17 00:00:00 2001 From: Mikalai Radchuk <509198+m1kola@users.noreply.github.com> Date: Wed, 2 Jul 2025 17:28:12 +0200 Subject: [PATCH] Use operator name from env var for webhook setup --- main.go | 7 ++++--- pkg/util/constants.go | 2 +- pkg/webhook/setup.go | 11 +++-------- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/main.go b/main.go index b69a76791..484c1b061 100644 --- a/main.go +++ b/main.go @@ -130,6 +130,7 @@ func main() { initOpsManagerImageVersion := env.ReadOrDefault(util.InitOpsManagerVersion, "latest") // Namespace where the operator is installed currentNamespace := env.ReadOrPanic(util.CurrentNamespace) + webhookSVCSelector := env.ReadOrPanic(util.OperatorNameEnv) enableClusterMongoDBRoles := slices.Contains(crds, clusterMongoDBRoleCRDPlural) @@ -163,7 +164,7 @@ func main() { managerOptions.HealthProbeBindAddress = "127.0.0.1:8181" } - webhookOptions := setupWebhook(ctx, cfg, log, slices.Contains(crds, mongoDBMultiClusterCRDPlural), currentNamespace) + webhookOptions := setupWebhook(ctx, cfg, log, webhookSVCSelector, currentNamespace) managerOptions.WebhookServer = crWebhook.NewServer(webhookOptions) mgr, err := ctrl.NewManager(cfg, managerOptions) @@ -393,7 +394,7 @@ func isInLocalMode() bool { // setupWebhook sets up the validation webhook for MongoDB resources in order // to give people early warning when their MongoDB resources are wrong. -func setupWebhook(ctx context.Context, cfg *rest.Config, log *zap.SugaredLogger, multiClusterMode bool, currentNamespace string) crWebhook.Options { +func setupWebhook(ctx context.Context, cfg *rest.Config, log *zap.SugaredLogger, svcSelector string, currentNamespace string) crWebhook.Options { // set webhook port — 1993 is chosen as Ben's birthday webhookPort := env.ReadIntOrDefault(util.MdbWebhookPortEnv, 1993) @@ -419,7 +420,7 @@ func setupWebhook(ctx context.Context, cfg *rest.Config, log *zap.SugaredLogger, Namespace: currentNamespace, } - if err := webhook.Setup(ctx, webhookClient, webhookServiceLocation, certDir, webhookPort, multiClusterMode, log); err != nil { + if err := webhook.Setup(ctx, webhookClient, webhookServiceLocation, certDir, webhookPort, svcSelector, log); err != nil { log.Errorf("could not set up webhook: %v", err) } diff --git a/pkg/util/constants.go b/pkg/util/constants.go index 9ed9d94eb..8816ba223 100644 --- a/pkg/util/constants.go +++ b/pkg/util/constants.go @@ -83,7 +83,6 @@ const ( // Pod/StatefulSet specific constants OperatorName = "mongodb-kubernetes-operator" LegacyOperatorName = "mongodb-enterprise-operator" // Still used for some selectors and labels - MultiClusterOperatorName = "mongodb-kubernetes-operator-multi-cluster" OperatorLabelName = "controller" OperatorLabelValue = LegacyOperatorName OpsManagerContainerName = "mongodb-ops-manager" @@ -196,6 +195,7 @@ const ( BackupDisableWaitRetriesEnv = "BACKUP_WAIT_RETRIES" ManagedSecurityContextEnv = "MANAGED_SECURITY_CONTEXT" CurrentNamespace = "NAMESPACE" + OperatorNameEnv = "OPERATOR_NAME" WatchNamespace = "WATCH_NAMESPACE" OpsManagerMonitorAppDB = "OPS_MANAGER_MONITOR_APPDB" MongodbCommunityAgentImageEnv = "MDB_COMMUNITY_AGENT_IMAGE" diff --git a/pkg/webhook/setup.go b/pkg/webhook/setup.go index c0f2071f9..283fadc6b 100644 --- a/pkg/webhook/setup.go +++ b/pkg/webhook/setup.go @@ -24,12 +24,7 @@ import ( const controllerLabelName = "app.kubernetes.io/name" // createWebhookService creates a Kubernetes service for the webhook. -func createWebhookService(ctx context.Context, client client.Client, location types.NamespacedName, webhookPort int, multiClusterMode bool) error { - svcSelector := util.OperatorName - if multiClusterMode { - svcSelector = util.MultiClusterOperatorName - } - +func createWebhookService(ctx context.Context, client client.Client, location types.NamespacedName, webhookPort int, svcSelector string) error { svc := corev1.Service{ ObjectMeta: metav1.ObjectMeta{ Name: location.Name, @@ -186,7 +181,7 @@ func shouldRegisterWebhookConfiguration() bool { return env.ReadBoolOrDefault(util.MdbWebhookRegisterConfigurationEnv, true) // nolint:forbidigo } -func Setup(ctx context.Context, client client.Client, serviceLocation types.NamespacedName, certDirectory string, webhookPort int, multiClusterMode bool, log *zap.SugaredLogger) error { +func Setup(ctx context.Context, client client.Client, serviceLocation types.NamespacedName, certDirectory string, webhookPort int, svcSelector string, log *zap.SugaredLogger) error { if !shouldRegisterWebhookConfiguration() { log.Debugf("Skipping configuration of ValidatingWebhookConfiguration") // After upgrading OLM version after migrating to proper OLM webhooks we don't need that `operator-service` anymore. @@ -217,7 +212,7 @@ func Setup(ctx context.Context, client client.Client, serviceLocation types.Name return err } - if err := createWebhookService(ctx, client, serviceLocation, webhookPort, multiClusterMode); err != nil { + if err := createWebhookService(ctx, client, serviceLocation, webhookPort, svcSelector); err != nil { return err }