@@ -60,9 +60,13 @@ type Server struct {
6060 CertDir string
6161
6262 // CertName is the server certificate name. Defaults to tls.crt.
63+ //
64+ // Note: This option should only be set when TLSOpts does not override GetCertificate.
6365 CertName string
6466
6567 // KeyName is the server key name. Defaults to tls.key.
68+ //
69+ // Note: This option should only be set when TLSOpts does not override GetCertificate.
6670 KeyName string
6771
6872 // ClientCAName is the CA certificate name which server used to verify remote(client)'s certificate.
@@ -169,32 +173,40 @@ func (s *Server) Start(ctx context.Context) error {
169173 baseHookLog := log .WithName ("webhooks" )
170174 baseHookLog .Info ("Starting webhook server" )
171175
172- certPath := filepath .Join (s .CertDir , s .CertName )
173- keyPath := filepath .Join (s .CertDir , s .KeyName )
174-
175- certWatcher , err := certwatcher .New (certPath , keyPath )
176- if err != nil {
177- return err
178- }
179-
180- go func () {
181- if err := certWatcher .Start (ctx ); err != nil {
182- log .Error (err , "certificate watcher error" )
183- }
184- }()
185-
186176 tlsMinVersion , err := tlsVersion (s .TLSMinVersion )
187177 if err != nil {
188178 return err
189179 }
190180
191181 cfg := & tls.Config { //nolint:gosec
192- NextProtos : []string {"h2" },
193- GetCertificate : certWatcher .GetCertificate ,
194- MinVersion : tlsMinVersion ,
182+ NextProtos : []string {"h2" },
183+ MinVersion : tlsMinVersion ,
184+ }
185+ // fallback TLS config ready, will now mutate if passer wants full control over it
186+ for _ , op := range s .TLSOpts {
187+ op (cfg )
188+ }
189+
190+ if cfg .GetCertificate == nil {
191+ certPath := filepath .Join (s .CertDir , s .CertName )
192+ keyPath := filepath .Join (s .CertDir , s .KeyName )
193+
194+ // Create the certificate watcher and
195+ // set the config's GetCertificate on the TLSConfig
196+ certWatcher , err := certwatcher .New (certPath , keyPath )
197+ if err != nil {
198+ return err
199+ }
200+ cfg .GetCertificate = certWatcher .GetCertificate
201+
202+ go func () {
203+ if err := certWatcher .Start (ctx ); err != nil {
204+ log .Error (err , "certificate watcher error" )
205+ }
206+ }()
195207 }
196208
197- // load CA to verify client certificate
209+ // Load CA to verify client certificate, if configured.
198210 if s .ClientCAName != "" {
199211 certPool := x509 .NewCertPool ()
200212 clientCABytes , err := os .ReadFile (filepath .Join (s .CertDir , s .ClientCAName ))
@@ -211,11 +223,6 @@ func (s *Server) Start(ctx context.Context) error {
211223 cfg .ClientAuth = tls .RequireAndVerifyClientCert
212224 }
213225
214- // fallback TLS config ready, will now mutate if passer wants full control over it
215- for _ , op := range s .TLSOpts {
216- op (cfg )
217- }
218-
219226 listener , err := tls .Listen ("tcp" , net .JoinHostPort (s .Host , strconv .Itoa (s .Port )), cfg )
220227 if err != nil {
221228 return err
0 commit comments