From 286caf966aa7c6046726aac25abed456254baee2 Mon Sep 17 00:00:00 2001 From: Wen Zhou Date: Tue, 6 Aug 2024 04:58:40 -0400 Subject: [PATCH] Revert "feat: remove leader election for operator (#1000)" (#1153) * Revert "feat: remove leader election for operator (#1000)" - we need have leader election enabled for Operator This reverts commit 3610b0ba0cf19bb32853d9d513c2f42ff8ab9f2d. - remove files (configmap) not in use --------- Signed-off-by: Wen Zhou --- ...-operator-manager-config_v1_configmap.yaml | 14 ------------- ...atahub-operator.clusterserviceversion.yaml | 1 + config/default/manager_auth_proxy_patch.yaml | 1 + config/manager/controller_manager_config.yaml | 8 -------- config/manager/kustomization.yaml.in | 4 ---- config/manager/manager.yaml | 1 + main.go | 20 +++++++++++++++++-- 7 files changed, 21 insertions(+), 28 deletions(-) delete mode 100644 bundle/manifests/opendatahub-operator-manager-config_v1_configmap.yaml delete mode 100644 config/manager/controller_manager_config.yaml diff --git a/bundle/manifests/opendatahub-operator-manager-config_v1_configmap.yaml b/bundle/manifests/opendatahub-operator-manager-config_v1_configmap.yaml deleted file mode 100644 index 99bb8b739f3..00000000000 --- a/bundle/manifests/opendatahub-operator-manager-config_v1_configmap.yaml +++ /dev/null @@ -1,14 +0,0 @@ -apiVersion: v1 -data: - controller_manager_config.yaml: | - apiVersion: controller-runtime.sigs.k8s.io/v1alpha1 - kind: ControllerManagerConfig - health: - healthProbeBindAddress: :8081 - metrics: - bindAddress: 0.0.0.0:8080 - webhook: - port: 9443 -kind: ConfigMap -metadata: - name: opendatahub-operator-manager-config diff --git a/bundle/manifests/opendatahub-operator.clusterserviceversion.yaml b/bundle/manifests/opendatahub-operator.clusterserviceversion.yaml index fe7eea1e317..68fab62040c 100644 --- a/bundle/manifests/opendatahub-operator.clusterserviceversion.yaml +++ b/bundle/manifests/opendatahub-operator.clusterserviceversion.yaml @@ -1701,6 +1701,7 @@ spec: - args: - --health-probe-bind-address=:8081 - --metrics-bind-address=0.0.0.0:8080 + - --leader-elect command: - /manager env: diff --git a/config/default/manager_auth_proxy_patch.yaml b/config/default/manager_auth_proxy_patch.yaml index 287adcc0029..348b4430479 100644 --- a/config/default/manager_auth_proxy_patch.yaml +++ b/config/default/manager_auth_proxy_patch.yaml @@ -13,3 +13,4 @@ spec: args: - "--health-probe-bind-address=:8081" - "--metrics-bind-address=0.0.0.0:8080" + - "--leader-elect" diff --git a/config/manager/controller_manager_config.yaml b/config/manager/controller_manager_config.yaml deleted file mode 100644 index ec87ba2becd..00000000000 --- a/config/manager/controller_manager_config.yaml +++ /dev/null @@ -1,8 +0,0 @@ -apiVersion: controller-runtime.sigs.k8s.io/v1alpha1 -kind: ControllerManagerConfig -health: - healthProbeBindAddress: :8081 -metrics: - bindAddress: 0.0.0.0:8080 -webhook: - port: 9443 diff --git a/config/manager/kustomization.yaml.in b/config/manager/kustomization.yaml.in index f956c36fbe6..f3dcf81a77b 100644 --- a/config/manager/kustomization.yaml.in +++ b/config/manager/kustomization.yaml.in @@ -4,10 +4,6 @@ resources: generatorOptions: disableNameSuffixHash: true -configMapGenerator: -- files: - - controller_manager_config.yaml - name: manager-config apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization images: diff --git a/config/manager/manager.yaml b/config/manager/manager.yaml index 84fc18df971..0aadbcc2e89 100644 --- a/config/manager/manager.yaml +++ b/config/manager/manager.yaml @@ -47,6 +47,7 @@ spec: - name: DEFAULT_MANIFESTS_PATH value: /opt/manifests args: + - --leader-elect - --operator-name=opendatahub image: controller:latest imagePullPolicy: Always diff --git a/main.go b/main.go index d696de3b179..79350c7f2c1 100644 --- a/main.go +++ b/main.go @@ -98,6 +98,7 @@ func init() { //nolint:gochecknoinits func main() { //nolint:funlen var metricsAddr string + var enableLeaderElection bool var probeAddr string var dscApplicationsNamespace string var dscMonitoringNamespace string @@ -106,6 +107,9 @@ func main() { //nolint:funlen flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.") flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.") + flag.BoolVar(&enableLeaderElection, "leader-elect", false, + "Enable leader election for controller manager. "+ + "Enabling this will ensure there is only one active controller manager.") flag.StringVar(&dscApplicationsNamespace, "dsc-applications-namespace", "opendatahub", "The namespace where data science cluster"+ "applications will be deployed") flag.StringVar(&dscMonitoringNamespace, "dsc-monitoring-namespace", "opendatahub", "The namespace where data science cluster"+ @@ -119,12 +123,24 @@ func main() { //nolint:funlen // root context ctx := ctrl.SetupSignalHandler() - - mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ // single pod does not need to have LeaderElection + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ Scheme: scheme, MetricsBindAddress: metricsAddr, Port: 9443, HealthProbeBindAddress: probeAddr, + LeaderElection: enableLeaderElection, + LeaderElectionID: "07ed84f7.opendatahub.io", + // LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily + // when the Manager ends. This requires the binary to immediately end when the + // Manager is stopped, otherwise, this setting is unsafe. Setting this significantly + // speeds up voluntary leader transitions as the new leader don't have to wait + // LeaseDuration time first. + // + // In the default scaffold provided, the program ends immediately after + // the manager stops, so would be fine to enable this option. However, + // if you are doing or is intended to do any operation such as perform cleanups + // after the manager stops then its usage might be unsafe. + // LeaderElectionReleaseOnCancel: true, }) if err != nil { setupLog.Error(err, "unable to start manager")