Skip to content

Use operator name from env var for webhook setup #222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/util/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
11 changes: 3 additions & 8 deletions pkg/webhook/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

Expand Down