@@ -32,6 +32,7 @@ import (
32
32
"k8s.io/apimachinery/pkg/runtime"
33
33
kscheme "k8s.io/client-go/kubernetes/scheme"
34
34
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
35
+ "sigs.k8s.io/controller-runtime/pkg/healthz"
35
36
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
36
37
"sigs.k8s.io/controller-runtime/pkg/webhook/internal/metrics"
37
38
)
@@ -87,6 +88,10 @@ type Server struct {
87
88
// defaultingOnce ensures that the default fields are only ever set once.
88
89
defaultingOnce sync.Once
89
90
91
+ // started is set to true immediately before the server is started
92
+ // and thus can be used to check if the server has been started
93
+ started bool
94
+
90
95
// mu protects access to the webhook map & setFields for Start, Register, etc
91
96
mu sync.Mutex
92
97
}
@@ -272,6 +277,9 @@ func (s *Server) Start(ctx context.Context) error {
272
277
close (idleConnsClosed )
273
278
}()
274
279
280
+ s .mu .Lock ()
281
+ s .started = true
282
+ s .mu .Unlock ()
275
283
if err := srv .Serve (listener ); err != nil && err != http .ErrServerClosed {
276
284
return err
277
285
}
@@ -280,6 +288,19 @@ func (s *Server) Start(ctx context.Context) error {
280
288
return nil
281
289
}
282
290
291
+ // StartedChecker returns an healthz.Checker which is healthy after the
292
+ // server has been started.
293
+ func (s * Server ) StartedChecker () healthz.Checker {
294
+ return func (req * http.Request ) error {
295
+ s .mu .Lock ()
296
+ defer s .mu .Unlock ()
297
+ if ! s .started {
298
+ return fmt .Errorf ("webhook server has not been started yet" )
299
+ }
300
+ return nil
301
+ }
302
+ }
303
+
283
304
// InjectFunc injects the field setter into the server.
284
305
func (s * Server ) InjectFunc (f inject.Func ) error {
285
306
s .setFields = f
0 commit comments