From 4379fade332a1832f1ac2e592d30e12ed714c157 Mon Sep 17 00:00:00 2001 From: aimuz Date: Fri, 30 Aug 2024 19:25:45 +0800 Subject: [PATCH] leaderelection: replace deprecated wait.PollImmediateUntil with wait.PollUntilContextTimeout This commit refactors the `renew` function in the leader election package to replace the deprecated `wait.PollImmediateUntil` with `wait.PollUntilContextTimeout`. This change simplifies the context handling by removing the need for an additional timeout context and its cancellation. The previous implementation created a timeout context for each retry period, which added unnecessary complexity. The new implementation directly passes the parent context to the retry function, ensuring that the timeout is handled within `PollUntilContextTimeout`. This change also ensures that the context passed to `tryAcquireOrRenew` and `tryCoordinatedRenew` is consistent, improving the readability and maintainability of the code. Kubernetes-commit: 3d2f498aa31cb2e90bcfc372b7930aed0604fbef --- tools/leaderelection/leaderelection.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/tools/leaderelection/leaderelection.go b/tools/leaderelection/leaderelection.go index 53a21e84..64c49f61 100644 --- a/tools/leaderelection/leaderelection.go +++ b/tools/leaderelection/leaderelection.go @@ -277,16 +277,13 @@ func (le *LeaderElector) renew(ctx context.Context) { ctx, cancel := context.WithCancel(ctx) defer cancel() wait.Until(func() { - timeoutCtx, timeoutCancel := context.WithTimeout(ctx, le.config.RenewDeadline) - defer timeoutCancel() - err := wait.PollImmediateUntil(le.config.RetryPeriod, func() (bool, error) { + err := wait.PollUntilContextTimeout(ctx, le.config.RetryPeriod, le.config.RenewDeadline, true, func(ctx context.Context) (done bool, err error) { if !le.config.Coordinated { - return le.tryAcquireOrRenew(timeoutCtx), nil + return le.tryAcquireOrRenew(ctx), nil } else { - return le.tryCoordinatedRenew(timeoutCtx), nil + return le.tryCoordinatedRenew(ctx), nil } - }, timeoutCtx.Done()) - + }) le.maybeReportTransition() desc := le.config.Lock.Describe() if err == nil {