From 364ceb6a25c075f782f20ec6ebb0d4d60c274ccc Mon Sep 17 00:00:00 2001 From: adel121 Date: Wed, 18 Dec 2024 12:16:35 +0100 Subject: [PATCH] quit leader election if context was cancelled --- .../apiserver/leaderelection/leaderelection.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/util/kubernetes/apiserver/leaderelection/leaderelection.go b/pkg/util/kubernetes/apiserver/leaderelection/leaderelection.go index 45ea62c6e2d26..79e8a89895beb 100644 --- a/pkg/util/kubernetes/apiserver/leaderelection/leaderelection.go +++ b/pkg/util/kubernetes/apiserver/leaderelection/leaderelection.go @@ -228,9 +228,15 @@ func (le *LeaderEngine) EnsureLeaderElectionRuns() error { func (le *LeaderEngine) runLeaderElection() { for { - log.Infof("Starting leader election process for %q...", le.HolderIdentity) - le.leaderElector.Run(le.ctx) - log.Info("Leader election lost") + select { + case <-le.ctx.Done(): + log.Infof("Quitting leader election process: context was cancelled") + return + default: + log.Infof("Starting leader election process for %q...", le.HolderIdentity) + le.leaderElector.Run(le.ctx) + log.Info("Leader election lost") + } } }