DRA helper: enhance context support

27a68aee3a 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.
This commit is contained in:
Patrick Ohly 2023-12-01 09:00:59 +01:00
parent 46f4248d56
commit a23b26913f
2 changed files with 6 additions and 6 deletions

View File

@ -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()

View File

@ -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,