Skip to content
Open
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
2 changes: 1 addition & 1 deletion pkg/manager/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func (cm *controllerManager) GetAPIReader() client.Reader {
func (cm *controllerManager) GetWebhookServer() webhook.Server {
cm.webhookServerOnce.Do(func() {
if cm.webhookServer == nil {
panic("webhook should not be nil")
return
}
if err := cm.Add(cm.webhookServer); err != nil {
panic(fmt.Sprintf("unable to add webhook server to the controller manager: %s", err))
Expand Down
3 changes: 3 additions & 0 deletions pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@ func New(config *rest.Config, options Options) (Manager, error) {

errChan := make(chan error, 1)
runnables := newRunnables(options.BaseContext, errChan).withLogger(options.Logger)
if _, ok := options.WebhookServer.(*webhook.DisabledServer); ok {
options.WebhookServer = nil
}
return &controllerManager{
stopProcedureEngaged: ptr.To(int64(0)),
cluster: cluster,
Expand Down
10 changes: 10 additions & 0 deletions pkg/manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ var _ = Describe("manger.Manager", func() {
_, isCustomWebhook := svr.(customWebhook)
Expect(isCustomWebhook).To(BeTrue())
})
It("should disable the webhook", func() {
By("setting the port to -1", func() {
m, err := New(cfg, Options{WebhookServer: webhook.NewServer(webhook.Options{Port: -1})})
Expect(err).NotTo(HaveOccurred())
Expect(m).NotTo(BeNil())

svr := m.GetWebhookServer()
Expect(svr).To(BeNil())
})
})

Context("with leader election enabled", func() {
It("should only cancel the leader election after all runnables are done", func(specCtx SpecContext) {
Expand Down
37 changes: 37 additions & 0 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.
Port int

// CertDir is the directory that contains the server key and certificate. Defaults to
Expand Down Expand Up @@ -105,6 +107,9 @@ type Options struct {

// NewServer constructs a new webhook.Server from the provided options.
func NewServer(o Options) Server {
if o.Port == -1 {
return &DisabledServer{}
}
return &DefaultServer{
Options: o,
}
Expand Down Expand Up @@ -300,3 +305,35 @@ func (s *DefaultServer) StartedChecker() healthz.Checker {
func (s *DefaultServer) WebhookMux() *http.ServeMux {
return s.webhookMux
}

var _ Server = &DisabledServer{}

// DisabledServer is a no-op implementation of Server
// that can be used when you want to disable the webhook server.
type DisabledServer struct{}

// NeedLeaderElection returns false since the server is disabled and thus does not need leader election.
func (*DisabledServer) NeedLeaderElection() bool {
return false
}

// Register returns immediately since the server is disabled and thus does not serve any webhooks.
func (d *DisabledServer) Register(path string, hook http.Handler) {
}

// Start does nothing since the server is disabled and thus does not need to be started.
func (d *DisabledServer) Start(ctx context.Context) error {
return nil
}

// StartedChecker returns nil since the server is disabled
// and thus does not have a started state or healthz checker for whether the
// server has been started.
func (d *DisabledServer) StartedChecker() healthz.Checker {
return nil
}

// WebhookMux returns nil since the server is disabled and does not have a webhook mux.
func (d *DisabledServer) WebhookMux() *http.ServeMux {
return nil
}
Loading