diff --git a/injection/sharedmain/main.go b/injection/sharedmain/main.go index 88ac7f42ae..2f11bdb070 100644 --- a/injection/sharedmain/main.go +++ b/injection/sharedmain/main.go @@ -308,7 +308,26 @@ 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 { + if len(webhooks) > 0 { + var stopConfigWatch context.CancelFunc + stopConfigWatch, err = startConfigMapWatcherForDrain(ctx, cmw) + defer stopConfigWatch() + } 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 + } + if err != nil { logger.Fatalw("Failed to start configmap watcher", zap.Error(err)) } @@ -347,13 +366,19 @@ 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()) +// 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) - // 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)) - } + stopOnShutdown := context.AfterFunc(ctx, stop) + err := watcher.Start(watchCtx.Done()) + stopOnShutdown() + + return stop, err } type healthProbesDisabledKey struct{} diff --git a/injection/sharedmain/main_test.go b/injection/sharedmain/main_test.go index 4e341a4875..ed9bdb422f 100644 --- a/injection/sharedmain/main_test.go +++ b/injection/sharedmain/main_test.go @@ -23,12 +23,48 @@ import ( "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 + stop <-chan struct{} +} + +func (w *startupWatcher) Start(stop <-chan struct{}) error { + w.stop = stop + return nil +} + +func TestStartConfigMapWatcherForDrain(t *testing.T) { + 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 <-watcher.stop: + t.Fatal("watcher stopped before draining completed") + default: + } + + stop() + select { + case <-watcher.stop: + default: + t.Fatal("explicit stop did not stop watcher") + } +} + func TestEnabledControllers(t *testing.T) { tests := []struct { name string