Skip to content

Commit

Permalink
Implement leader election for manager
Browse files Browse the repository at this point in the history
This adds integration with:
openshift/machine-api-operator#571

Adding 3 new cli arguments for configuring leader elections:
-leader-elect
-leader-elect-lease-duration int
-leader-elect-resource-namespace string

Using leader election will add stronger guarantees than we have today
that only one controller is running at a time to protect against edge
cases where the deployment replica could be increased or upgrades with
permissive maxSurge.
  • Loading branch information
andymcc committed Jul 15, 2020
1 parent 538d6e0 commit 5ccc992
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"flag"
"time"

"github.com/golang/glog"
"github.com/openshift/cluster-api-provider-libvirt/pkg/apis"
Expand Down Expand Up @@ -31,6 +32,25 @@ func main() {
)
klogFlags := flag.NewFlagSet("klog", flag.ExitOnError)
klog.InitFlags(klogFlags)

leaderElectResourceNamespace := flag.String(
"leader-elect-resource-namespace",
"",
"The namespace of resource object that is used for locking during leader election. If unspecified and running in cluster, defaults to the service account namespace for the controller. Required for leader-election outside of a cluster.",
)

leaderElect := flag.Bool(
"leader-elect",
false,
"Start a leader election client and gain leadership before executing the main loop. Enable this when running replicated components for high availability.",
)

leaderElectLeaseDuration := flag.Duration(
"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.Parse()
flag.VisitAll(func(f1 *flag.Flag) {
f2 := klogFlags.Lookup(f1.Name)
Expand All @@ -47,7 +67,11 @@ func main() {
}

opts := manager.Options{
HealthProbeBindAddress: *healthAddr,
LeaderElection: *leaderElect,
LeaderElectionNamespace: *leaderElectResourceNamespace,
LeaderElectionID: "cluster-api-provider-libvirt-leader",
LeaseDuration: leaderElectLeaseDuration,
HealthProbeBindAddress: *healthAddr,
}

if *watchNamespace != "" {
Expand Down

0 comments on commit 5ccc992

Please sign in to comment.