leasecandidate: Improve goroutine management

Make sure all goroutines are terminated when Run returns.

Kubernetes-commit: 498896ec4270b790e971a6fb01a292aa4c8cdfe0
This commit is contained in:
Ondra Kupka
2026-01-06 10:43:05 +01:00
committed by Kubernetes Publisher
parent d9d16a9e17
commit 0728b482e1

View File

@@ -19,6 +19,7 @@ package leaderelection
import (
"context"
"reflect"
"sync"
"time"
v1 "k8s.io/api/coordination/v1"
@@ -118,19 +119,25 @@ func NewCandidate(clientset kubernetes.Interface,
}
func (c *LeaseCandidate) Run(ctx context.Context) {
defer c.queue.ShutDown()
logger := klog.FromContext(ctx)
logger = klog.LoggerWithName(logger, "leasecandidate")
ctx = klog.NewContext(ctx, logger)
var wg sync.WaitGroup
defer func() {
c.queue.ShutDown()
wg.Wait()
}()
c.informerFactory.Start(ctx.Done())
if !cache.WaitForNamedCacheSyncWithContext(ctx, c.hasSynced) {
return
}
c.enqueueLease()
go c.runWorker(ctx)
wg.Go(func() {
c.runWorker(ctx)
})
<-ctx.Done()
}