Skip to content

Commit a9e062c

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

File tree

3 files changed

+58
-8
lines changed

3 files changed

+58
-8
lines changed

pkg/leaderelection/leader_election.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ type Options struct {
4343
// LeaderElectionID determines the name of the configmap that leader election
4444
// will use for holding the leader lock.
4545
LeaderElectionID string
46+
47+
// LeaseDurationSeconds is the duration that non-leader candidates will
48+
// wait to force acquire leadership.
49+
LeaseDurationSeconds int
50+
// RenewDeadlineSeconds is the duration that the acting master will retry
51+
// refreshing leadership before giving up.
52+
RenewDeadlineSeconds int
53+
// RetryPeriodSeconds is the duration the LeaderElector clients should wait
54+
// between tries of actions.
55+
RetryPeriodSeconds int
4656
}
4757

4858
// NewResourceLock creates a new config map resource lock for use in a leader
@@ -85,8 +95,11 @@ func NewResourceLock(config *rest.Config, recorderProvider recorder.Provider, op
8595
options.LeaderElectionID,
8696
client.CoreV1(),
8797
resourcelock.ResourceLockConfig{
88-
Identity: id,
89-
EventRecorder: recorderProvider.GetEventRecorderFor(id),
98+
Identity: id,
99+
LeaseDurationSeconds: options.LeaseDurationSeconds,
100+
RenewDeadlineSeconds: options.RenewDeadlineSeconds,
101+
RetryPeriodSeconds: options.RetryPeriodSeconds,
102+
EventRecorder: recorderProvider.GetEventRecorderFor(id),
90103
})
91104
}
92105

pkg/manager/internal.go

Lines changed: 29 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 {
@@ -286,13 +293,29 @@ func (cm *controllerManager) start() {
286293
}
287294

288295
func (cm *controllerManager) startLeaderElection() (err error) {
296+
297+
var leaseDuration, renewDeadline, retryPeriod int
298+
pLeaseDuration, pRenewDeadline, pRetryPeriod := cm.resourceLock.LeaderElectionConfig()
299+
if pLeaseDuration == nil || *pLeaseDuration == 0 {
300+
leaseDuration = defaultLeaseDuration
301+
} else {
302+
leaseDuration = *pLeaseDuration
303+
}
304+
if pRenewDeadline == nil || *pRenewDeadline == 0 {
305+
renewDeadline = defaultRenewDeadline
306+
} else {
307+
renewDeadline = *pRenewDeadline
308+
}
309+
if pRetryPeriod == nil || *pRetryPeriod == 0 {
310+
retryPeriod = defaultRetryPeriod
311+
} else {
312+
retryPeriod = *pRetryPeriod
313+
}
289314
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,
315+
Lock: cm.resourceLock,
316+
LeaseDuration: time.Duration(leaseDuration) * time.Second,
317+
RenewDeadline: time.Duration(renewDeadline) * time.Second,
318+
RetryPeriod: time.Duration(retryPeriod) * time.Second,
296319
Callbacks: leaderelection.LeaderCallbacks{
297320
OnStartedLeading: func(_ context.Context) {
298321
cm.start()

pkg/manager/manager.go

Lines changed: 14 additions & 0 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 int
120+
// RenewDeadline is the duration that the acting master will retry
121+
// refreshing leadership before giving up.
122+
RenewDeadlineSeconds int
123+
// RetryPeriod is the duration the LeaderElector clients should wait
124+
// between tries of actions.
125+
RetryPeriodSeconds int
126+
116127
// Namespace if specified restricts the manager's cache to watch objects in
117128
// the desired namespace Defaults to all namespaces
118129
//
@@ -217,6 +228,9 @@ func New(config *rest.Config, options Options) (Manager, error) {
217228
LeaderElection: options.LeaderElection,
218229
LeaderElectionID: options.LeaderElectionID,
219230
LeaderElectionNamespace: options.LeaderElectionNamespace,
231+
LeaseDurationSeconds: options.LeaseDurationSeconds,
232+
RenewDeadlineSeconds: options.RenewDeadlineSeconds,
233+
RetryPeriodSeconds: options.RetryPeriodSeconds,
220234
})
221235
if err != nil {
222236
return nil, err

0 commit comments

Comments
 (0)