mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-02-22 07:03:28 +00:00
controller/nodeipam: Improve goroutine mgmt
Make sure all threads are terminated when Run returns.
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user