Skip to content

Commit 8b8d524

Browse files
committed
make it possible to monitor if the webhook server has been started
Signed-off-by: Stefan Büringer buringerst@vmware.com
1 parent ef5c8a3 commit 8b8d524

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

pkg/webhook/server.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"k8s.io/apimachinery/pkg/runtime"
3333
kscheme "k8s.io/client-go/kubernetes/scheme"
3434
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
35+
"sigs.k8s.io/controller-runtime/pkg/healthz"
3536
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
3637
"sigs.k8s.io/controller-runtime/pkg/webhook/internal/metrics"
3738
)
@@ -87,6 +88,10 @@ type Server struct {
8788
// defaultingOnce ensures that the default fields are only ever set once.
8889
defaultingOnce sync.Once
8990

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+
9095
// mu protects access to the webhook map & setFields for Start, Register, etc
9196
mu sync.Mutex
9297
}
@@ -272,6 +277,9 @@ func (s *Server) Start(ctx context.Context) error {
272277
close(idleConnsClosed)
273278
}()
274279

280+
s.mu.Lock()
281+
s.started = true
282+
s.mu.Unlock()
275283
if err := srv.Serve(listener); err != nil && err != http.ErrServerClosed {
276284
return err
277285
}
@@ -280,6 +288,19 @@ func (s *Server) Start(ctx context.Context) error {
280288
return nil
281289
}
282290

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+
283304
// InjectFunc injects the field setter into the server.
284305
func (s *Server) InjectFunc(f inject.Func) error {
285306
s.setFields = f

pkg/webhook/server_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ var _ = Describe("Webhook Server", func() {
137137
return ioutil.ReadAll(resp.Body)
138138
}).Should(Equal([]byte("gadzooks!")))
139139

140+
Expect(server.StartedChecker()(nil)).To(Succeed())
141+
140142
ctxCancel()
141143
Eventually(doneCh, "4s").Should(BeClosed())
142144
})

0 commit comments

Comments
 (0)