Skip to content

Commit f39791e

Browse files
authored
Merge pull request #412 from vikaschoudhary16/le-leaseduration-configurable
Make leader election lease duration configurable through manager options
2 parents b026aaa + b8b7071 commit f39791e

File tree

3 files changed

+92
-6
lines changed

3 files changed

+92
-6
lines changed

example_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"os"
23+
"time"
2324

2425
appsv1 "k8s.io/api/apps/v1"
2526
corev1 "k8s.io/api/core/v1"
@@ -59,6 +60,50 @@ func Example() {
5960
}
6061
}
6162

63+
// This example creates a simple application Controller that is configured for ReplicaSets and Pods.
64+
// This application controller will be running leader election with the provided configuration in the manager options.
65+
// If leader election configuration is not provided, controller runs leader election with default values.
66+
// Default values taken from: https://github.com/kubernetes/apiserver/blob/master/pkg/apis/config/v1alpha1/defaults.go
67+
// defaultLeaseDuration = 15 * time.Second
68+
// defaultRenewDeadline = 10 * time.Second
69+
// defaultRetryPeriod = 2 * time.Second
70+
//
71+
// * Create a new application for ReplicaSets that manages Pods owned by the ReplicaSet and calls into
72+
// ReplicaSetReconciler.
73+
//
74+
// * Start the application.
75+
// TODO(pwittrock): Update this example when we have better dependency injection support
76+
func Example_updateLeaderElectionDurations() {
77+
var log = controllers.Log.WithName("builder-examples")
78+
leaseDuration := 100 * time.Second
79+
renewDeadline := 80 * time.Second
80+
retryPeriod := 20 * time.Second
81+
manager, err := controllers.NewManager(controllers.GetConfigOrDie(), controllers.Options{
82+
LeaseDuration: &leaseDuration,
83+
RenewDeadline: &renewDeadline,
84+
RetryPeriod: &retryPeriod,
85+
})
86+
if err != nil {
87+
log.Error(err, "could not create manager")
88+
os.Exit(1)
89+
}
90+
91+
err = controllers.
92+
NewControllerManagedBy(manager). // Create the Controller
93+
For(&appsv1.ReplicaSet{}). // ReplicaSet is the Application API
94+
Owns(&corev1.Pod{}). // ReplicaSet owns Pods created by it
95+
Complete(&ReplicaSetReconciler{Client: manager.GetClient()})
96+
if err != nil {
97+
log.Error(err, "could not create controller")
98+
os.Exit(1)
99+
}
100+
101+
if err := manager.Start(controllers.SetupSignalHandler()); err != nil {
102+
log.Error(err, "could not start manager")
103+
os.Exit(1)
104+
}
105+
}
106+
62107
// ReplicaSetReconciler is a simple Controller example implementation.
63108
type ReplicaSetReconciler struct {
64109
client.Client

pkg/manager/internal.go

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

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

4653
type controllerManager struct {
@@ -102,6 +109,16 @@ type controllerManager struct {
102109
host string
103110

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

107124
// Add sets dependencies on i, and adds it to the list of runnables to start.
@@ -289,12 +306,10 @@ func (cm *controllerManager) start() {
289306

290307
func (cm *controllerManager) startLeaderElection() (err error) {
291308
l, err := leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{
292-
Lock: cm.resourceLock,
293-
// Values taken from: https://github.com/kubernetes/apiserver/blob/master/pkg/apis/config/v1alpha1/defaults.go
294-
// TODO(joelspeed): These timings should be configurable
295-
LeaseDuration: 15 * time.Second,
296-
RenewDeadline: 10 * time.Second,
297-
RetryPeriod: 2 * time.Second,
309+
Lock: cm.resourceLock,
310+
LeaseDuration: cm.leaseDuration,
311+
RenewDeadline: cm.renewDeadline,
312+
RetryPeriod: cm.retryPeriod,
298313
Callbacks: leaderelection.LeaderCallbacks{
299314
OnStartedLeading: func(_ context.Context) {
300315
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
//
@@ -247,6 +258,9 @@ func New(config *rest.Config, options Options) (Manager, error) {
247258
internalStopper: stop,
248259
port: options.Port,
249260
host: options.Host,
261+
leaseDuration: *options.LeaseDuration,
262+
renewDeadline: *options.RenewDeadline,
263+
retryPeriod: *options.RetryPeriod,
250264
}, nil
251265
}
252266

@@ -302,6 +316,18 @@ func setOptionsDefaults(options Options) Options {
302316
if options.newMetricsListener == nil {
303317
options.newMetricsListener = metrics.NewListener
304318
}
319+
leaseDuration, renewDeadline, retryPeriod := defaultLeaseDuration, defaultRenewDeadline, defaultRetryPeriod
320+
if options.LeaseDuration == nil {
321+
options.LeaseDuration = &leaseDuration
322+
}
323+
324+
if options.RenewDeadline == nil {
325+
options.RenewDeadline = &renewDeadline
326+
}
327+
328+
if options.RetryPeriod == nil {
329+
options.RetryPeriod = &retryPeriod
330+
}
305331

306332
return options
307333
}

0 commit comments

Comments
 (0)