diff --git a/example/main.go b/example/main.go index 52508e233d..f68a094225 100644 --- a/example/main.go +++ b/example/main.go @@ -39,9 +39,9 @@ import ( var log = logf.Log.WithName("example-controller") func main() { - var installWebhookConfig bool - flag.BoolVar(&installWebhookConfig, "install-webhook-config", false, - "enable the installer in the webhook server, so it will install webhook related resources during bootstrapping") + var disableWebhookConfigInstaller bool + flag.BoolVar(&disableWebhookConfigInstaller, "disable-webhook-config-installer", false, + "disable the installer in the webhook server, so it won't install webhook configuration resources during bootstrapping") flag.Parse() logf.SetLogger(logf.ZapLogger(false)) @@ -108,9 +108,9 @@ func main() { entryLog.Info("setting up webhook server") as, err := webhook.NewServer("foo-admission-server", mgr, webhook.ServerOptions{ - Port: 9876, - CertDir: "/tmp/cert", - InstallWebhookConfig: installWebhookConfig, + Port: 9876, + CertDir: "/tmp/cert", + DisableWebhookConfigInstaller: &disableWebhookConfigInstaller, BootstrapOptions: &webhook.BootstrapOptions{ Secret: &apitypes.NamespacedName{ Namespace: "default", diff --git a/pkg/webhook/bootstrap.go b/pkg/webhook/bootstrap.go index c2d17cfc0a..f0ac9425e3 100644 --- a/pkg/webhook/bootstrap.go +++ b/pkg/webhook/bootstrap.go @@ -63,6 +63,10 @@ func (s *Server) setServerDefault() { if len(s.CertDir) == 0 { s.CertDir = path.Join("k8s-webhook-server", "cert") } + if s.DisableWebhookConfigInstaller == nil { + diwc := false + s.DisableWebhookConfigInstaller = &diwc + } if s.Client == nil { cfg, err := config.GetConfig() diff --git a/pkg/webhook/server.go b/pkg/webhook/server.go index bae22685aa..6724cee8db 100644 --- a/pkg/webhook/server.go +++ b/pkg/webhook/server.go @@ -54,9 +54,10 @@ type ServerOptions struct { // Client will be injected by the manager if not set. Client client.Client - // InstallWebhookConfig controls if the server will automatically create webhook related objects + // DisableWebhookConfigInstaller controls if the server will automatically create webhook related objects // during bootstrapping. e.g. webhookConfiguration, service and secret. - InstallWebhookConfig bool + // If false, the server will install the webhook config objects. It is defaulted to false. + DisableWebhookConfigInstaller *bool // BootstrapOptions contains the options for bootstrapping the admission server. *BootstrapOptions @@ -190,7 +191,12 @@ var _ manager.Runnable = &Server{} // Start runs the server. // It will install the webhook related resources depend on the server configuration. func (s *Server) Start(stop <-chan struct{}) error { - if s.InstallWebhookConfig { + s.once.Do(s.setDefault) + if s.err != nil { + return s.err + } + + if s.DisableWebhookConfigInstaller != nil && !*s.DisableWebhookConfigInstaller { log.Info("installing webhook configuration in cluster") err := s.InstallWebhookManifests() if err != nil { @@ -200,6 +206,10 @@ func (s *Server) Start(stop <-chan struct{}) error { log.Info("webhook installer is disabled") } + return s.run(stop) +} + +func (s *Server) run(stop <-chan struct{}) error { srv := &http.Server{ Addr: fmt.Sprintf(":%v", s.Port), Handler: s.sMux,