Review feedback: fix context handling in LeaseCandidateGCController

Signed-off-by: Dr. Stefan Schimanski <stefan.schimanski@gmail.com>
This commit is contained in:
Dr. Stefan Schimanski 2024-07-23 20:51:37 +02:00 committed by Jefftree
parent 15affefcab
commit a738daa88a
2 changed files with 8 additions and 7 deletions

View File

@ -21,6 +21,7 @@ import (
"fmt" "fmt"
"time" "time"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/labels"
utilruntime "k8s.io/apimachinery/pkg/util/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@ -55,23 +56,23 @@ func NewLeaseCandidateGC(clientset kubernetes.Interface, gcCheckPeriod time.Dura
} }
// Run starts one worker. // Run starts one worker.
func (c *LeaseCandidateGCController) Run(stopCh <-chan struct{}) { func (c *LeaseCandidateGCController) Run(ctx context.Context) {
defer utilruntime.HandleCrash() defer utilruntime.HandleCrash()
defer klog.Infof("Shutting down apiserver leasecandidate garbage collector") defer klog.Infof("Shutting down apiserver leasecandidate garbage collector")
klog.Infof("Starting 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")) utilruntime.HandleError(fmt.Errorf("timed out waiting for caches to sync"))
return 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()) lcs, err := c.leaseCandidateLister.List(labels.Everything())
if err != nil { if err != nil {
klog.ErrorS(err, "Error while listing lease candidates") klog.ErrorS(err, "Error while listing lease candidates")
@ -92,7 +93,7 @@ func (c *LeaseCandidateGCController) gc() {
continue continue
} }
if err := c.kubeclientset.CoordinationV1alpha1().LeaseCandidates(lc.Namespace).Delete( 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") klog.ErrorS(err, "Error deleting lease")
} }
} }

View File

@ -128,7 +128,7 @@ func TestLeaseCandidateGCController(t *testing.T) {
cache.WaitForCacheSync(ctx.Done(), controller.leaseCandidatesSynced) 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) { 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{}) lcs, err := client.CoordinationV1alpha1().LeaseCandidates("default").List(ctx, metav1.ListOptions{})
if err != nil { if err != nil {