@@ -23,6 +23,8 @@ import (
2323 "time"
2424
2525 "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"
2628 "k8s.io/client-go/rest"
2729 "k8s.io/client-go/tools/leaderelection/resourcelock"
2830
@@ -49,7 +51,10 @@ type Options struct {
4951 // will use for holding the leader lock.
5052 LeaderElectionID string
5153
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.
5358 RenewDeadline time.Duration
5459}
5560
@@ -91,15 +96,37 @@ func NewResourceLock(config *rest.Config, recorderProvider recorder.Provider, op
9196 // Construct clients for leader election
9297 rest .AddUserAgent (config , "leader-election" )
9398
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 ,
95122 options .LeaderElectionNamespace ,
96123 options .LeaderElectionID ,
124+ corev1Client ,
125+ coordinationClient ,
97126 resourcelock.ResourceLockConfig {
98127 Identity : id ,
99128 EventRecorder : recorderProvider .GetEventRecorderFor (id ),
100129 },
101- config ,
102- options .RenewDeadline ,
103130 )
104131}
105132
0 commit comments