Skip to content

Commit 64cbce7

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

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed

pkg/manager/internal.go

Lines changed: 21 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 * time.Second
46+
defaultRenewDeadline = 10 * time.Second
47+
defaultRetryPeriod = 2 * time.Second
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+
// leaseDuration is the duration that non-leader candidates will
113+
// wait to force acquire leadership.
114+
leaseDuration time.Duration
115+
// renewDeadline is the duration that the acting master will retry
116+
// refreshing leadership before giving up.
117+
renewDeadline time.Duration
118+
// retryPeriod is the duration the LeaderElector clients should wait
119+
// between tries of actions.
120+
retryPeriod time.Duration
104121
}
105122

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

288305
func (cm *controllerManager) startLeaderElection() (err error) {
289306
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,
307+
Lock: cm.resourceLock,
308+
LeaseDuration: cm.leaseDuration,
309+
RenewDeadline: cm.renewDeadline,
310+
RetryPeriod: cm.retryPeriod,
296311
Callbacks: leaderelection.LeaderCallbacks{
297312
OnStartedLeading: func(_ context.Context) {
298313
cm.start()

pkg/manager/manager.go

Lines changed: 26 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. Default is 15 seconds.
119+
LeaseDuration *time.Duration
120+
// RenewDeadline is the duration that the acting master will retry
121+
// refreshing leadership before giving up. Default is 10 seconds.
122+
RenewDeadline *time.Duration
123+
// RetryPeriod is the duration the LeaderElector clients should wait
124+
// between tries of actions. Default is 2 seconds.
125+
RetryPeriod *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
//
@@ -173,6 +184,7 @@ func (r RunnableFunc) Start(s <-chan struct{}) error {
173184
}
174185

175186
// New returns a new Manager for creating Controllers.
187+
// nolint: gocyclo
176188
func New(config *rest.Config, options Options) (Manager, error) {
177189
// Initialize a rest.config if none was specified
178190
if config == nil {
@@ -231,6 +243,17 @@ func New(config *rest.Config, options Options) (Manager, error) {
231243

232244
stop := make(chan struct{})
233245

246+
leaseDuration, renewDeadline, retryPeriod := defaultLeaseDuration, defaultRenewDeadline, defaultRetryPeriod
247+
if options.LeaseDuration == nil {
248+
leaseDuration = defaultLeaseDuration
249+
}
250+
if options.RenewDeadline == nil {
251+
renewDeadline = defaultRenewDeadline
252+
}
253+
if options.RetryPeriod == nil {
254+
retryPeriod = defaultRetryPeriod
255+
}
256+
234257
return &controllerManager{
235258
config: config,
236259
scheme: options.Scheme,
@@ -247,6 +270,9 @@ func New(config *rest.Config, options Options) (Manager, error) {
247270
internalStopper: stop,
248271
port: options.Port,
249272
host: options.Host,
273+
leaseDuration: leaseDuration,
274+
renewDeadline: renewDeadline,
275+
retryPeriod: retryPeriod,
250276
}, nil
251277
}
252278

0 commit comments

Comments
 (0)