Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pkg/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
18 changes: 16 additions & 2 deletions pkg/webhook/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

@sbueringer sbueringer Jul 10, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that 0 is the zero value and we previously used the DefaultPort in that case I guess -1 is our only option.

Unfortunately it's inconsistent with the metrics server (that uses 0 for disabled), but I don't have a better idea

@alvaroaleman is that fine for you as well or do you see an alternative?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am unaware of this history of why we would default to 9443 if the user set anything < 0, which to me seems like the user wants to not serve on 9443 if they are going through the trouble of setting it. The 0 value I agree if they don't set it should be 9443, but anything less than that it seems the user is intentionally trying to set it to serve on a port that wouldn't work.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree!

Port int

// CertDir is the directory that contains the server key and certificate. Defaults to
Expand Down Expand Up @@ -136,7 +138,7 @@ func (o *Options) setDefaults() {
o.WebhookMux = http.NewServeMux()
}

if o.Port <= 0 {
if o.Port == 0 {
o.Port = DefaultPort
}

Expand Down Expand Up @@ -180,14 +182,23 @@ 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.
// It will install the webhook related resources depend on the server configuration.
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{
Expand Down Expand Up @@ -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")
}
Expand Down
Loading