mirror of
https://github.com/k3s-io/kubernetes.git
synced 2026-08-08 23:37:11 +00:00
controller/podautoscaler: Improve goroutine mgmt
Make sure all threads are terminated when Run returns.
This commit is contained in:
@@ -199,20 +199,26 @@ func NewHorizontalController(
|
||||
// Run begins watching and syncing.
|
||||
func (a *HorizontalController) Run(ctx context.Context, workers int) {
|
||||
defer utilruntime.HandleCrash()
|
||||
defer a.queue.ShutDown()
|
||||
|
||||
logger := klog.FromContext(ctx)
|
||||
logger.Info("Starting HPA controller")
|
||||
defer logger.Info("Shutting down HPA controller")
|
||||
|
||||
var wg sync.WaitGroup
|
||||
defer func() {
|
||||
logger.Info("Shutting down HPA controller")
|
||||
a.queue.ShutDown()
|
||||
wg.Wait()
|
||||
}()
|
||||
|
||||
if !cache.WaitForNamedCacheSyncWithContext(ctx, a.hpaListerSynced, a.podListerSynced) {
|
||||
return
|
||||
}
|
||||
|
||||
for i := 0; i < workers; i++ {
|
||||
go wait.UntilWithContext(ctx, a.worker, time.Second)
|
||||
wg.Go(func() {
|
||||
wait.UntilWithContext(ctx, a.worker, time.Second)
|
||||
})
|
||||
}
|
||||
|
||||
<-ctx.Done()
|
||||
}
|
||||
|
||||
|
||||
@@ -825,9 +825,14 @@ func coolCPUCreationTime() metav1.Time {
|
||||
|
||||
func (tc *testCase) runTestWithController(t *testing.T, hpaController *HorizontalController, informerFactory informers.SharedInformerFactory) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
informerFactory.Start(ctx.Done())
|
||||
go hpaController.Run(ctx, 5)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
wg.Go(func() {
|
||||
hpaController.Run(ctx, 5)
|
||||
})
|
||||
defer wg.Wait()
|
||||
defer cancel()
|
||||
|
||||
tc.Lock()
|
||||
shouldWait := tc.verifyEvents
|
||||
|
||||
Reference in New Issue
Block a user