diff --git a/pkg/manager/manager_test.go b/pkg/manager/manager_test.go index 4bf553572d..4aa5d7277e 100644 --- a/pkg/manager/manager_test.go +++ b/pkg/manager/manager_test.go @@ -160,6 +160,21 @@ var _ = Describe("manger.Manager", func() { Expect(isCustomWebhook).To(BeTrue()) }) + It("should create a webhook server that is disabled", func(specCtx SpecContext) { + By("setting the port to -1", func() { + srv := webhook.NewServer(webhook.Options{Port: -1}) + m, err := New(cfg, Options{WebhookServer: srv}) + Expect(err).NotTo(HaveOccurred()) + Expect(m).NotTo(BeNil()) + + svr := m.GetWebhookServer() + Expect(svr).NotTo(BeNil()) + Expect(svr.(*webhook.DefaultServer).Options.Port).To(Equal(-1)) + Expect(svr.Start(specCtx)).NotTo(HaveOccurred()) + Expect(svr.WebhookMux()).ToNot(BeNil()) + }) + }) + Context("with leader election enabled", func() { It("should only cancel the leader election after all runnables are done", func(specCtx SpecContext) { m, err := New(cfg, Options{ diff --git a/pkg/webhook/server.go b/pkg/webhook/server.go index 079f0a55ff..e04bb7b001 100644 --- a/pkg/webhook/server.go +++ b/pkg/webhook/server.go @@ -75,6 +75,8 @@ type Options struct { // Port is the port number that the server will serve. // It will be defaulted to 9443 if unspecified. + // + // To disable the webhook server set Port to -1. Port int // CertDir is the directory that contains the server key and certificate. Defaults to @@ -136,7 +138,7 @@ func (o *Options) setDefaults() { o.WebhookMux = http.NewServeMux() } - if o.Port <= 0 { + if o.Port == 0 { o.Port = DefaultPort } @@ -180,7 +182,11 @@ func (s *DefaultServer) Register(path string, hook http.Handler) { s.webhookMux.Handle(path, metrics.InstrumentedHook(path, hook)) regLog := log.WithValues("path", path) - regLog.Info("Registering webhook") + if s.Options.Port < 0 { + regLog.Info("Webhook is disabled") + } else { + regLog.Info("Registering webhook") + } } // Start runs the server. @@ -188,6 +194,11 @@ func (s *DefaultServer) Register(path string, hook http.Handler) { func (s *DefaultServer) Start(ctx context.Context) error { s.defaultingOnce.Do(s.setDefaults) + if s.Options.Port < 0 { + log.Info("Webhook server is disabled") + return nil + } + log.Info("Starting webhook server") cfg := &tls.Config{ @@ -278,6 +289,9 @@ func (s *DefaultServer) StartedChecker() healthz.Checker { s.mu.Lock() defer s.mu.Unlock() + if s.Options.Port < 0 { + return nil + } if !s.started { return fmt.Errorf("webhook server has not been started yet") }