diff --git a/injection/sharedmain/main.go b/injection/sharedmain/main.go index 79c42c2014..2703e4b7f4 100644 --- a/injection/sharedmain/main.go +++ b/injection/sharedmain/main.go @@ -289,14 +289,6 @@ func MainWithConfig(ctx context.Context, component string, cfg *rest.Config, cto // and pass them in. var wh *webhook.Webhook if len(webhooks) > 0 { - // Register webhook metrics - opts := webhook.GetOptions(ctx) - if opts != nil { - webhook.RegisterMetrics(opts.StatsReporterOptions...) - } else { - webhook.RegisterMetrics() - } - wh, err = webhook.New(ctx, webhooks) if err != nil { logger.Fatalw("Failed to create webhook", zap.Error(err)) diff --git a/webhook/webhook.go b/webhook/webhook.go index d8842df35a..d507bf55f0 100644 --- a/webhook/webhook.go +++ b/webhook/webhook.go @@ -25,6 +25,7 @@ import ( "log" "net" "net/http" + "sync" "time" // Injection stuff @@ -40,6 +41,10 @@ import ( "knative.dev/pkg/system" ) +var ( + metricsOnce sync.Once +) + // Options contains the configuration for the webhook type Options struct { // TLSMinVersion contains the minimum TLS version that is acceptable to communicate with the API server. @@ -147,6 +152,8 @@ func New( logger := logging.FromContext(ctx) if opts.StatsReporter == nil { + // Register webhook metrics + metricsOnce.Do(func() { RegisterMetrics(opts.StatsReporterOptions...) }) reporter, err := NewStatsReporter(opts.StatsReporterOptions...) if err != nil { return nil, err