From dfcadb4f89627e4bcdbdef5f55a5794dadf2f0cd Mon Sep 17 00:00:00 2001 From: Aditi Gupta Date: Tue, 9 Sep 2025 22:10:33 -0700 Subject: [PATCH] Replace WaitForNamedCacheSync with WaitForNamedCacheSyncWithContext in pkg/controller/garbagecollector --- .../garbagecollector/garbagecollector.go | 24 +++++++------------ 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/pkg/controller/garbagecollector/garbagecollector.go b/pkg/controller/garbagecollector/garbagecollector.go index a829714b4b9..54872a7c94e 100644 --- a/pkg/controller/garbagecollector/garbagecollector.go +++ b/pkg/controller/garbagecollector/garbagecollector.go @@ -146,7 +146,11 @@ func (gc *GarbageCollector) Run(ctx context.Context, workers int, initialSyncTim go gc.dependencyGraphBuilder.Run(ctx) - if !cache.WaitForNamedCacheSync("garbage collector", waitForStopOrTimeout(ctx.Done(), initialSyncTimeout), func() bool { + // Create a new context that is cancelled after the initialSyncTimeout. + syncCtx, cancel := context.WithTimeout(ctx, initialSyncTimeout) + defer cancel() + + if !cache.WaitForNamedCacheSyncWithContext(syncCtx, func() bool { return gc.dependencyGraphBuilder.IsSynced(logger) }) { logger.Info("Garbage collector: not all resource monitors could be synced, proceeding anyways") @@ -227,8 +231,11 @@ func (gc *GarbageCollector) Sync(ctx context.Context, discoveryClient discovery. } logger.V(4).Info("resynced monitors") + syncCtx, cancel := context.WithTimeout(ctx, period) + defer cancel() + // gc worker no longer waits for cache to be synced, but we will keep the periodical check to provide logs & metrics - cacheSynced := cache.WaitForNamedCacheSync("garbage collector", waitForStopOrTimeout(ctx.Done(), period), func() bool { + cacheSynced := cache.WaitForNamedCacheSyncWithContext(syncCtx, func() bool { return gc.dependencyGraphBuilder.IsSynced(logger) }) if cacheSynced { @@ -262,19 +269,6 @@ func printDiff(oldResources, newResources map[schema.GroupVersionResource]struct return fmt.Sprintf("added: %v, removed: %v", added.List(), removed.List()) } -// waitForStopOrTimeout returns a stop channel that closes when the provided stop channel closes or when the specified timeout is reached -func waitForStopOrTimeout(stopCh <-chan struct{}, timeout time.Duration) <-chan struct{} { - stopChWithTimeout := make(chan struct{}) - go func() { - select { - case <-stopCh: - case <-time.After(timeout): - } - close(stopChWithTimeout) - }() - return stopChWithTimeout -} - // IsSynced returns true if dependencyGraphBuilder is synced. func (gc *GarbageCollector) IsSynced(logger klog.Logger) bool { return gc.dependencyGraphBuilder.IsSynced(logger)