Skip to content

Commit 18dd587

Browse files
Jefftreek8s-publishing-bot
authored andcommitted
feedback: leasecandidate clients
Kubernetes-commit: fac758164029e278e9bda924090ed078bb6514c8
1 parent 1f27757 commit 18dd587

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

tools/leaderelection/leaderelection.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ type LeaderElectionConfig struct {
161161
Name string
162162

163163
// Coordinated will use the Coordinated Leader Election feature
164+
// WARNING: Coordinated leader election is ALPHA.
164165
Coordinated bool
165166
}
166167

@@ -293,6 +294,7 @@ func (le *LeaderElector) renew(ctx context.Context) {
293294
return
294295
}
295296
le.metrics.leaderOff(le.config.Name)
297+
klog.Infof("failed to renew lease %v: %v", desc, err)
296298
cancel()
297299
}, le.config.RetryPeriod, ctx.Done())
298300

@@ -354,23 +356,23 @@ func (le *LeaderElector) tryCoordinatedRenew(ctx context.Context) bool {
354356

355357
le.observedRawRecord = oldLeaderElectionRawRecord
356358
}
357-
hasExpired := le.observedTime.Add(time.Second * time.Duration(oldLeaderElectionRecord.LeaseDurationSeconds)).Before(now.Time)
358359

360+
hasExpired := le.observedTime.Add(time.Second * time.Duration(oldLeaderElectionRecord.LeaseDurationSeconds)).Before(now.Time)
359361
if hasExpired {
360362
klog.Infof("lock has expired: %v", le.config.Lock.Describe())
361363
return false
362364
}
363365

364366
if !le.IsLeader() {
365-
klog.V(4).Infof("lock is held by %v and has not yet expired: %v", oldLeaderElectionRecord.HolderIdentity, le.config.Lock.Describe())
367+
klog.V(6).Infof("lock is held by %v and has not yet expired: %v", oldLeaderElectionRecord.HolderIdentity, le.config.Lock.Describe())
366368
return false
367369
}
368370

369371
// 2b. If the lease has been marked as "end of term", don't renew it
370372
if le.IsLeader() && oldLeaderElectionRecord.PreferredHolder != "" {
371373
klog.V(4).Infof("lock is marked as 'end of term': %v", le.config.Lock.Describe())
372374
// TODO: Instead of letting lease expire, the holder may deleted it directly
373-
// This will not be compatible with all controllers, so it needs to be opt-in behavior..
375+
// This will not be compatible with all controllers, so it needs to be opt-in behavior.
374376
// We must ensure all code guarded by this lease has successfully completed
375377
// prior to releasing or there may be two processes
376378
// simultaneously acting on the critical path.

tools/leaderelection/leasecandidate.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,14 @@ type LeaseCandidate struct {
6363
preferredStrategies []v1.CoordinatedLeaseStrategy
6464
}
6565

66+
// NewCandidate creates new LeaseCandidate controller that creates a
67+
// LeaseCandidate object if it does not exist and watches changes
68+
// to the corresponding object and renews if PingTime is set.
69+
// WARNING: This is an ALPHA feature. Ensure that the CoordinatedLeaderElection
70+
// feature gate is on.
6671
func NewCandidate(clientset kubernetes.Interface,
67-
candidateName string,
6872
candidateNamespace string,
73+
candidateName string,
6974
targetLease string,
7075
binaryVersion, emulationVersion string,
7176
preferredStrategies []v1.CoordinatedLeaseStrategy,
@@ -144,7 +149,6 @@ func (c *LeaseCandidate) processNextWorkItem(ctx context.Context) bool {
144149
}
145150

146151
utilruntime.HandleError(err)
147-
klog.Infof("processNextWorkItem.AddRateLimited: %v", key)
148152
c.queue.AddRateLimited(key)
149153

150154
return true
@@ -161,17 +165,16 @@ func (c *LeaseCandidate) ensureLease(ctx context.Context) error {
161165
if apierrors.IsNotFound(err) {
162166
klog.V(2).Infof("Creating lease candidate")
163167
// lease does not exist, create it.
164-
leaseToCreate := c.newLease()
165-
_, err := c.leaseClient.Create(ctx, leaseToCreate, metav1.CreateOptions{})
166-
if err != nil {
168+
leaseToCreate := c.newLeaseCandidate()
169+
if _, err := c.leaseClient.Create(ctx, leaseToCreate, metav1.CreateOptions{}); err != nil {
167170
return err
168171
}
169172
klog.V(2).Infof("Created lease candidate")
170173
return nil
171174
} else if err != nil {
172175
return err
173176
}
174-
klog.V(2).Infof("lease candidate exists.. renewing")
177+
klog.V(2).Infof("lease candidate exists. Renewing.")
175178
clone := lease.DeepCopy()
176179
clone.Spec.RenewTime = &metav1.MicroTime{Time: c.clock.Now()}
177180
clone.Spec.PingTime = nil
@@ -182,8 +185,8 @@ func (c *LeaseCandidate) ensureLease(ctx context.Context) error {
182185
return nil
183186
}
184187

185-
func (c *LeaseCandidate) newLease() *v1alpha1.LeaseCandidate {
186-
lease := &v1alpha1.LeaseCandidate{
188+
func (c *LeaseCandidate) newLeaseCandidate() *v1alpha1.LeaseCandidate {
189+
lc := &v1alpha1.LeaseCandidate{
187190
ObjectMeta: metav1.ObjectMeta{
188191
Name: c.name,
189192
Namespace: c.namespace,
@@ -195,6 +198,6 @@ func (c *LeaseCandidate) newLease() *v1alpha1.LeaseCandidate {
195198
PreferredStrategies: c.preferredStrategies,
196199
},
197200
}
198-
lease.Spec.RenewTime = &metav1.MicroTime{Time: c.clock.Now()}
199-
return lease
201+
lc.Spec.RenewTime = &metav1.MicroTime{Time: c.clock.Now()}
202+
return lc
200203
}

tools/leaderelection/leasecandidate_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ func TestLeaseCandidateCreation(t *testing.T) {
4848
client := fake.NewSimpleClientset()
4949
candidate, _, err := NewCandidate(
5050
client,
51-
tc.candidateName,
5251
tc.candidateNamespace,
52+
tc.candidateName,
5353
tc.leaseName,
5454
tc.binaryVersion,
5555
tc.emulationVersion,

0 commit comments

Comments
 (0)