Skip to content

Commit 01f1a87

Browse files
committed
fix context handling so informers are active while webhook is draining
1 parent 8b27a4c commit 01f1a87

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

  • vendor/knative.dev/pkg/injection/sharedmain

vendor/knative.dev/pkg/injection/sharedmain/main.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,11 @@ func IsHADisabled(ctx context.Context) bool {
240240

241241
// MainWithConfig runs the generic main flow for controllers and webhooks
242242
// with the given config.
243-
func MainWithConfig(ctx context.Context, component string, cfg *rest.Config, ctors ...injection.ControllerConstructor) {
243+
func MainWithConfig(parentCtx context.Context, component string, cfg *rest.Config, ctors ...injection.ControllerConstructor) {
244+
ctx := context.WithoutCancel(parentCtx)
245+
ctx, ctxCancel := context.WithCancel(ctx)
246+
defer ctxCancel()
247+
244248
log.Printf("Registering %d clients", len(injection.Default.GetClients()))
245249
log.Printf("Registering %d informer factories", len(injection.Default.GetInformerFactories()))
246250
log.Printf("Registering %d informers", len(injection.Default.GetInformers()))
@@ -315,13 +319,21 @@ func MainWithConfig(ctx context.Context, component string, cfg *rest.Config, cto
315319
// and pass them in.
316320
var wh *webhook.Webhook
317321
if len(webhooks) > 0 {
318-
wh, err = webhook.New(ctx, webhooks)
322+
webhookCtx := parentCtx
323+
wh, err = webhook.New(webhookCtx, webhooks)
319324
if err != nil {
320325
logger.Fatalw("Failed to create webhook", zap.Error(err))
321326
}
322327
eg.Go(func() error {
323-
return wh.Run(ctx.Done())
328+
defer ctxCancel()
329+
return wh.Run(webhookCtx.Done())
324330
})
331+
} else {
332+
// no webhooks cancel the context when the parent is cancelled
333+
go func() {
334+
<-parentCtx.Done()
335+
ctxCancel()
336+
}()
325337
}
326338

327339
// Start the injection clients and informers.
@@ -522,7 +534,8 @@ func WatchObservabilityConfigOrDie(
522534

523535
// ControllersAndWebhooksFromCtors returns a list of the controllers and a list
524536
// of the webhooks created from the given constructors.
525-
func ControllersAndWebhooksFromCtors(ctx context.Context,
537+
func ControllersAndWebhooksFromCtors(
538+
ctx context.Context,
526539
cmw *cminformer.InformedWatcher,
527540
ctors ...injection.ControllerConstructor,
528541
) ([]*controller.Impl, []any) {

0 commit comments

Comments
 (0)