Skip to content

Commit

Permalink
Tune leader election (#221)
Browse files Browse the repository at this point in the history
Extends the leader election defaults and exposes them as CLI flags.

Also causes the processes to release the lease when shutting down
gracefully to avoid waiting the full lease TTL for a new leader to be
elected.

---------

Co-authored-by: Jordan Olshevski <[email protected]>
  • Loading branch information
jveski and Jordan Olshevski authored Oct 17, 2024
1 parent 0835f9d commit 22d2870
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 7 additions & 4 deletions internal/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,13 @@ func newMgr(logger logr.Logger, opts *Options, isController, isReconciler bool)
Cache: cache.Options{
ByObject: make(map[client.Object]cache.ByObject),
},
LeaderElection: opts.LeaderElection,
LeaderElectionNamespace: opts.LeaderElectionNamespace,
LeaderElectionResourceLock: opts.LeaderElectionResourceLock,
LeaderElectionID: opts.LeaderElectionID,
LeaderElection: opts.LeaderElection,
LeaderElectionNamespace: opts.LeaderElectionNamespace,
LeaderElectionResourceLock: opts.LeaderElectionResourceLock,
LeaderElectionID: opts.LeaderElectionID,
LeaseDuration: &opts.ElectionLeaseDuration,
RenewDeadline: &opts.ElectionLeaseRenewDeadline,
LeaderElectionReleaseOnCancel: true,
}

if isController {
Expand Down
6 changes: 6 additions & 0 deletions internal/manager/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package manager
import (
"flag"
"os"
"time"

"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/rest"
Expand All @@ -11,6 +12,9 @@ import (

type Options struct {
leaderelection.Options
ElectionLeaseDuration time.Duration
ElectionLeaseRenewDeadline time.Duration

Rest *rest.Config
HealthProbeAddr string
MetricsAddr string
Expand All @@ -31,4 +35,6 @@ func (o *Options) Bind(set *flag.FlagSet) {
set.StringVar(&o.LeaderElectionNamespace, "leader-election-namespace", os.Getenv("POD_NAMESPACE"), "Determines the namespace in which the leader election resource will be created")
set.StringVar(&o.LeaderElectionResourceLock, "leader-election-resource-lock", "", "Determines which resource lock to use for leader election")
set.StringVar(&o.LeaderElectionID, "leader-election-id", "", "Determines the name of the resource that leader election will use for holding the leader lock")
set.DurationVar(&o.ElectionLeaseDuration, "leader-election-lease-duration", time.Second*90, "")
set.DurationVar(&o.ElectionLeaseRenewDeadline, "leader-election-lease-renew-deadline", time.Second*60, "")
}

0 comments on commit 22d2870

Please sign in to comment.