diff --git a/cmd/kube-controller-manager/app/core.go b/cmd/kube-controller-manager/app/core.go index 41ea7470715..b3637f273af 100644 --- a/cmd/kube-controller-manager/app/core.go +++ b/cmd/kube-controller-manager/app/core.go @@ -698,11 +698,12 @@ func startGarbageCollectorController(ctx context.Context, controllerContext Cont // Start the garbage collector. workers := int(controllerContext.ComponentConfig.GarbageCollectorController.ConcurrentGCSyncs) - go garbageCollector.Run(ctx, workers) + const syncPeriod = 30 * time.Second + go garbageCollector.Run(ctx, workers, syncPeriod) // Periodically refresh the RESTMapper with new discovery information and sync // the garbage collector. - go garbageCollector.Sync(ctx, discoveryClient, 30*time.Second) + go garbageCollector.Sync(ctx, discoveryClient, syncPeriod) return garbageCollector, true, nil } diff --git a/pkg/controller/garbagecollector/garbagecollector.go b/pkg/controller/garbagecollector/garbagecollector.go index b1c604919ba..e5e121c5dd9 100644 --- a/pkg/controller/garbagecollector/garbagecollector.go +++ b/pkg/controller/garbagecollector/garbagecollector.go @@ -129,7 +129,7 @@ func (gc *GarbageCollector) resyncMonitors(logger klog.Logger, deletableResource } // Run starts garbage collector workers. -func (gc *GarbageCollector) Run(ctx context.Context, workers int) { +func (gc *GarbageCollector) Run(ctx context.Context, workers int, initialSyncTimeout time.Duration) { defer utilruntime.HandleCrash() defer gc.attemptToDelete.ShutDown() defer gc.attemptToOrphan.ShutDown() @@ -146,7 +146,7 @@ func (gc *GarbageCollector) Run(ctx context.Context, workers int) { go gc.dependencyGraphBuilder.Run(ctx) - if !cache.WaitForNamedCacheSync("garbage collector", waitForStopOrTimeout(ctx.Done(), 30*time.Second), func() bool { + if !cache.WaitForNamedCacheSync("garbage collector", waitForStopOrTimeout(ctx.Done(), initialSyncTimeout), func() bool { return gc.dependencyGraphBuilder.IsSynced(logger) }) { logger.Info("Garbage collector: all resource monitors could not be synced, proceeding anyways") diff --git a/pkg/controller/garbagecollector/garbagecollector_test.go b/pkg/controller/garbagecollector/garbagecollector_test.go index 174bd838071..15e82a324ec 100644 --- a/pkg/controller/garbagecollector/garbagecollector_test.go +++ b/pkg/controller/garbagecollector/garbagecollector_test.go @@ -124,7 +124,7 @@ func TestGarbageCollectorConstruction(t *testing.T) { } assert.Len(t, gc.dependencyGraphBuilder.monitors, 1) - go gc.Run(tCtx, 1) + go gc.Run(tCtx, 1, 5*time.Second) err = gc.resyncMonitors(logger, twoResources) if err != nil { @@ -914,7 +914,8 @@ func TestGarbageCollectorSync(t *testing.T) { t.Fatal(err) } - go gc.Run(tCtx, 1) + syncPeriod := 200 * time.Millisecond + go gc.Run(tCtx, 1, syncPeriod) // The pseudo-code of GarbageCollector.Sync(): // GarbageCollector.Sync(client, period, stopCh): // wait.Until() loops with `period` until the `stopCh` is closed : @@ -929,7 +930,7 @@ func TestGarbageCollectorSync(t *testing.T) { // The 1s sleep in the test allows GetDeletableResources and // gc.resyncMonitors to run ~5 times to ensure the changes to the // fakeDiscoveryClient are picked up. - go gc.Sync(tCtx, fakeDiscoveryClient, 200*time.Millisecond) + go gc.Sync(tCtx, fakeDiscoveryClient, syncPeriod) // Wait until the sync discovers the initial resources time.Sleep(1 * time.Second) diff --git a/test/integration/garbagecollector/garbage_collector_test.go b/test/integration/garbagecollector/garbage_collector_test.go index 9e62cc50133..7202bbbaeb2 100644 --- a/test/integration/garbagecollector/garbage_collector_test.go +++ b/test/integration/garbagecollector/garbage_collector_test.go @@ -301,7 +301,7 @@ func setupWithServer(t *testing.T, result *kubeapiservertesting.TestServer, work // mapper, but we'll deal with it for now. restMapper.Reset() }, syncPeriod, tCtx.Done()) - go gc.Run(tCtx, workers) + go gc.Run(tCtx, workers, syncPeriod) go gc.Sync(tCtx, clientSet.Discovery(), syncPeriod) } @@ -1371,6 +1371,8 @@ func TestCascadingDeleteOnCRDConversionFailure(t *testing.T) { } ctx.startGC(5) + // make sure gc.Sync finds the new CRD and starts monitoring it + time.Sleep(ctx.syncPeriod + 1*time.Second) rcClient := clientSet.CoreV1().ReplicationControllers(ns.Name) podClient := clientSet.CoreV1().Pods(ns.Name) diff --git a/test/integration/util/util.go b/test/integration/util/util.go index 9a028e0f36d..243ff5b1728 100644 --- a/test/integration/util/util.go +++ b/test/integration/util/util.go @@ -219,7 +219,7 @@ func CreateGCController(ctx context.Context, tb ktesting.TB, restConfig restclie go wait.Until(func() { restMapper.Reset() }, syncPeriod, ctx.Done()) - go gc.Run(ctx, 1) + go gc.Run(ctx, 1, syncPeriod) go gc.Sync(ctx, clientSet.Discovery(), syncPeriod) } return startGC