Skip to content

Commit ac6d6f3

Browse files
committed
chore: make leadership election configurable
1 parent e00259b commit ac6d6f3

2 files changed

Lines changed: 103 additions & 19 deletions

File tree

cmd/controller/manager.go

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"os"
99
"path/filepath"
10+
"time"
1011

1112
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
1213
// to ensure that exec-entrypoint and run can make use of them.
@@ -52,6 +53,12 @@ func NewControllerManagerCommand() *cobra.Command {
5253
var metricsCertPath, metricsCertName, metricsCertKey string
5354
var webhookCertPath, webhookCertName, webhookCertKey string
5455
var enableLeaderElection bool
56+
var leaderElectionID string
57+
var leaderElectionNamespace string
58+
var leaderElectionLeaseDuration time.Duration
59+
var leaderElectionRenewDeadline time.Duration
60+
var leaderElectionRetryPeriod time.Duration
61+
var leaderElectionReleaseOnCancel bool
5562
var probeAddr string
5663
var secureMetrics bool
5764
var enableHTTP2 bool
@@ -67,6 +74,12 @@ func NewControllerManagerCommand() *cobra.Command {
6774
metricsCertPath, metricsCertName, metricsCertKey,
6875
webhookCertPath, webhookCertName, webhookCertKey,
6976
enableLeaderElection,
77+
leaderElectionID,
78+
leaderElectionNamespace,
79+
leaderElectionLeaseDuration,
80+
leaderElectionRenewDeadline,
81+
leaderElectionRetryPeriod,
82+
leaderElectionReleaseOnCancel,
7083
serverConfigFile,
7184
probeAddr,
7285
secureMetrics,
@@ -79,9 +92,27 @@ func NewControllerManagerCommand() *cobra.Command {
7992
cmd.Flags().StringVar(&metricsAddr, "metrics-bind-address", "0", "The address the metrics endpoint binds to. "+
8093
"Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service.")
8194
cmd.Flags().StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
95+
96+
// Leader election flags
8297
cmd.Flags().BoolVar(&enableLeaderElection, "leader-elect", false,
8398
"Enable leader election for controller manager. "+
8499
"Enabling this will ensure there is only one active controller manager.")
100+
cmd.Flags().StringVar(&leaderElectionID, "leader-election-id", "81afa9db.datumapis.com",
101+
"The name of the resource that leader election will use for holding the leader lock.")
102+
cmd.Flags().StringVar(&leaderElectionNamespace, "leader-election-namespace", "",
103+
"The namespace in which the leader election resource will be created. "+
104+
"If not specified, it will use the namespace where the controller is running.")
105+
cmd.Flags().DurationVar(&leaderElectionLeaseDuration, "leader-election-lease-duration", 15*time.Second,
106+
"The duration that non-leader candidates will wait to force acquire leadership.")
107+
cmd.Flags().DurationVar(&leaderElectionRenewDeadline, "leader-election-renew-deadline", 10*time.Second,
108+
"The duration that the acting leader will retry refreshing leadership before giving up.")
109+
cmd.Flags().DurationVar(&leaderElectionRetryPeriod, "leader-election-retry-period", 2*time.Second,
110+
"The duration the LeaderElector clients should wait between tries of actions.")
111+
cmd.Flags().BoolVar(&leaderElectionReleaseOnCancel, "leader-election-release-on-cancel", false,
112+
"If the leader should step down voluntarily when the Manager ends. "+
113+
"This requires the binary to immediately end when the Manager is stopped.")
114+
115+
// Security and certificate flags
85116
cmd.Flags().BoolVar(&secureMetrics, "metrics-secure", true,
86117
"If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead.")
87118
cmd.Flags().StringVar(&webhookCertPath, "webhook-cert-path", "", "The directory that contains the webhook certificate.")
@@ -112,6 +143,12 @@ func runControllerManager(
112143
metricsCertPath, metricsCertName, metricsCertKey string,
113144
webhookCertPath, webhookCertName, webhookCertKey string,
114145
enableLeaderElection bool,
146+
leaderElectionID string,
147+
leaderElectionNamespace string,
148+
leaderElectionLeaseDuration time.Duration,
149+
leaderElectionRenewDeadline time.Duration,
150+
leaderElectionRetryPeriod time.Duration,
151+
leaderElectionReleaseOnCancel bool,
115152
serverConfigFile string,
116153
probeAddr string,
117154
secureMetrics bool,
@@ -232,23 +269,17 @@ func runControllerManager(
232269
}
233270

234271
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
235-
Scheme: scheme,
236-
Metrics: metricsServerOptions,
237-
WebhookServer: webhookServer,
238-
HealthProbeBindAddress: probeAddr,
239-
LeaderElection: enableLeaderElection,
240-
LeaderElectionID: "81afa9db.datumapis.com",
241-
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
242-
// when the Manager ends. This requires the binary to immediately end when the
243-
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly
244-
// speeds up voluntary leader transitions as the new leader don't have to wait
245-
// LeaseDuration time first.
246-
//
247-
// In the default scaffold provided, the program ends immediately after
248-
// the manager stops, so would be fine to enable this option. However,
249-
// if you are doing or is intended to do any operation such as perform cleanups
250-
// after the manager stops then its usage might be unsafe.
251-
// LeaderElectionReleaseOnCancel: true,
272+
Scheme: scheme,
273+
Metrics: metricsServerOptions,
274+
WebhookServer: webhookServer,
275+
HealthProbeBindAddress: probeAddr,
276+
LeaderElection: enableLeaderElection,
277+
LeaderElectionID: leaderElectionID,
278+
LeaderElectionNamespace: leaderElectionNamespace,
279+
LeaseDuration: &leaderElectionLeaseDuration,
280+
RenewDeadline: &leaderElectionRenewDeadline,
281+
RetryPeriod: &leaderElectionRetryPeriod,
282+
LeaderElectionReleaseOnCancel: leaderElectionReleaseOnCancel,
252283
})
253284
if err != nil {
254285
setupLog.Error(err, "unable to start manager")

config/manager/manager.yaml

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,61 @@ spec:
5050
containers:
5151
- args:
5252
- controller-manager
53-
- --leader-elect
54-
- --health-probe-bind-address=:8081
53+
- --metrics-bind-address=$(METRICS_BIND_ADDRESS)
54+
- --health-probe-bind-address=$(HEALTH_PROBE_BIND_ADDRESS)
55+
- --leader-elect=$(LEADER_ELECT)
56+
- --leader-election-id=$(LEADER_ELECTION_ID)
57+
- --leader-election-namespace=$(LEADER_ELECTION_NAMESPACE)
58+
- --leader-election-lease-duration=$(LEADER_ELECTION_LEASE_DURATION)
59+
- --leader-election-renew-deadline=$(LEADER_ELECTION_RENEW_DEADLINE)
60+
- --leader-election-retry-period=$(LEADER_ELECTION_RETRY_PERIOD)
61+
- --leader-election-release-on-cancel=$(LEADER_ELECTION_RELEASE_ON_CANCEL)
62+
- --metrics-secure=$(METRICS_SECURE)
63+
- --webhook-cert-path=$(WEBHOOK_CERT_PATH)
64+
- --webhook-cert-name=$(WEBHOOK_CERT_NAME)
65+
- --webhook-cert-key=$(WEBHOOK_CERT_KEY)
66+
- --metrics-cert-path=$(METRICS_CERT_PATH)
67+
- --metrics-cert-name=$(METRICS_CERT_NAME)
68+
- --metrics-cert-key=$(METRICS_CERT_KEY)
69+
- --enable-http2=$(ENABLE_HTTP2)
70+
- --config=$(CONFIG_FILE)
71+
env:
72+
- name: METRICS_BIND_ADDRESS
73+
value: "0"
74+
- name: HEALTH_PROBE_BIND_ADDRESS
75+
value: ":8081"
76+
- name: LEADER_ELECT
77+
value: "true"
78+
- name: LEADER_ELECTION_ID
79+
value: "81afa9db.datumapis.com"
80+
- name: LEADER_ELECTION_NAMESPACE
81+
value: ""
82+
- name: LEADER_ELECTION_LEASE_DURATION
83+
value: "15s"
84+
- name: LEADER_ELECTION_RENEW_DEADLINE
85+
value: "10s"
86+
- name: LEADER_ELECTION_RETRY_PERIOD
87+
value: "2s"
88+
- name: LEADER_ELECTION_RELEASE_ON_CANCEL
89+
value: "false"
90+
- name: METRICS_SECURE
91+
value: "true"
92+
- name: WEBHOOK_CERT_PATH
93+
value: ""
94+
- name: WEBHOOK_CERT_NAME
95+
value: "tls.crt"
96+
- name: WEBHOOK_CERT_KEY
97+
value: "tls.key"
98+
- name: METRICS_CERT_PATH
99+
value: ""
100+
- name: METRICS_CERT_NAME
101+
value: "tls.crt"
102+
- name: METRICS_CERT_KEY
103+
value: "tls.key"
104+
- name: ENABLE_HTTP2
105+
value: "false"
106+
- name: CONFIG_FILE
107+
value: ""
55108
image: ghcr.io/datum-cloud/datum:latest
56109
name: datum-controller-manager
57110
ports: []

0 commit comments

Comments
 (0)