@@ -20,6 +20,7 @@ import (
2020 "errors"
2121 "fmt"
2222 "os"
23+ "time"
2324
2425 "k8s.io/apimachinery/pkg/util/uuid"
2526 coordinationv1client "k8s.io/client-go/kubernetes/typed/coordination/v1"
@@ -49,6 +50,12 @@ type Options struct {
4950 // LeaderElectionID determines the name of the resource that leader election
5051 // will use for holding the leader lock.
5152 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
5259}
5360
5461// 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
8895
8996 // Construct clients for leader election
9097 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+
91112 corev1Client , err := corev1client .NewForConfig (config )
92113 if err != nil {
93114 return nil , err
@@ -97,7 +118,6 @@ func NewResourceLock(config *rest.Config, recorderProvider recorder.Provider, op
97118 if err != nil {
98119 return nil , err
99120 }
100-
101121 return resourcelock .New (options .LeaderElectionResourceLock ,
102122 options .LeaderElectionNamespace ,
103123 options .LeaderElectionID ,
@@ -106,7 +126,8 @@ func NewResourceLock(config *rest.Config, recorderProvider recorder.Provider, op
106126 resourcelock.ResourceLockConfig {
107127 Identity : id ,
108128 EventRecorder : recorderProvider .GetEventRecorderFor (id ),
109- })
129+ },
130+ )
110131}
111132
112133func getInClusterNamespace () (string , error ) {
0 commit comments