@@ -56,14 +56,17 @@ type Options struct {
5656// Without that, a single slow response from the API server can result
5757// in losing leadership.
5858RenewDeadline 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
5963}
6064
6165// NewResourceLock creates a new resource lock for use in a leader election loop.
6266func NewResourceLock (config * rest.Config , recorderProvider recorder.Provider , options Options ) (resourcelock.Interface , error ) {
6367if ! options .LeaderElection {
6468return nil , nil
6569}
66-
6770// Default resource lock to "leases". The previous default (from v0.7.0 to v0.11.x) was configmapsleases, which was
6871// used to migrate from configmaps to leases. Since the default was "configmapsleases" for over a year, spanning
6972// 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
9396}
9497id = id + "_" + string (uuid .NewUUID ())
9598
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" )
98101
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.
99105if 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
110111}
111112
113+ // Construct clients for leader election
112114corev1Client , err := corev1client .NewForConfig (config )
113115if err != nil {
114116return nil , err
@@ -118,7 +120,8 @@ func NewResourceLock(config *rest.Config, recorderProvider recorder.Provider, op
118120if err != nil {
119121return nil , err
120122}
121- return resourcelock .New (options .LeaderElectionResourceLock ,
123+
124+ return resourcelock .NewWithLabels (options .LeaderElectionResourceLock ,
122125options .LeaderElectionNamespace ,
123126options .LeaderElectionID ,
124127corev1Client ,
@@ -127,6 +130,7 @@ func NewResourceLock(config *rest.Config, recorderProvider recorder.Provider, op
127130Identity : id ,
128131EventRecorder : recorderProvider .GetEventRecorderFor (id ),
129132},
133+ options .LeaderLabels ,
130134)
131135}
132136
0 commit comments