Skip to content

Commit

Permalink
Bug 1864352: Add leader election mechanism to release-4.5
Browse files Browse the repository at this point in the history
This cherry-picks the fix from master/4.6 and integrates with:
openshift/machine-api-operator#664

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.

(cherry picked from commit 5ccc992)
  • Loading branch information
andymcc committed Oct 12, 2020
1 parent 18593ad commit 53d0f66
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 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 All @@ -25,6 +26,25 @@ func main() {
watchNamespace := flag.String("namespace", "", "Namespace that the controller watches to reconcile machine-api objects. If unspecified, the controller watches for machine-api objects across all namespaces.")
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 @@ -40,7 +60,12 @@ func main() {
glog.Fatal(err)
}

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

if *watchNamespace != "" {
opts.Namespace = *watchNamespace
Expand Down

0 comments on commit 53d0f66

Please sign in to comment.