Prevent the failure of releasing the lock by updating the resource version in case of a resource conflict

Kubernetes-commit: 271233a62aeab02eb0f7a6567d3ddba6d09a4f21
This commit is contained in:
Keisuke Ishigami 2025-07-10 02:59:21 +09:00 committed by Kubernetes Publisher
parent 715e46ed57
commit 500bfe00cd

View File

@ -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)