controller/nodeipam: Improve goroutine mgmt

Make sure all threads are terminated when Run returns.
This commit is contained in:
Ondra Kupka
2025-10-27 14:50:46 +01:00
parent a265769245
commit 34e688eb3d
2 changed files with 15 additions and 10 deletions

View File

@@ -20,6 +20,7 @@ import (
"context"
"fmt"
"net"
"sync"
"time"
v1 "k8s.io/api/core/v1"
@@ -177,19 +178,24 @@ func (r *rangeAllocator) Run(ctx context.Context) {
r.broadcaster.StartRecordingToSink(&v1core.EventSinkImpl{Interface: r.client.CoreV1().Events("")})
defer r.broadcaster.Shutdown()
defer r.queue.ShutDown()
logger.Info("Starting range CIDR allocator")
defer logger.Info("Shutting down range CIDR allocator")
var wg sync.WaitGroup
defer func() {
logger.Info("Shutting down range CIDR allocator")
r.queue.ShutDown()
wg.Wait()
}()
if !cache.WaitForNamedCacheSyncWithContext(ctx, r.nodesSynced) {
return
}
for i := 0; i < cidrUpdateWorkers; i++ {
go wait.UntilWithContext(ctx, r.runWorker, time.Second)
wg.Go(func() {
wait.UntilWithContext(ctx, r.runWorker, time.Second)
})
}
<-ctx.Done()
}

View File

@@ -19,6 +19,8 @@ package nodeipam
import (
"context"
"fmt"
"net"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
coreinformers "k8s.io/client-go/informers/core/v1"
clientset "k8s.io/client-go/kubernetes"
@@ -30,7 +32,6 @@ import (
controllersmetrics "k8s.io/component-base/metrics/prometheus/controllers"
"k8s.io/klog/v2"
"k8s.io/kubernetes/pkg/controller/nodeipam/ipam"
"net"
)
// ipamController is an interface abstracting an interface for
@@ -146,12 +147,10 @@ func (nc *Controller) Run(ctx context.Context) {
}
if nc.allocatorType == ipam.IPAMFromClusterAllocatorType || nc.allocatorType == ipam.IPAMFromCloudAllocatorType {
go nc.legacyIPAM.Run(ctx)
nc.legacyIPAM.Run(ctx)
} else {
go nc.cidrAllocator.Run(ctx)
nc.cidrAllocator.Run(ctx)
}
<-ctx.Done()
}
// RunWithMetrics is a wrapper for Run that also tracks starting and stopping of the nodeipam controller with additional metric