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 e8eca3c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/webhook/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,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 +276,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 +287,13 @@ func (s *Server) Start(ctx context.Context) error {
return nil
}

// Started returns if the server has been started
func (s *Server) Started() bool {
s.mu.Lock()
defer s.mu.Unlock()
return s.started
}

// 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.Started()).To(BeTrue())

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

0 comments on commit e8eca3c

Please sign in to comment.