diff --git a/pkg/controlplane/controller/leaderelection/leaderelection_controller.go b/pkg/controlplane/controller/leaderelection/leaderelection_controller.go index 3b44c30e452..db5a99ce60b 100644 --- a/pkg/controlplane/controller/leaderelection/leaderelection_controller.go +++ b/pkg/controlplane/controller/leaderelection/leaderelection_controller.go @@ -75,7 +75,7 @@ type Controller struct { } func (c *Controller) Run(ctx context.Context, workers int) { - defer utilruntime.HandleCrash() + defer utilruntime.HandleCrashWithContext(ctx) defer c.queue.ShutDown() defer func() { err := c.leaseInformer.Informer().RemoveEventHandler(c.leaseRegistration) @@ -95,7 +95,7 @@ func (c *Controller) Run(ctx context.Context, workers int) { // This controller is leader elected and may start after informers have already started. List on startup. lcs, err := c.leaseCandidateInformer.Lister().List(labels.Everything()) if err != nil { - utilruntime.HandleError(err) + utilruntime.HandleErrorWithContext(ctx, err, "Failed to list lease candidates") return } for _, lc := range lcs { @@ -166,7 +166,7 @@ func (c *Controller) processNextElectionItem(ctx context.Context) bool { } intervalForRequeue, err := c.reconcileElectionStep(ctx, key) - utilruntime.HandleError(err) + utilruntime.HandleErrorWithContext(ctx, err, "Failed to reconcile election step") if intervalForRequeue != noRequeue { defer c.queue.AddAfter(key, intervalForRequeue) } diff --git a/pkg/controlplane/controller/leaderelection/leasecandidategc_controller.go b/pkg/controlplane/controller/leaderelection/leasecandidategc_controller.go index bcc4f89fdca..f3d5449e061 100644 --- a/pkg/controlplane/controller/leaderelection/leasecandidategc_controller.go +++ b/pkg/controlplane/controller/leaderelection/leasecandidategc_controller.go @@ -18,7 +18,6 @@ package leaderelection import ( "context" - "fmt" "time" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -61,13 +60,13 @@ func NewLeaseCandidateGC(clientset kubernetes.Interface, gcCheckPeriod time.Dura // Run starts one worker. func (c *LeaseCandidateGCController) Run(ctx context.Context) { - defer utilruntime.HandleCrash() + defer utilruntime.HandleCrashWithContext(ctx) defer klog.Infof("Shutting down apiserver leasecandidate garbage collector") klog.Infof("Starting apiserver leasecandidate garbage collector") - if !cache.WaitForCacheSync(ctx.Done(), c.leaseCandidatesSynced) { - utilruntime.HandleError(fmt.Errorf("timed out waiting for caches to sync")) + if !cache.WaitForNamedCacheSyncWithContext(ctx, c.leaseCandidatesSynced) { + utilruntime.HandleErrorWithContext(ctx, nil, "Timed out waiting for caches to sync") return } diff --git a/pkg/controlplane/controller/leaderelection/run_with_leaderelection.go b/pkg/controlplane/controller/leaderelection/run_with_leaderelection.go index 2593bc76423..cca4a547a3d 100644 --- a/pkg/controlplane/controller/leaderelection/run_with_leaderelection.go +++ b/pkg/controlplane/controller/leaderelection/run_with_leaderelection.go @@ -50,7 +50,7 @@ func RunWithLeaderElection(ctx context.Context, config *rest.Config, newRunnerFn } identity := hostname + "_" + string(uuid.NewUUID()) - wait.Until(func() { + wait.UntilWithContext(ctx, func(ctx context.Context) { callbacks := leaderelection.LeaderCallbacks{ OnStartedLeading: func(ctx context.Context) { var err error @@ -94,5 +94,5 @@ func RunWithLeaderElection(ctx context.Context, config *rest.Config, newRunnerFn return } le.Run(ctx) - }, timers.RetryPeriod, ctx.Done()) + }, timers.RetryPeriod) }