garbagecollector: use contextual logging

Signed-off-by: Andy Goldstein <andy.goldstein@redhat.com>
This commit is contained in:
Andy Goldstein
2023-01-13 15:00:17 -05:00
parent 77af0be42f
commit 26e3dab78b
7 changed files with 232 additions and 123 deletions

View File

@@ -46,6 +46,8 @@ import (
"k8s.io/client-go/restmapper"
"k8s.io/client-go/tools/cache"
"k8s.io/controller-manager/pkg/informerfactory"
"k8s.io/klog/v2"
"k8s.io/klog/v2/ktesting"
kubeapiservertesting "k8s.io/kubernetes/cmd/kube-apiserver/app/testing"
"k8s.io/kubernetes/pkg/controller/garbagecollector"
"k8s.io/kubernetes/test/integration"
@@ -199,6 +201,7 @@ func createRandomCustomResourceDefinition(
}
type testContext struct {
logger klog.Logger
tearDown func()
gc *garbagecollector.GarbageCollector
clientSet clientset.Interface
@@ -258,7 +261,8 @@ func setupWithServer(t *testing.T, result *kubeapiservertesting.TestServer, work
t.Fatalf("failed to create garbage collector: %v", err)
}
ctx, cancel := context.WithCancel(context.Background())
logger, ctx := ktesting.NewTestContext(t)
ctx, cancel := context.WithCancel(ctx)
tearDown := func() {
cancel()
result.TearDownFn()
@@ -272,7 +276,7 @@ func setupWithServer(t *testing.T, result *kubeapiservertesting.TestServer, work
restMapper.Reset()
}, syncPeriod, ctx.Done())
go gc.Run(ctx, workers)
go gc.Sync(clientSet.Discovery(), syncPeriod, ctx.Done())
go gc.Sync(ctx, clientSet.Discovery(), syncPeriod)
}
if workerCount > 0 {
@@ -280,6 +284,7 @@ func setupWithServer(t *testing.T, result *kubeapiservertesting.TestServer, work
}
return &testContext{
logger: logger,
tearDown: tearDown,
gc: gc,
clientSet: clientSet,
@@ -1025,7 +1030,9 @@ func TestBlockingOwnerRefDoesBlock(t *testing.T) {
ctx.startGC(5)
timeout := make(chan struct{})
time.AfterFunc(5*time.Second, func() { close(timeout) })
if !cache.WaitForCacheSync(timeout, gc.IsSynced) {
if !cache.WaitForCacheSync(timeout, func() bool {
return gc.IsSynced(ctx.logger)
}) {
t.Fatalf("failed to wait for garbage collector to be synced")
}