diff --git a/cmd/main.go b/cmd/main.go index 243c17a2..9f2d7ac5 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -19,6 +19,7 @@ package main import ( "flag" "os" + "time" // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) // to ensure that exec-entrypoint and run can make use of them. @@ -52,18 +53,47 @@ func init() { } func main() { - var metricsAddr string - var enableLeaderElection bool - var probeAddr string - var qps float64 - var burst int + var ( + metricsAddr string + probeAddr string + qps float64 + burst int + + // leader election + enableLeaderElection bool + leaderElectLeaseDuration time.Duration + leaderElectRenewDeadline time.Duration + leaderElectRetryPeriod time.Duration + leaderElectResourceLock string + leaderElectionID string + leaderElectResourceNamespace string + ) + 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.Float64Var(&qps, "kube-api-qps", 500, "Maximum QPS to use while talking with Kubernetes API") flag.IntVar(&burst, "kube-api-burst", 500, "Maximum burst for throttle while talking with Kubernetes API") + flag.BoolVar(&enableLeaderElection, "leader-elect", true, + "Enable leader election for controller manager. "+ + "Enabling this will ensure there is only one active controller manager.") + flag.DurationVar(&leaderElectLeaseDuration, "leader-elect-lease-duration", 15*time.Second, + "The duration that non-leader candidates will wait after observing a leadership renewal until attempting to acquire "+ + "leadership of a led but unrenewed leader slot. This is effectively the maximum duration that a leader can be stopped"+ + " before it is replaced by another candidate. This is only applicable if leader election is enabled.") + flag.DurationVar(&leaderElectRenewDeadline, "leader-elect-renew-deadline", 10*time.Second, + "The interval between attempts by the acting master to renew a leadership slot before it stops leading. This"+ + "must be less than or equal to the lease duration. This is only applicable if leader election is enabled.") + flag.DurationVar(&leaderElectRetryPeriod, "leader-elect-retry-period", 2*time.Second, + "The duration the clients should wait between attempting acquisition and renewal of a leadership. This is only"+ + "applicable if leader election is enabled.") + flag.StringVar(&leaderElectResourceLock, "leader-elect-resource-lock", "leases", + "The type of resource object that is used for locking during leader election. Supported options are "+ + "'endpoints', 'configmaps', 'leases', 'endpointsleases' and 'configmapsleases'") + flag.StringVar(&leaderElectionID, "leader-elect-resource-name", "b8b2488c.x-k8s.io", + "The name of resource object that is used for locking during leader election. ") + flag.StringVar(&leaderElectResourceNamespace, "leader-elect-resource-namespace", "lws-system", + "The namespace of resource object that is used for locking during leader election.") + opts := zap.Options{ Development: true, } @@ -77,11 +107,16 @@ func main() { kubeConfig.Burst = burst mgr, err := ctrl.NewManager(kubeConfig, ctrl.Options{ - Scheme: scheme, - Metrics: metricsserver.Options{BindAddress: metricsAddr}, - HealthProbeBindAddress: probeAddr, - LeaderElection: enableLeaderElection, - LeaderElectionID: "b8b2488c.x-k8s.io", + Scheme: scheme, + Metrics: metricsserver.Options{BindAddress: metricsAddr}, + HealthProbeBindAddress: probeAddr, + LeaderElection: enableLeaderElection, + LeaderElectionID: leaderElectionID, + LeaderElectionResourceLock: leaderElectResourceLock, + LeaderElectionNamespace: leaderElectResourceNamespace, + LeaseDuration: &leaderElectLeaseDuration, + RenewDeadline: &leaderElectRenewDeadline, + RetryPeriod: &leaderElectRetryPeriod, // 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 diff --git a/config/manager/manager.yaml b/config/manager/manager.yaml index 8316c5de..9e51cda2 100644 --- a/config/manager/manager.yaml +++ b/config/manager/manager.yaml @@ -28,7 +28,7 @@ spec: selector: matchLabels: control-plane: controller-manager - replicas: 1 + replicas: 2 template: metadata: annotations: