@@ -23,6 +23,8 @@ import (
23
23
"time"
24
24
25
25
"k8s.io/apimachinery/pkg/util/uuid"
26
+ coordinationv1client "k8s.io/client-go/kubernetes/typed/coordination/v1"
27
+ corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
26
28
"k8s.io/client-go/rest"
27
29
"k8s.io/client-go/tools/leaderelection/resourcelock"
28
30
@@ -49,7 +51,10 @@ type Options struct {
49
51
// will use for holding the leader lock.
50
52
LeaderElectionID string
51
53
52
- // RenewDeadline is the renew deadline for this leader election client
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.
53
58
RenewDeadline time.Duration
54
59
}
55
60
@@ -91,15 +96,37 @@ func NewResourceLock(config *rest.Config, recorderProvider recorder.Provider, op
91
96
// Construct clients for leader election
92
97
rest .AddUserAgent (config , "leader-election" )
93
98
94
- return resourcelock .NewFromKubeconfig (options .LeaderElectionResourceLock ,
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
+
112
+ corev1Client , err := corev1client .NewForConfig (config )
113
+ if err != nil {
114
+ return nil , err
115
+ }
116
+
117
+ coordinationClient , err := coordinationv1client .NewForConfig (config )
118
+ if err != nil {
119
+ return nil , err
120
+ }
121
+ return resourcelock .New (options .LeaderElectionResourceLock ,
95
122
options .LeaderElectionNamespace ,
96
123
options .LeaderElectionID ,
124
+ corev1Client ,
125
+ coordinationClient ,
97
126
resourcelock.ResourceLockConfig {
98
127
Identity : id ,
99
128
EventRecorder : recorderProvider .GetEventRecorderFor (id ),
100
129
},
101
- config ,
102
- options .RenewDeadline ,
103
130
)
104
131
}
105
132
0 commit comments