From f8e25eff926c640c86daa46222bfaf8d625e75d7 Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Fri, 1 Dec 2023 09:00:59 +0100 Subject: [PATCH] client-go events: also support context for NewEventBroadcasterAdapter 27a68aee3a4834 introduced context support. In order to use that also with NewEventBroadcasterAdapter, a variant of the call is needed to allow the caller to specify the context. The `logcheck:context` comment ensures that code which is meant to be contextual uses the new call. --- cmd/kube-scheduler/app/options/options.go | 2 +- .../k8s.io/client-go/tools/events/event_broadcaster.go | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/cmd/kube-scheduler/app/options/options.go b/cmd/kube-scheduler/app/options/options.go index 8f787396efb..e039b0d05c9 100644 --- a/cmd/kube-scheduler/app/options/options.go +++ b/cmd/kube-scheduler/app/options/options.go @@ -283,7 +283,7 @@ func (o *Options) Config(ctx context.Context) (*schedulerappconfig.Config, error return nil, err } - c.EventBroadcaster = events.NewEventBroadcasterAdapter(eventClient) + c.EventBroadcaster = events.NewEventBroadcasterAdapterWithContext(ctx, eventClient) // Set up leader election if enabled. var leaderElectionConfig *leaderelection.LeaderElectionConfig diff --git a/staging/src/k8s.io/client-go/tools/events/event_broadcaster.go b/staging/src/k8s.io/client-go/tools/events/event_broadcaster.go index e0164f301ee..94c2012b8bb 100644 --- a/staging/src/k8s.io/client-go/tools/events/event_broadcaster.go +++ b/staging/src/k8s.io/client-go/tools/events/event_broadcaster.go @@ -404,7 +404,15 @@ type eventBroadcasterAdapterImpl struct { // NewEventBroadcasterAdapter creates a wrapper around new and legacy broadcasters to simplify // migration of individual components to the new Event API. +// +//logcheck:context // NewEventBroadcasterAdapterWithContext should be used instead because record.NewBroadcaster is called and works better when a context is supplied (contextual logging, cancellation). func NewEventBroadcasterAdapter(client clientset.Interface) EventBroadcasterAdapter { + return NewEventBroadcasterAdapterWithContext(context.Background(), client) +} + +// NewEventBroadcasterAdapterWithContext creates a wrapper around new and legacy broadcasters to simplify +// migration of individual components to the new Event API. +func NewEventBroadcasterAdapterWithContext(ctx context.Context, client clientset.Interface) EventBroadcasterAdapter { eventClient := &eventBroadcasterAdapterImpl{} if _, err := client.Discovery().ServerResourcesForGroupVersion(eventsv1.SchemeGroupVersion.String()); err == nil { eventClient.eventsv1Client = client.EventsV1() @@ -414,7 +422,7 @@ func NewEventBroadcasterAdapter(client clientset.Interface) EventBroadcasterAdap // we create it unconditionally because its overhead is minor and will simplify using usage // patterns of this library in all components. eventClient.coreClient = client.CoreV1() - eventClient.coreBroadcaster = record.NewBroadcaster() + eventClient.coreBroadcaster = record.NewBroadcaster(record.WithContext(ctx)) return eventClient }