Skip to content

Commit a738daa

Browse files
stttsJefftree
authored andcommitted
Review feedback: fix context handling in LeaseCandidateGCController
Signed-off-by: Dr. Stefan Schimanski <[email protected]>
1 parent 15affef commit a738daa

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

pkg/controlplane/controller/leaderelection/leasecandidategc_controller.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222
"time"
2323

24+
apierrors "k8s.io/apimachinery/pkg/api/errors"
2425
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2526
"k8s.io/apimachinery/pkg/labels"
2627
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
@@ -55,23 +56,23 @@ func NewLeaseCandidateGC(clientset kubernetes.Interface, gcCheckPeriod time.Dura
5556
}
5657

5758
// Run starts one worker.
58-
func (c *LeaseCandidateGCController) Run(stopCh <-chan struct{}) {
59+
func (c *LeaseCandidateGCController) Run(ctx context.Context) {
5960
defer utilruntime.HandleCrash()
6061
defer klog.Infof("Shutting down apiserver leasecandidate garbage collector")
6162

6263
klog.Infof("Starting apiserver leasecandidate garbage collector")
6364

64-
if !cache.WaitForCacheSync(stopCh, c.leaseCandidatesSynced) {
65+
if !cache.WaitForCacheSync(ctx.Done(), c.leaseCandidatesSynced) {
6566
utilruntime.HandleError(fmt.Errorf("timed out waiting for caches to sync"))
6667
return
6768
}
6869

69-
go wait.Until(c.gc, c.gcCheckPeriod, stopCh)
70+
go wait.UntilWithContext(ctx, c.gc, c.gcCheckPeriod)
7071

71-
<-stopCh
72+
<-ctx.Done()
7273
}
7374

74-
func (c *LeaseCandidateGCController) gc() {
75+
func (c *LeaseCandidateGCController) gc(ctx context.Context) {
7576
lcs, err := c.leaseCandidateLister.List(labels.Everything())
7677
if err != nil {
7778
klog.ErrorS(err, "Error while listing lease candidates")
@@ -92,7 +93,7 @@ func (c *LeaseCandidateGCController) gc() {
9293
continue
9394
}
9495
if err := c.kubeclientset.CoordinationV1alpha1().LeaseCandidates(lc.Namespace).Delete(
95-
context.TODO(), lc.Name, metav1.DeleteOptions{}); err != nil {
96+
ctx, lc.Name, metav1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) {
9697
klog.ErrorS(err, "Error deleting lease")
9798
}
9899
}

pkg/controlplane/controller/leaderelection/leasecandidategc_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func TestLeaseCandidateGCController(t *testing.T) {
128128

129129
cache.WaitForCacheSync(ctx.Done(), controller.leaseCandidatesSynced)
130130

131-
go controller.Run(ctx.Done())
131+
go controller.Run(ctx)
132132
err := wait.PollUntilContextTimeout(ctx, 100*time.Millisecond, 600*time.Second, true, func(ctx context.Context) (done bool, err error) {
133133
lcs, err := client.CoordinationV1alpha1().LeaseCandidates("default").List(ctx, metav1.ListOptions{})
134134
if err != nil {

0 commit comments

Comments
 (0)