Skip to content

Commit e7a24d7

Browse files
Make leader election lease duration configurable through manager options
1 parent 856708d commit e7a24d7

File tree

2 files changed

+67
-21
lines changed

2 files changed

+67
-21
lines changed

pkg/manager/internal.go

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ import (
4040
"sigs.k8s.io/controller-runtime/pkg/webhook"
4141
)
4242

43+
const (
44+
// Values taken from: https://github.com/kubernetes/apiserver/blob/master/pkg/apis/config/v1alpha1/defaults.go
45+
defaultLeaseDuration = 15
46+
defaultRenewDeadline = 10
47+
defaultRetryPeriod = 2
48+
)
49+
4350
var log = logf.RuntimeLog.WithName("manager")
4451

4552
type controllerManager struct {
@@ -101,6 +108,16 @@ type controllerManager struct {
101108
host string
102109

103110
webhookServer *webhook.Server
111+
112+
// leaseDurationSeconds is the duration that non-leader candidates will
113+
// wait to force acquire leadership.
114+
leaseDurationSeconds *time.Duration
115+
// renewDeadlineSeconds is the duration that the acting master will retry
116+
// refreshing leadership before giving up.
117+
renewDeadlineSeconds *time.Duration
118+
// retryPeriodSeconds is the duration the LeaderElector clients should wait
119+
// between tries of actions.
120+
retryPeriodSeconds *time.Duration
104121
}
105122

106123
// Add sets dependencies on i, and adds it to the list of runnables to start.
@@ -286,13 +303,28 @@ func (cm *controllerManager) start() {
286303
}
287304

288305
func (cm *controllerManager) startLeaderElection() (err error) {
306+
307+
var leaseDuration, renewDeadline, retryPeriod time.Duration
308+
if cm.leaseDurationSeconds == nil {
309+
leaseDuration = defaultLeaseDuration
310+
} else {
311+
leaseDuration = *cm.leaseDurationSeconds
312+
}
313+
if cm.renewDeadlineSeconds == nil {
314+
renewDeadline = defaultRenewDeadline
315+
} else {
316+
renewDeadline = *cm.renewDeadlineSeconds
317+
}
318+
if cm.retryPeriodSeconds == nil {
319+
retryPeriod = defaultRetryPeriod
320+
} else {
321+
retryPeriod = *cm.retryPeriodSeconds
322+
}
289323
l, err := leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{
290-
Lock: cm.resourceLock,
291-
// Values taken from: https://github.com/kubernetes/apiserver/blob/master/pkg/apis/config/v1alpha1/defaults.go
292-
// TODO(joelspeed): These timings should be configurable
293-
LeaseDuration: 15 * time.Second,
294-
RenewDeadline: 10 * time.Second,
295-
RetryPeriod: 2 * time.Second,
324+
Lock: cm.resourceLock,
325+
LeaseDuration: leaseDuration * time.Second,
326+
RenewDeadline: renewDeadline * time.Second,
327+
RetryPeriod: retryPeriod * time.Second,
296328
Callbacks: leaderelection.LeaderCallbacks{
297329
OnStartedLeading: func(_ context.Context) {
298330
cm.start()

pkg/manager/manager.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,17 @@ type Options struct {
113113
// will use for holding the leader lock.
114114
LeaderElectionID string
115115

116+
// LeaseDuration is the duration that non-leader candidates will
117+
// wait to force acquire leadership. This is measured against time of
118+
// last observed ack.
119+
LeaseDurationSeconds *time.Duration
120+
// RenewDeadline is the duration that the acting master will retry
121+
// refreshing leadership before giving up.
122+
RenewDeadlineSeconds *time.Duration
123+
// RetryPeriod is the duration the LeaderElector clients should wait
124+
// between tries of actions.
125+
RetryPeriodSeconds *time.Duration
126+
116127
// Namespace if specified restricts the manager's cache to watch objects in
117128
// the desired namespace Defaults to all namespaces
118129
//
@@ -232,21 +243,24 @@ func New(config *rest.Config, options Options) (Manager, error) {
232243
stop := make(chan struct{})
233244

234245
return &controllerManager{
235-
config: config,
236-
scheme: options.Scheme,
237-
errChan: make(chan error),
238-
cache: cache,
239-
fieldIndexes: cache,
240-
client: writeObj,
241-
apiReader: apiReader,
242-
recorderProvider: recorderProvider,
243-
resourceLock: resourceLock,
244-
mapper: mapper,
245-
metricsListener: metricsListener,
246-
internalStop: stop,
247-
internalStopper: stop,
248-
port: options.Port,
249-
host: options.Host,
246+
config: config,
247+
scheme: options.Scheme,
248+
errChan: make(chan error),
249+
cache: cache,
250+
fieldIndexes: cache,
251+
client: writeObj,
252+
apiReader: apiReader,
253+
recorderProvider: recorderProvider,
254+
resourceLock: resourceLock,
255+
mapper: mapper,
256+
metricsListener: metricsListener,
257+
internalStop: stop,
258+
internalStopper: stop,
259+
port: options.Port,
260+
host: options.Host,
261+
leaseDurationSeconds: options.LeaseDurationSeconds,
262+
renewDeadlineSeconds: options.RenewDeadlineSeconds,
263+
retryPeriodSeconds: options.RetryPeriodSeconds,
250264
}, nil
251265
}
252266

0 commit comments

Comments
 (0)