From c9e1c103ee524517fd1e1eacf7c753ea2310f6df Mon Sep 17 00:00:00 2001 From: Vince Prignano Date: Thu, 21 Jan 2021 10:19:38 -0800 Subject: [PATCH] :bug: Manager.Elected() should beclosed after runnables are started During debugging a weird issue with some tests failing if they were running too fast. In other words, calling Start() and Close() on a manager too fast throws an error where the informers haven't been able to sync, which then makes Start() fail with an error. In an effort to improve this behavior, tried to use Elected() to wait for leader election to start, which also waits for the cache. During some debugging, this issue happened again and upon digging a bit more it seems that the channel was closed before starting the runnables in some cases. This change reorders the close on cm.elected, which should fix the above issue. Signed-off-by: Vince Prignano --- pkg/manager/internal.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/manager/internal.go b/pkg/manager/internal.go index 78202943f1..af4eb2d072 100644 --- a/pkg/manager/internal.go +++ b/pkg/manager/internal.go @@ -473,8 +473,8 @@ func (cm *controllerManager) Start(ctx context.Context) (err error) { } } else { // Treat not having leader election enabled the same as being elected. + cm.startLeaderElectionRunnables() close(cm.elected) - go cm.startLeaderElectionRunnables() } }() @@ -632,8 +632,8 @@ func (cm *controllerManager) startLeaderElection() (err error) { RetryPeriod: cm.retryPeriod, Callbacks: leaderelection.LeaderCallbacks{ OnStartedLeading: func(_ context.Context) { - close(cm.elected) cm.startLeaderElectionRunnables() + close(cm.elected) }, OnStoppedLeading: cm.onStoppedLeading, },