Skip to content

Commit

Permalink
Make leader election lease duration configurable through manager options
Browse files Browse the repository at this point in the history
  • Loading branch information
vikaschoudhary16 committed Apr 30, 2019
1 parent 856708d commit 8abca02
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 21 deletions.
27 changes: 21 additions & 6 deletions pkg/manager/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

const (
// Values taken from: https://github.com/kubernetes/apiserver/blob/master/pkg/apis/config/v1alpha1/defaults.go
defaultLeaseDuration = 15
defaultRenewDeadline = 10
defaultRetryPeriod = 2
)

var log = logf.RuntimeLog.WithName("manager")

type controllerManager struct {
Expand Down Expand Up @@ -101,6 +108,16 @@ type controllerManager struct {
host string

webhookServer *webhook.Server

// leaseDurationSeconds is the duration that non-leader candidates will
// wait to force acquire leadership.
leaseDurationSeconds time.Duration
// renewDeadlineSeconds is the duration that the acting master will retry
// refreshing leadership before giving up.
renewDeadlineSeconds time.Duration
// retryPeriodSeconds is the duration the LeaderElector clients should wait
// between tries of actions.
retryPeriodSeconds time.Duration
}

// Add sets dependencies on i, and adds it to the list of runnables to start.
Expand Down Expand Up @@ -287,12 +304,10 @@ func (cm *controllerManager) start() {

func (cm *controllerManager) startLeaderElection() (err error) {
l, err := leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{
Lock: cm.resourceLock,
// Values taken from: https://github.com/kubernetes/apiserver/blob/master/pkg/apis/config/v1alpha1/defaults.go
// TODO(joelspeed): These timings should be configurable
LeaseDuration: 15 * time.Second,
RenewDeadline: 10 * time.Second,
RetryPeriod: 2 * time.Second,
Lock: cm.resourceLock,
LeaseDuration: cm.leaseDurationSeconds * time.Second,
RenewDeadline: cm.renewDeadlineSeconds * time.Second,
RetryPeriod: cm.retryPeriodSeconds * time.Second,
Callbacks: leaderelection.LeaderCallbacks{
OnStartedLeading: func(_ context.Context) {
cm.start()
Expand Down
55 changes: 40 additions & 15 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ type Options struct {
// will use for holding the leader lock.
LeaderElectionID string

// LeaseDuration is the duration that non-leader candidates will
// wait to force acquire leadership. This is measured against time of
// last observed ack.
LeaseDurationSeconds *time.Duration
// RenewDeadline is the duration that the acting master will retry
// refreshing leadership before giving up.
RenewDeadlineSeconds *time.Duration
// RetryPeriod is the duration the LeaderElector clients should wait
// between tries of actions.
RetryPeriodSeconds *time.Duration

// Namespace if specified restricts the manager's cache to watch objects in
// the desired namespace Defaults to all namespaces
//
Expand Down Expand Up @@ -231,22 +242,36 @@ func New(config *rest.Config, options Options) (Manager, error) {

stop := make(chan struct{})

leaseDuration, renewDeadline, retryPeriod := defaultLeaseDuration, defaultRenewDeadline, defaultRetryPeriod
if options.LeaseDurationSeconds == nil {
leaseDuration = defaultLeaseDuration
}
if options.RenewDeadlineSeconds == nil {
renewDeadline = defaultRenewDeadline
}
if options.RetryPeriodSeconds == nil {
retryPeriod = defaultRetryPeriod
}

return &controllerManager{
config: config,
scheme: options.Scheme,
errChan: make(chan error),
cache: cache,
fieldIndexes: cache,
client: writeObj,
apiReader: apiReader,
recorderProvider: recorderProvider,
resourceLock: resourceLock,
mapper: mapper,
metricsListener: metricsListener,
internalStop: stop,
internalStopper: stop,
port: options.Port,
host: options.Host,
config: config,
scheme: options.Scheme,
errChan: make(chan error),
cache: cache,
fieldIndexes: cache,
client: writeObj,
apiReader: apiReader,
recorderProvider: recorderProvider,
resourceLock: resourceLock,
mapper: mapper,
metricsListener: metricsListener,
internalStop: stop,
internalStopper: stop,
port: options.Port,
host: options.Host,
leaseDurationSeconds: leaseDuration,
renewDeadlineSeconds: renewDeadline,
retryPeriodSeconds: retryPeriod,
}, nil
}

Expand Down

0 comments on commit 8abca02

Please sign in to comment.