@@ -56,14 +56,17 @@ type Options struct {
56
56
// Without that, a single slow response from the API server can result
57
57
// in losing leadership.
58
58
RenewDeadline time.Duration
59
+
60
+ // LeaderLabels are an optional set of labels that will be set on the lease object
61
+ // when this replica becomes leader
62
+ LeaderLabels map [string ]string
59
63
}
60
64
61
65
// NewResourceLock creates a new resource lock for use in a leader election loop.
62
66
func NewResourceLock (config * rest.Config , recorderProvider recorder.Provider , options Options ) (resourcelock.Interface , error ) {
63
67
if ! options .LeaderElection {
64
68
return nil , nil
65
69
}
66
-
67
70
// Default resource lock to "leases". The previous default (from v0.7.0 to v0.11.x) was configmapsleases, which was
68
71
// used to migrate from configmaps to leases. Since the default was "configmapsleases" for over a year, spanning
69
72
// five minor releases, any actively maintained operators are very likely to have a released version that uses
@@ -93,22 +96,21 @@ func NewResourceLock(config *rest.Config, recorderProvider recorder.Provider, op
93
96
}
94
97
id = id + "_" + string (uuid .NewUUID ())
95
98
96
- // Construct clients for leader election
97
- rest .AddUserAgent (config , "leader-election" )
99
+ // Construct config for leader election
100
+ config = rest .AddUserAgent (config , "leader-election" )
98
101
102
+ // Timeout set for a client used to contact to Kubernetes should be lower than
103
+ // RenewDeadline to keep a single hung request from forcing a leader loss.
104
+ // Setting it to max(time.Second, RenewDeadline/2) as a reasonable heuristic.
99
105
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
- )
106
+ timeout := options .RenewDeadline / 2
107
+ if timeout < time .Second {
108
+ timeout = time .Second
109
+ }
110
+ config .Timeout = timeout
110
111
}
111
112
113
+ // Construct clients for leader election
112
114
corev1Client , err := corev1client .NewForConfig (config )
113
115
if err != nil {
114
116
return nil , err
@@ -118,7 +120,8 @@ func NewResourceLock(config *rest.Config, recorderProvider recorder.Provider, op
118
120
if err != nil {
119
121
return nil , err
120
122
}
121
- return resourcelock .New (options .LeaderElectionResourceLock ,
123
+
124
+ return resourcelock .NewWithLabels (options .LeaderElectionResourceLock ,
122
125
options .LeaderElectionNamespace ,
123
126
options .LeaderElectionID ,
124
127
corev1Client ,
@@ -127,6 +130,7 @@ func NewResourceLock(config *rest.Config, recorderProvider recorder.Provider, op
127
130
Identity : id ,
128
131
EventRecorder : recorderProvider .GetEventRecorderFor (id ),
129
132
},
133
+ options .LeaderLabels ,
130
134
)
131
135
}
132
136
0 commit comments