@@ -20,6 +20,7 @@ import (
20
20
"errors"
21
21
"fmt"
22
22
"os"
23
+ "time"
23
24
24
25
"k8s.io/apimachinery/pkg/util/uuid"
25
26
coordinationv1client "k8s.io/client-go/kubernetes/typed/coordination/v1"
@@ -49,6 +50,12 @@ type Options struct {
49
50
// LeaderElectionID determines the name of the resource that leader election
50
51
// will use for holding the leader lock.
51
52
LeaderElectionID string
53
+
54
+ // RenewDeadline is the renew deadline for this leader election client.
55
+ // Must be set to ensure the resource lock has an appropriate client timeout.
56
+ // Without that, a single slow response from the API server can result
57
+ // in losing leadership.
58
+ RenewDeadline time.Duration
52
59
}
53
60
54
61
// NewResourceLock creates a new resource lock for use in a leader election loop.
@@ -88,6 +95,20 @@ func NewResourceLock(config *rest.Config, recorderProvider recorder.Provider, op
88
95
89
96
// Construct clients for leader election
90
97
rest .AddUserAgent (config , "leader-election" )
98
+
99
+ if options .RenewDeadline != 0 {
100
+ return resourcelock .NewFromKubeconfig (options .LeaderElectionResourceLock ,
101
+ options .LeaderElectionNamespace ,
102
+ options .LeaderElectionID ,
103
+ resourcelock.ResourceLockConfig {
104
+ Identity : id ,
105
+ EventRecorder : recorderProvider .GetEventRecorderFor (id ),
106
+ },
107
+ config ,
108
+ options .RenewDeadline ,
109
+ )
110
+ }
111
+
91
112
corev1Client , err := corev1client .NewForConfig (config )
92
113
if err != nil {
93
114
return nil , err
@@ -97,7 +118,6 @@ func NewResourceLock(config *rest.Config, recorderProvider recorder.Provider, op
97
118
if err != nil {
98
119
return nil , err
99
120
}
100
-
101
121
return resourcelock .New (options .LeaderElectionResourceLock ,
102
122
options .LeaderElectionNamespace ,
103
123
options .LeaderElectionID ,
@@ -106,7 +126,8 @@ func NewResourceLock(config *rest.Config, recorderProvider recorder.Provider, op
106
126
resourcelock.ResourceLockConfig {
107
127
Identity : id ,
108
128
EventRecorder : recorderProvider .GetEventRecorderFor (id ),
109
- })
129
+ },
130
+ )
110
131
}
111
132
112
133
func getInClusterNamespace () (string , error ) {
0 commit comments