Skip to content

Commit

Permalink
Merge pull request #163 from gujingit/feature/leader-elect
Browse files Browse the repository at this point in the history
add leader elect args
  • Loading branch information
k8s-ci-robot authored Jun 25, 2024
2 parents 557dfd8 + 8cd307b commit 034a888
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 14 deletions.
61 changes: 48 additions & 13 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ spec:
selector:
matchLabels:
control-plane: controller-manager
replicas: 1
replicas: 2
template:
metadata:
annotations:
Expand Down

0 comments on commit 034a888

Please sign in to comment.