From f6c1c0afc7d54bb286fad9dde1359f5f9a14e048 Mon Sep 17 00:00:00 2001 From: Dave Protasowski Date: Thu, 26 Nov 2026 21:58:54 -0500 Subject: [PATCH 1/8] Keep webhook configuration watches active during draining --- injection/sharedmain/main.go | 22 +++++++++++++++++++++- injection/sharedmain/main_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/injection/sharedmain/main.go b/injection/sharedmain/main.go index 88ac7f42ae..b6f8c7da74 100644 --- a/injection/sharedmain/main.go +++ b/injection/sharedmain/main.go @@ -308,7 +308,17 @@ func MainWithConfig(ctx context.Context, component string, cfg *rest.Config, cto // So make sure that we have synchronized our configuration state before launching the // webhooks, so that things are properly initialized. logger.Info("Starting configmap watcher...") - if err := cmw.Start(ctx.Done()); err != nil { + configCtx, stopConfigWatch := configMapWatchContext(ctx, len(webhooks) > 0) + defer stopConfigWatch() + // Preserve cancellation during initial synchronization. Only a running + // webhook needs its configuration watcher to outlive the shutdown signal. + stopStartupCancellation := context.AfterFunc(ctx, stopConfigWatch) + err = cmw.Start(configCtx.Done()) + stopStartupCancellation() + if ctx.Err() != nil { + return + } + if err != nil { logger.Fatalw("Failed to start configmap watcher", zap.Error(err)) } @@ -356,6 +366,16 @@ func MainWithConfig(ctx context.Context, component string, cfg *rest.Config, cto } } +// configMapWatchContext keeps configuration updates running until the caller +// stops the watcher after webhook draining and HTTP shutdown have completed. +// Controller-only processes retain their usual context cancellation behavior. +func configMapWatchContext(ctx context.Context, hasWebhooks bool) (context.Context, context.CancelFunc) { + if hasWebhooks { + ctx = context.WithoutCancel(ctx) + } + return context.WithCancel(ctx) +} + type healthProbesDisabledKey struct{} // WithHealthProbesDisabled signals to MainWithContext that it should disable default probes (readiness and liveness). diff --git a/injection/sharedmain/main_test.go b/injection/sharedmain/main_test.go index 4e341a4875..2006450942 100644 --- a/injection/sharedmain/main_test.go +++ b/injection/sharedmain/main_test.go @@ -29,6 +29,33 @@ import ( "knative.dev/pkg/observability" ) +func TestConfigMapWatchContext(t *testing.T) { + for _, tt := range []struct { + name string + hasWebhooks bool + wantErr error + }{ + {name: "controllers", wantErr: context.Canceled}, + {name: "webhooks", hasWebhooks: true}, + } { + t.Run(tt.name, func(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + watchCtx, stop := configMapWatchContext(ctx, tt.hasWebhooks) + defer stop() + + cancel() + if got := watchCtx.Err(); got != tt.wantErr { + t.Errorf("after shutdown signal: got %v, want %v", got, tt.wantErr) + } + stop() + if got := watchCtx.Err(); got != context.Canceled { + t.Errorf("after watcher stop: got %v, want context.Canceled", got) + } + }) + } +} + func TestEnabledControllers(t *testing.T) { tests := []struct { name string From 42c6610819368e6554a76b9d53577dfa992ae3cc Mon Sep 17 00:00:00 2001 From: Dave Protasowski Date: Thu, 26 Nov 2026 22:01:26 -0500 Subject: [PATCH 2/8] Inline the webhook watcher context adjustment --- injection/sharedmain/main.go | 17 ++++++----------- injection/sharedmain/main_test.go | 27 --------------------------- 2 files changed, 6 insertions(+), 38 deletions(-) diff --git a/injection/sharedmain/main.go b/injection/sharedmain/main.go index b6f8c7da74..637eaec88d 100644 --- a/injection/sharedmain/main.go +++ b/injection/sharedmain/main.go @@ -308,7 +308,12 @@ func MainWithConfig(ctx context.Context, component string, cfg *rest.Config, cto // So make sure that we have synchronized our configuration state before launching the // webhooks, so that things are properly initialized. logger.Info("Starting configmap watcher...") - configCtx, stopConfigWatch := configMapWatchContext(ctx, len(webhooks) > 0) + configCtx := ctx + stopConfigWatch := func() {} + if len(webhooks) > 0 { + // Keep configuration live until webhook draining and HTTP shutdown finish. + configCtx, stopConfigWatch = context.WithCancel(context.WithoutCancel(ctx)) + } defer stopConfigWatch() // Preserve cancellation during initial synchronization. Only a running // webhook needs its configuration watcher to outlive the shutdown signal. @@ -366,16 +371,6 @@ func MainWithConfig(ctx context.Context, component string, cfg *rest.Config, cto } } -// configMapWatchContext keeps configuration updates running until the caller -// stops the watcher after webhook draining and HTTP shutdown have completed. -// Controller-only processes retain their usual context cancellation behavior. -func configMapWatchContext(ctx context.Context, hasWebhooks bool) (context.Context, context.CancelFunc) { - if hasWebhooks { - ctx = context.WithoutCancel(ctx) - } - return context.WithCancel(ctx) -} - type healthProbesDisabledKey struct{} // WithHealthProbesDisabled signals to MainWithContext that it should disable default probes (readiness and liveness). diff --git a/injection/sharedmain/main_test.go b/injection/sharedmain/main_test.go index 2006450942..4e341a4875 100644 --- a/injection/sharedmain/main_test.go +++ b/injection/sharedmain/main_test.go @@ -29,33 +29,6 @@ import ( "knative.dev/pkg/observability" ) -func TestConfigMapWatchContext(t *testing.T) { - for _, tt := range []struct { - name string - hasWebhooks bool - wantErr error - }{ - {name: "controllers", wantErr: context.Canceled}, - {name: "webhooks", hasWebhooks: true}, - } { - t.Run(tt.name, func(t *testing.T) { - ctx, cancel := context.WithCancel(t.Context()) - defer cancel() - watchCtx, stop := configMapWatchContext(ctx, tt.hasWebhooks) - defer stop() - - cancel() - if got := watchCtx.Err(); got != tt.wantErr { - t.Errorf("after shutdown signal: got %v, want %v", got, tt.wantErr) - } - stop() - if got := watchCtx.Err(); got != context.Canceled { - t.Errorf("after watcher stop: got %v, want context.Canceled", got) - } - }) - } -} - func TestEnabledControllers(t *testing.T) { tests := []struct { name string From c334c4d467ab14b89b397fad7a13d9836c6c3811 Mon Sep 17 00:00:00 2001 From: Dave Protasowski Date: Thu, 26 Nov 2026 22:02:14 -0500 Subject: [PATCH 3/8] Scope watcher lifetime setup to the webhook branch --- injection/sharedmain/main.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/injection/sharedmain/main.go b/injection/sharedmain/main.go index 637eaec88d..901a6ba762 100644 --- a/injection/sharedmain/main.go +++ b/injection/sharedmain/main.go @@ -308,18 +308,17 @@ func MainWithConfig(ctx context.Context, component string, cfg *rest.Config, cto // So make sure that we have synchronized our configuration state before launching the // webhooks, so that things are properly initialized. logger.Info("Starting configmap watcher...") - configCtx := ctx - stopConfigWatch := func() {} if len(webhooks) > 0 { // Keep configuration live until webhook draining and HTTP shutdown finish. - configCtx, stopConfigWatch = context.WithCancel(context.WithoutCancel(ctx)) - } - defer stopConfigWatch() - // Preserve cancellation during initial synchronization. Only a running - // webhook needs its configuration watcher to outlive the shutdown signal. - stopStartupCancellation := context.AfterFunc(ctx, stopConfigWatch) - err = cmw.Start(configCtx.Done()) - stopStartupCancellation() + configCtx, stopConfigWatch := context.WithCancel(context.WithoutCancel(ctx)) + defer stopConfigWatch() + // Preserve cancellation during initial synchronization. + stopStartupCancellation := context.AfterFunc(ctx, stopConfigWatch) + err = cmw.Start(configCtx.Done()) + stopStartupCancellation() + } else { + err = cmw.Start(ctx.Done()) + } if ctx.Err() != nil { return } From b7fdd9685671b6b55c9e3cf68cc93bd24bf96b3f Mon Sep 17 00:00:00 2001 From: Dave Protasowski Date: Thu, 26 Nov 2026 22:04:38 -0500 Subject: [PATCH 4/8] Split watcher context setup into two steps --- injection/sharedmain/main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/injection/sharedmain/main.go b/injection/sharedmain/main.go index 901a6ba762..0444ce7a91 100644 --- a/injection/sharedmain/main.go +++ b/injection/sharedmain/main.go @@ -310,7 +310,8 @@ func MainWithConfig(ctx context.Context, component string, cfg *rest.Config, cto logger.Info("Starting configmap watcher...") if len(webhooks) > 0 { // Keep configuration live until webhook draining and HTTP shutdown finish. - configCtx, stopConfigWatch := context.WithCancel(context.WithoutCancel(ctx)) + configCtx := context.WithoutCancel(ctx) + configCtx, stopConfigWatch := context.WithCancel(configCtx) defer stopConfigWatch() // Preserve cancellation during initial synchronization. stopStartupCancellation := context.AfterFunc(ctx, stopConfigWatch) From 63144a4bd2e594b049c66008262dd7afb5a06b8e Mon Sep 17 00:00:00 2001 From: Dave Protasowski Date: Thu, 26 Nov 2026 22:09:38 -0500 Subject: [PATCH 5/8] Start profiling after configuration synchronization succeeds --- injection/sharedmain/main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/injection/sharedmain/main.go b/injection/sharedmain/main.go index 0444ce7a91..1a36005465 100644 --- a/injection/sharedmain/main.go +++ b/injection/sharedmain/main.go @@ -302,7 +302,6 @@ func MainWithConfig(ctx context.Context, component string, cfg *rest.Config, cto WatchObservabilityConfigOrDie(ctx, cmw, pprof, logger, component) eg, egCtx := errgroup.WithContext(ctx) - eg.Go(pprof.ListenAndServe) // Many of the webhooks rely on configuration, e.g. configurable defaults, feature flags. // So make sure that we have synchronized our configuration state before launching the @@ -327,6 +326,8 @@ func MainWithConfig(ctx context.Context, component string, cfg *rest.Config, cto logger.Fatalw("Failed to start configmap watcher", zap.Error(err)) } + eg.Go(pprof.ListenAndServe) + // If we have one or more admission controllers, then start the webhook // and pass them in. var wh *webhook.Webhook From 69c23ee6f5c378301b758f14a5fe6b6e51fa5a6f Mon Sep 17 00:00:00 2001 From: Dave Protasowski Date: Thu, 26 Nov 2026 22:12:05 -0500 Subject: [PATCH 6/8] Share deferred server cleanup across shutdown paths --- injection/sharedmain/main.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/injection/sharedmain/main.go b/injection/sharedmain/main.go index 1a36005465..62cd852308 100644 --- a/injection/sharedmain/main.go +++ b/injection/sharedmain/main.go @@ -302,6 +302,7 @@ func MainWithConfig(ctx context.Context, component string, cfg *rest.Config, cto WatchObservabilityConfigOrDie(ctx, cmw, pprof, logger, component) eg, egCtx := errgroup.WithContext(ctx) + eg.Go(pprof.ListenAndServe) // Many of the webhooks rely on configuration, e.g. configurable defaults, feature flags. // So make sure that we have synchronized our configuration state before launching the @@ -319,6 +320,15 @@ func MainWithConfig(ctx context.Context, component string, cfg *rest.Config, cto } else { err = cmw.Start(ctx.Done()) } + // Register cleanup after stopConfigWatch so HTTP draining finishes before + // the ConfigMap watcher is stopped. This also cleans up startup cancellation. + defer func() { + pprof.Shutdown(context.Background()) + // Don't forward ErrServerClosed as that indicates we're already shutting down. + if err := eg.Wait(); err != nil && !errors.Is(err, http.ErrServerClosed) { + logger.Errorw("Error while running server", zap.Error(err)) + } + }() if ctx.Err() != nil { return } @@ -326,8 +336,6 @@ func MainWithConfig(ctx context.Context, component string, cfg *rest.Config, cto logger.Fatalw("Failed to start configmap watcher", zap.Error(err)) } - eg.Go(pprof.ListenAndServe) - // If we have one or more admission controllers, then start the webhook // and pass them in. var wh *webhook.Webhook @@ -363,13 +371,6 @@ func MainWithConfig(ctx context.Context, component string, cfg *rest.Config, cto // This will block until either a signal arrives or one of the grouped functions // returns an error. <-egCtx.Done() - - pprof.Shutdown(context.Background()) - - // Don't forward ErrServerClosed as that indicates we're already shutting down. - if err := eg.Wait(); err != nil && !errors.Is(err, http.ErrServerClosed) { - logger.Errorw("Error while running server", zap.Error(err)) - } } type healthProbesDisabledKey struct{} From dbe7bfaf02fbd18d4a74f1fe97106731232fec35 Mon Sep 17 00:00:00 2001 From: Dave Protasowski Date: Thu, 26 Nov 2026 22:14:32 -0500 Subject: [PATCH 7/8] Name and test the config watcher startup-to-draining handoff --- injection/sharedmain/main.go | 22 ++++++---- injection/sharedmain/main_test.go | 67 +++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 7 deletions(-) diff --git a/injection/sharedmain/main.go b/injection/sharedmain/main.go index 62cd852308..2f11bdb070 100644 --- a/injection/sharedmain/main.go +++ b/injection/sharedmain/main.go @@ -309,14 +309,9 @@ func MainWithConfig(ctx context.Context, component string, cfg *rest.Config, cto // webhooks, so that things are properly initialized. logger.Info("Starting configmap watcher...") if len(webhooks) > 0 { - // Keep configuration live until webhook draining and HTTP shutdown finish. - configCtx := context.WithoutCancel(ctx) - configCtx, stopConfigWatch := context.WithCancel(configCtx) + var stopConfigWatch context.CancelFunc + stopConfigWatch, err = startConfigMapWatcherForDrain(ctx, cmw) defer stopConfigWatch() - // Preserve cancellation during initial synchronization. - stopStartupCancellation := context.AfterFunc(ctx, stopConfigWatch) - err = cmw.Start(configCtx.Done()) - stopStartupCancellation() } else { err = cmw.Start(ctx.Done()) } @@ -373,6 +368,19 @@ func MainWithConfig(ctx context.Context, component string, cfg *rest.Config, cto <-egCtx.Done() } +// startConfigMapWatcherForDrain respects shutdown during initial sync, +// then keeps watching until the caller stops it after HTTP draining. +func startConfigMapWatcherForDrain(ctx context.Context, watcher configmap.Watcher) (context.CancelFunc, error) { + watchCtx := context.WithoutCancel(ctx) + watchCtx, stop := context.WithCancel(watchCtx) + + stopOnShutdown := context.AfterFunc(ctx, stop) + err := watcher.Start(watchCtx.Done()) + stopOnShutdown() + + return stop, err +} + type healthProbesDisabledKey struct{} // WithHealthProbesDisabled signals to MainWithContext that it should disable default probes (readiness and liveness). diff --git a/injection/sharedmain/main_test.go b/injection/sharedmain/main_test.go index 4e341a4875..069c15c33d 100644 --- a/injection/sharedmain/main_test.go +++ b/injection/sharedmain/main_test.go @@ -19,16 +19,83 @@ package sharedmain import ( "context" "testing" + "time" "github.com/google/go-cmp/cmp" "go.uber.org/zap/zapcore" + "knative.dev/pkg/configmap" "knative.dev/pkg/injection" "knative.dev/pkg/leaderelection" "knative.dev/pkg/logging" "knative.dev/pkg/observability" ) +type startupWatcher struct { + configmap.Watcher + start func(<-chan struct{}) error +} + +func (w startupWatcher) Start(stop <-chan struct{}) error { + return w.start(stop) +} + +func TestStartConfigMapWatcherForDrain(t *testing.T) { + for _, duringSync := range []bool{true, false} { + name := "after sync" + if duringSync { + name = "during sync" + } + t.Run(name, func(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + var watcherStop <-chan struct{} + watcher := startupWatcher{start: func(stop <-chan struct{}) error { + watcherStop = stop + if duringSync { + cancel() + select { + case <-stop: + return context.Canceled + case <-time.After(5 * time.Second): + t.Fatal("shutdown did not interrupt synchronization") + } + } + return nil + }} + + stop, err := startConfigMapWatcherForDrain(ctx, watcher) + defer stop() + var wantErr error + if duringSync { + wantErr = context.Canceled + } + if err != wantErr { + t.Fatalf("Start error = %v, want %v", err, wantErr) + } + + cancel() + select { + case <-watcherStop: + if !duringSync { + t.Fatal("watcher stopped before draining completed") + } + default: + if duringSync { + t.Fatal("watcher still running after startup cancellation") + } + } + + stop() + select { + case <-watcherStop: + default: + t.Fatal("explicit stop did not stop watcher") + } + }) + } +} + func TestEnabledControllers(t *testing.T) { tests := []struct { name string From 91e3e701c74e549ca5cc645810e9169dfc60fe82 Mon Sep 17 00:00:00 2001 From: Dave Protasowski Date: Thu, 26 Nov 2026 22:15:13 -0500 Subject: [PATCH 8/8] Reduce watcher test to shutdown and explicit cleanup --- injection/sharedmain/main_test.go | 77 +++++++++---------------------- 1 file changed, 23 insertions(+), 54 deletions(-) diff --git a/injection/sharedmain/main_test.go b/injection/sharedmain/main_test.go index 069c15c33d..ed9bdb422f 100644 --- a/injection/sharedmain/main_test.go +++ b/injection/sharedmain/main_test.go @@ -19,7 +19,6 @@ package sharedmain import ( "context" "testing" - "time" "github.com/google/go-cmp/cmp" "go.uber.org/zap/zapcore" @@ -33,66 +32,36 @@ import ( type startupWatcher struct { configmap.Watcher - start func(<-chan struct{}) error + stop <-chan struct{} } -func (w startupWatcher) Start(stop <-chan struct{}) error { - return w.start(stop) +func (w *startupWatcher) Start(stop <-chan struct{}) error { + w.stop = stop + return nil } func TestStartConfigMapWatcherForDrain(t *testing.T) { - for _, duringSync := range []bool{true, false} { - name := "after sync" - if duringSync { - name = "during sync" - } - t.Run(name, func(t *testing.T) { - ctx, cancel := context.WithCancel(t.Context()) - defer cancel() - var watcherStop <-chan struct{} - watcher := startupWatcher{start: func(stop <-chan struct{}) error { - watcherStop = stop - if duringSync { - cancel() - select { - case <-stop: - return context.Canceled - case <-time.After(5 * time.Second): - t.Fatal("shutdown did not interrupt synchronization") - } - } - return nil - }} - - stop, err := startConfigMapWatcherForDrain(ctx, watcher) - defer stop() - var wantErr error - if duringSync { - wantErr = context.Canceled - } - if err != wantErr { - t.Fatalf("Start error = %v, want %v", err, wantErr) - } + ctx, cancel := context.WithCancel(t.Context()) + defer cancel() + watcher := &startupWatcher{} + stop, err := startConfigMapWatcherForDrain(ctx, watcher) + if err != nil { + t.Fatal(err) + } + defer stop() - cancel() - select { - case <-watcherStop: - if !duringSync { - t.Fatal("watcher stopped before draining completed") - } - default: - if duringSync { - t.Fatal("watcher still running after startup cancellation") - } - } + cancel() + select { + case <-watcher.stop: + t.Fatal("watcher stopped before draining completed") + default: + } - stop() - select { - case <-watcherStop: - default: - t.Fatal("explicit stop did not stop watcher") - } - }) + stop() + select { + case <-watcher.stop: + default: + t.Fatal("explicit stop did not stop watcher") } }