From 500bfe00cdbec423ebb23467d3d49967d4f8ea8f Mon Sep 17 00:00:00 2001 From: Keisuke Ishigami Date: Thu, 10 Jul 2025 02:59:21 +0900 Subject: [PATCH] Prevent the failure of releasing the lock by updating the resource version in case of a resource conflict Kubernetes-commit: 271233a62aeab02eb0f7a6567d3ddba6d09a4f21 --- tools/leaderelection/leaderelection.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/tools/leaderelection/leaderelection.go b/tools/leaderelection/leaderelection.go index c3c1d9be..35afc372 100644 --- a/tools/leaderelection/leaderelection.go +++ b/tools/leaderelection/leaderelection.go @@ -306,17 +306,37 @@ func (le *LeaderElector) renew(ctx context.Context) { // release attempts to release the leader lease if we have acquired it. func (le *LeaderElector) release() bool { + ctx := context.Background() + + // 1. obtain the electionRecord + oldLeaderElectionRecord, oldLeaderElectionRawRecord, err := le.config.Lock.Get(ctx) + if err != nil { + if !errors.IsNotFound(err) { + klog.Errorf("error retrieving resource lock %v: %v", le.config.Lock.Describe(), err) + return false + } + klog.Infof("lease lock not found: %v", le.config.Lock.Describe()) + return false + } + // 2. Record obtained, check the Identity & Time + if !bytes.Equal(le.observedRawRecord, oldLeaderElectionRawRecord) { + le.setObservedRecord(oldLeaderElectionRecord) + + le.observedRawRecord = oldLeaderElectionRawRecord + } + if !le.IsLeader() { return true } now := metav1.NewTime(le.clock.Now()) leaderElectionRecord := rl.LeaderElectionRecord{ - LeaderTransitions: le.observedRecord.LeaderTransitions, + LeaderTransitions: oldLeaderElectionRecord.LeaderTransitions, LeaseDurationSeconds: 1, RenewTime: now, AcquireTime: now, } - timeoutCtx, timeoutCancel := context.WithTimeout(context.Background(), le.config.RenewDeadline) + + timeoutCtx, timeoutCancel := context.WithTimeout(ctx, le.config.RenewDeadline) defer timeoutCancel() if err := le.config.Lock.Update(timeoutCtx, leaderElectionRecord); err != nil { klog.Errorf("Failed to release lock: %v", err)