Skip to content

Commit

Permalink
make it possible to monitor if the webhook server has been started
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Büringer buringerst@vmware.com
  • Loading branch information
sbueringer committed Jul 9, 2021
1 parent ef5c8a3 commit 8b8d524
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/webhook/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
kscheme "k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/certwatcher"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/runtime/inject"
"sigs.k8s.io/controller-runtime/pkg/webhook/internal/metrics"
)
Expand Down Expand Up @@ -87,6 +88,10 @@ type Server struct {
// defaultingOnce ensures that the default fields are only ever set once.
defaultingOnce sync.Once

// started is set to true immediately before the server is started
// and thus can be used to check if the server has been started
started bool

// mu protects access to the webhook map & setFields for Start, Register, etc
mu sync.Mutex
}
Expand Down Expand Up @@ -272,6 +277,9 @@ func (s *Server) Start(ctx context.Context) error {
close(idleConnsClosed)
}()

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

// StartedChecker returns an healthz.Checker which is healthy after the
// server has been started.
func (s *Server) StartedChecker() healthz.Checker {
return func(req *http.Request) error {
s.mu.Lock()
defer s.mu.Unlock()
if !s.started {
return fmt.Errorf("webhook server has not been started yet")
}
return nil
}
}

// InjectFunc injects the field setter into the server.
func (s *Server) InjectFunc(f inject.Func) error {
s.setFields = f
Expand Down
2 changes: 2 additions & 0 deletions pkg/webhook/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ var _ = Describe("Webhook Server", func() {
return ioutil.ReadAll(resp.Body)
}).Should(Equal([]byte("gadzooks!")))

Expect(server.StartedChecker()(nil)).To(Succeed())

ctxCancel()
Eventually(doneCh, "4s").Should(BeClosed())
})
Expand Down

0 comments on commit 8b8d524

Please sign in to comment.