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" )
0 commit comments