Skip to content
37 changes: 31 additions & 6 deletions injection/sharedmain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,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))
}

Expand Down Expand Up @@ -346,13 +365,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{}
Expand Down
36 changes: 36 additions & 0 deletions injection/sharedmain/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading