Skip to content

Commit f5d202d

Browse files
alvaroalemank8s-infra-cherrypick-robot
authored andcommitted
warning: Use leader elector with client timeout
This change makes the leader elector use a client that internally has a smaller timeout than the renew deadline, which avoids a situation where a single request timing out makes us lose the leader lease.
1 parent 267b59e commit f5d202d

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

pkg/leaderelection/leader_election.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@ import (
2020
"errors"
2121
"fmt"
2222
"os"
23+
"time"
2324

2425
"k8s.io/apimachinery/pkg/util/uuid"
25-
coordinationv1client "k8s.io/client-go/kubernetes/typed/coordination/v1"
26-
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
2726
"k8s.io/client-go/rest"
2827
"k8s.io/client-go/tools/leaderelection/resourcelock"
2928

@@ -49,6 +48,9 @@ type Options struct {
4948
// LeaderElectionID determines the name of the resource that leader election
5049
// will use for holding the leader lock.
5150
LeaderElectionID string
51+
52+
// RewnewDeadline is the renew deadline for this leader election client
53+
RewnewDeadline time.Duration
5254
}
5355

5456
// NewResourceLock creates a new resource lock for use in a leader election loop.
@@ -88,25 +90,17 @@ func NewResourceLock(config *rest.Config, recorderProvider recorder.Provider, op
8890

8991
// Construct clients for leader election
9092
rest.AddUserAgent(config, "leader-election")
91-
corev1Client, err := corev1client.NewForConfig(config)
92-
if err != nil {
93-
return nil, err
94-
}
95-
96-
coordinationClient, err := coordinationv1client.NewForConfig(config)
97-
if err != nil {
98-
return nil, err
99-
}
10093

101-
return resourcelock.New(options.LeaderElectionResourceLock,
94+
return resourcelock.NewFromKubeconfig(options.LeaderElectionResourceLock,
10295
options.LeaderElectionNamespace,
10396
options.LeaderElectionID,
104-
corev1Client,
105-
coordinationClient,
10697
resourcelock.ResourceLockConfig{
10798
Identity: id,
10899
EventRecorder: recorderProvider.GetEventRecorderFor(id),
109-
})
100+
},
101+
config,
102+
options.RewnewDeadline,
103+
)
110104
}
111105

112106
func getInClusterNamespace() (string, error) {

pkg/manager/manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ func New(config *rest.Config, options Options) (Manager, error) {
389389
LeaderElectionResourceLock: options.LeaderElectionResourceLock,
390390
LeaderElectionID: options.LeaderElectionID,
391391
LeaderElectionNamespace: options.LeaderElectionNamespace,
392+
RewnewDeadline: *options.RenewDeadline,
392393
})
393394
if err != nil {
394395
return nil, err

pkg/manager/manager_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,28 @@ var _ = Describe("manger.Manager", func() {
316316
<-m2done
317317
})
318318

319+
It("should default RenewDeadline for leader election config", func() {
320+
var rl resourcelock.Interface
321+
m1, err := New(cfg, Options{
322+
LeaderElection: true,
323+
LeaderElectionNamespace: "default",
324+
LeaderElectionID: "test-leader-election-id",
325+
newResourceLock: func(config *rest.Config, recorderProvider recorder.Provider, options leaderelection.Options) (resourcelock.Interface, error) {
326+
if options.RewnewDeadline != 10*time.Second {
327+
return nil, fmt.Errorf("expected RenewDeadline to be 10s, got %v", options.RewnewDeadline)
328+
}
329+
var err error
330+
rl, err = leaderelection.NewResourceLock(config, recorderProvider, options)
331+
return rl, err
332+
},
333+
HealthProbeBindAddress: "0",
334+
Metrics: metricsserver.Options{BindAddress: "0"},
335+
PprofBindAddress: "0",
336+
})
337+
Expect(err).ToNot(HaveOccurred())
338+
Expect(m1).ToNot(BeNil())
339+
})
340+
319341
It("should default ID to controller-runtime if ID is not set", func() {
320342
var rl resourcelock.Interface
321343
m1, err := New(cfg, Options{

0 commit comments

Comments
 (0)