From a23b26913fc970a0085d1801204d77b9739c98c7 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Fri, 1 Dec 2023 09:00:59 +0100 Subject: [PATCH] DRA helper: enhance context support 27a68aee3a4834 introduced context support for events. Creating an event broadcaster with context makes tests more resilient against leaking goroutines when that context gets canceled at the end of a test and enables per-test output via ktesting. --- .../controller/controller.go | 2 +- .../leaderelection/leaderelection.go | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/staging/src/k8s.io/dynamic-resource-allocation/controller/controller.go b/staging/src/k8s.io/dynamic-resource-allocation/controller/controller.go index 957b83f91ce..d8b9df6d220 100644 --- a/staging/src/k8s.io/dynamic-resource-allocation/controller/controller.go +++ b/staging/src/k8s.io/dynamic-resource-allocation/controller/controller.go @@ -189,7 +189,7 @@ func New( schedulingCtxInformer := informerFactory.Resource().V1alpha2().PodSchedulingContexts() claimNameLookup := resourceclaim.NewNameLookup(kubeClient) - eventBroadcaster := record.NewBroadcaster() + eventBroadcaster := record.NewBroadcaster(record.WithContext(ctx)) go func() { <-ctx.Done() eventBroadcaster.Shutdown() diff --git a/staging/src/k8s.io/dynamic-resource-allocation/leaderelection/leaderelection.go b/staging/src/k8s.io/dynamic-resource-allocation/leaderelection/leaderelection.go index d6e031e4792..2b35e8bf13f 100644 --- a/staging/src/k8s.io/dynamic-resource-allocation/leaderelection/leaderelection.go +++ b/staging/src/k8s.io/dynamic-resource-allocation/leaderelection/leaderelection.go @@ -161,6 +161,10 @@ func (l *leaderElection) PrepareHealthCheck(s Server) { } func (l *leaderElection) Run() error { + ctx := l.ctx + if ctx == nil { + ctx = context.Background() + } if l.identity == "" { id, err := defaultLeaderElectionIdentity() if err != nil { @@ -174,7 +178,7 @@ func (l *leaderElection) Run() error { l.namespace = inClusterNamespace() } - broadcaster := record.NewBroadcaster() + broadcaster := record.NewBroadcaster(record.WithContext(ctx)) broadcaster.StartRecordingToSink(&corev1.EventSinkImpl{Interface: l.clientset.CoreV1().Events(l.namespace)}) eventRecorder := broadcaster.NewRecorder(scheme.Scheme, v1.EventSource{Component: fmt.Sprintf("%s/%s", l.lockName, l.identity)}) @@ -188,10 +192,6 @@ func (l *leaderElection) Run() error { return err } - ctx := l.ctx - if ctx == nil { - ctx = context.Background() - } leaderConfig := leaderelection.LeaderElectionConfig{ Lock: lock, LeaseDuration: l.leaseDuration,