diff --git a/pkg/controlplane/controller/leaderelection/leasecandidategc_controller.go b/pkg/controlplane/controller/leaderelection/leasecandidategc_controller.go index e8a7c4f7cc4..60659cd5393 100644 --- a/pkg/controlplane/controller/leaderelection/leasecandidategc_controller.go +++ b/pkg/controlplane/controller/leaderelection/leasecandidategc_controller.go @@ -21,6 +21,7 @@ import ( "fmt" "time" + apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/labels" utilruntime "k8s.io/apimachinery/pkg/util/runtime" @@ -55,23 +56,23 @@ func NewLeaseCandidateGC(clientset kubernetes.Interface, gcCheckPeriod time.Dura } // Run starts one worker. -func (c *LeaseCandidateGCController) Run(stopCh <-chan struct{}) { +func (c *LeaseCandidateGCController) Run(ctx context.Context) { defer utilruntime.HandleCrash() defer klog.Infof("Shutting down apiserver leasecandidate garbage collector") klog.Infof("Starting apiserver leasecandidate garbage collector") - if !cache.WaitForCacheSync(stopCh, c.leaseCandidatesSynced) { + if !cache.WaitForCacheSync(ctx.Done(), c.leaseCandidatesSynced) { utilruntime.HandleError(fmt.Errorf("timed out waiting for caches to sync")) return } - go wait.Until(c.gc, c.gcCheckPeriod, stopCh) + go wait.UntilWithContext(ctx, c.gc, c.gcCheckPeriod) - <-stopCh + <-ctx.Done() } -func (c *LeaseCandidateGCController) gc() { +func (c *LeaseCandidateGCController) gc(ctx context.Context) { lcs, err := c.leaseCandidateLister.List(labels.Everything()) if err != nil { klog.ErrorS(err, "Error while listing lease candidates") @@ -92,7 +93,7 @@ func (c *LeaseCandidateGCController) gc() { continue } if err := c.kubeclientset.CoordinationV1alpha1().LeaseCandidates(lc.Namespace).Delete( - context.TODO(), lc.Name, metav1.DeleteOptions{}); err != nil { + ctx, lc.Name, metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) { klog.ErrorS(err, "Error deleting lease") } } diff --git a/pkg/controlplane/controller/leaderelection/leasecandidategc_controller_test.go b/pkg/controlplane/controller/leaderelection/leasecandidategc_controller_test.go index 9eca831460d..b081f715e94 100644 --- a/pkg/controlplane/controller/leaderelection/leasecandidategc_controller_test.go +++ b/pkg/controlplane/controller/leaderelection/leasecandidategc_controller_test.go @@ -128,7 +128,7 @@ func TestLeaseCandidateGCController(t *testing.T) { cache.WaitForCacheSync(ctx.Done(), controller.leaseCandidatesSynced) - go controller.Run(ctx.Done()) + go controller.Run(ctx) err := wait.PollUntilContextTimeout(ctx, 100*time.Millisecond, 600*time.Second, true, func(ctx context.Context) (done bool, err error) { lcs, err := client.CoordinationV1alpha1().LeaseCandidates("default").List(ctx, metav1.ListOptions{}) if err != nil {