@@ -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,23 @@ 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 {
322+ // we use ctx that has the informers etc.
318323 wh , err = webhook .New (ctx , 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+ // we use the parent context because we want the webhook
329+ // to stop first prior to stopping the informers
330+ defer ctxCancel ()
331+ return wh .Run (parentCtx .Done ())
324332 })
333+ } else {
334+ // no webhooks cancel the context when the parent is cancelled
335+ go func () {
336+ <- parentCtx .Done ()
337+ ctxCancel ()
338+ }()
325339 }
326340
327341 // Start the injection clients and informers.
@@ -522,7 +536,8 @@ func WatchObservabilityConfigOrDie(
522536
523537// ControllersAndWebhooksFromCtors returns a list of the controllers and a list
524538// of the webhooks created from the given constructors.
525- func ControllersAndWebhooksFromCtors (ctx context.Context ,
539+ func ControllersAndWebhooksFromCtors (
540+ ctx context.Context ,
526541 cmw * cminformer.InformedWatcher ,
527542 ctors ... injection.ControllerConstructor ,
528543) ([]* controller.Impl , []any ) {
0 commit comments