Skip to content

Commit 2571d04

Browse files
authored
Use operator name from env var for webhook setup (#222)
No need to hardcode operator name as we can determine it from the env var we already pass to the operator.
1 parent 9ed5f98 commit 2571d04

File tree

3 files changed

+8
-12
lines changed

3 files changed

+8
-12
lines changed

main.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ func main() {
130130
initOpsManagerImageVersion := env.ReadOrDefault(util.InitOpsManagerVersion, "latest")
131131
// Namespace where the operator is installed
132132
currentNamespace := env.ReadOrPanic(util.CurrentNamespace)
133+
webhookSVCSelector := env.ReadOrPanic(util.OperatorNameEnv)
133134

134135
enableClusterMongoDBRoles := slices.Contains(crds, clusterMongoDBRoleCRDPlural)
135136

@@ -163,7 +164,7 @@ func main() {
163164
managerOptions.HealthProbeBindAddress = "127.0.0.1:8181"
164165
}
165166

166-
webhookOptions := setupWebhook(ctx, cfg, log, slices.Contains(crds, mongoDBMultiClusterCRDPlural), currentNamespace)
167+
webhookOptions := setupWebhook(ctx, cfg, log, webhookSVCSelector, currentNamespace)
167168
managerOptions.WebhookServer = crWebhook.NewServer(webhookOptions)
168169

169170
mgr, err := ctrl.NewManager(cfg, managerOptions)
@@ -393,7 +394,7 @@ func isInLocalMode() bool {
393394

394395
// setupWebhook sets up the validation webhook for MongoDB resources in order
395396
// to give people early warning when their MongoDB resources are wrong.
396-
func setupWebhook(ctx context.Context, cfg *rest.Config, log *zap.SugaredLogger, multiClusterMode bool, currentNamespace string) crWebhook.Options {
397+
func setupWebhook(ctx context.Context, cfg *rest.Config, log *zap.SugaredLogger, svcSelector string, currentNamespace string) crWebhook.Options {
397398
// set webhook port — 1993 is chosen as Ben's birthday
398399
webhookPort := env.ReadIntOrDefault(util.MdbWebhookPortEnv, 1993)
399400

@@ -419,7 +420,7 @@ func setupWebhook(ctx context.Context, cfg *rest.Config, log *zap.SugaredLogger,
419420
Namespace: currentNamespace,
420421
}
421422

422-
if err := webhook.Setup(ctx, webhookClient, webhookServiceLocation, certDir, webhookPort, multiClusterMode, log); err != nil {
423+
if err := webhook.Setup(ctx, webhookClient, webhookServiceLocation, certDir, webhookPort, svcSelector, log); err != nil {
423424
log.Errorf("could not set up webhook: %v", err)
424425
}
425426

pkg/util/constants.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ const (
8383
// Pod/StatefulSet specific constants
8484
OperatorName = "mongodb-kubernetes-operator"
8585
LegacyOperatorName = "mongodb-enterprise-operator" // Still used for some selectors and labels
86-
MultiClusterOperatorName = "mongodb-kubernetes-operator-multi-cluster"
8786
OperatorLabelName = "controller"
8887
OperatorLabelValue = LegacyOperatorName
8988
OpsManagerContainerName = "mongodb-ops-manager"
@@ -196,6 +195,7 @@ const (
196195
BackupDisableWaitRetriesEnv = "BACKUP_WAIT_RETRIES"
197196
ManagedSecurityContextEnv = "MANAGED_SECURITY_CONTEXT"
198197
CurrentNamespace = "NAMESPACE"
198+
OperatorNameEnv = "OPERATOR_NAME"
199199
WatchNamespace = "WATCH_NAMESPACE"
200200
OpsManagerMonitorAppDB = "OPS_MANAGER_MONITOR_APPDB"
201201
MongodbCommunityAgentImageEnv = "MDB_COMMUNITY_AGENT_IMAGE"

pkg/webhook/setup.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@ import (
2424
const controllerLabelName = "app.kubernetes.io/name"
2525

2626
// createWebhookService creates a Kubernetes service for the webhook.
27-
func createWebhookService(ctx context.Context, client client.Client, location types.NamespacedName, webhookPort int, multiClusterMode bool) error {
28-
svcSelector := util.OperatorName
29-
if multiClusterMode {
30-
svcSelector = util.MultiClusterOperatorName
31-
}
32-
27+
func createWebhookService(ctx context.Context, client client.Client, location types.NamespacedName, webhookPort int, svcSelector string) error {
3328
svc := corev1.Service{
3429
ObjectMeta: metav1.ObjectMeta{
3530
Name: location.Name,
@@ -186,7 +181,7 @@ func shouldRegisterWebhookConfiguration() bool {
186181
return env.ReadBoolOrDefault(util.MdbWebhookRegisterConfigurationEnv, true) // nolint:forbidigo
187182
}
188183

189-
func Setup(ctx context.Context, client client.Client, serviceLocation types.NamespacedName, certDirectory string, webhookPort int, multiClusterMode bool, log *zap.SugaredLogger) error {
184+
func Setup(ctx context.Context, client client.Client, serviceLocation types.NamespacedName, certDirectory string, webhookPort int, svcSelector string, log *zap.SugaredLogger) error {
190185
if !shouldRegisterWebhookConfiguration() {
191186
log.Debugf("Skipping configuration of ValidatingWebhookConfiguration")
192187
// 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
217212
return err
218213
}
219214

220-
if err := createWebhookService(ctx, client, serviceLocation, webhookPort, multiClusterMode); err != nil {
215+
if err := createWebhookService(ctx, client, serviceLocation, webhookPort, svcSelector); err != nil {
221216
return err
222217
}
223218

0 commit comments

Comments
 (0)