Skip to content

✨ Add an option to set ReleaseOnCancel for leader election #1128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions pkg/manager/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ type controllerManager struct {
// retryPeriod is the duration the LeaderElector clients should wait
// between tries of actions.
retryPeriod time.Duration
// releaseOnCancel specifies whether the lock should be released
// when the run context is cancelled.
releaseOnCancel bool

// waitForRunnable is holding the number of runnables currently running so that
// we can wait for them to exit before quitting the manager
Expand Down Expand Up @@ -629,10 +632,11 @@ func (cm *controllerManager) startLeaderElection() (err error) {
}
}
l, err := leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{
Lock: cm.resourceLock,
LeaseDuration: cm.leaseDuration,
RenewDeadline: cm.renewDeadline,
RetryPeriod: cm.retryPeriod,
Lock: cm.resourceLock,
ReleaseOnCancel: cm.releaseOnCancel,
LeaseDuration: cm.leaseDuration,
RenewDeadline: cm.renewDeadline,
RetryPeriod: cm.retryPeriod,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: func(_ context.Context) {
close(cm.elected)
Expand Down
4 changes: 4 additions & 0 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ type Options struct {
// RetryPeriod is the duration the LeaderElector clients should wait
// between tries of actions. Default is 2 seconds.
RetryPeriod *time.Duration
// ReleaseOnCancel specifies whether the lock should be released
// when the run context is cancelled. Default if False
ReleaseOnCancel bool

// Namespace if specified restricts the manager's cache to watch objects in
// the desired namespace Defaults to all namespaces
Expand Down Expand Up @@ -362,6 +365,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
leaseDuration: *options.LeaseDuration,
renewDeadline: *options.RenewDeadline,
retryPeriod: *options.RetryPeriod,
releaseOnCancel: options.ReleaseOnCancel,
healthProbeListener: healthProbeListener,
readinessEndpointName: options.ReadinessEndpointName,
livenessEndpointName: options.LivenessEndpointName,
Expand Down