Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 2a93892

Browse files
committed
Fix probe to allow proper shutdowns
Signed-off-by: JoshVanL <[email protected]>
1 parent 830c605 commit 2a93892

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

pkg/probe/probe.go

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,45 @@ const (
1919
)
2020

2121
type HealthCheck struct {
22-
handler healthcheck.Handler
23-
22+
*http.Server
2423
oidcAuther authenticator.Token
2524
fakeJWT string
2625

2726
ready bool
2827
}
2928

30-
func Run(port, fakeJWT string, oidcAuther authenticator.Token) error {
29+
func Run(port, fakeJWT string, oidcAuther authenticator.Token) (*HealthCheck, error) {
30+
handler := healthcheck.NewHandler()
31+
32+
ln, err := net.Listen("tcp", "0.0.0.0:"+port)
33+
if err != nil {
34+
return nil, fmt.Errorf("failed to listen on health check port: %s", err)
35+
}
36+
3137
h := &HealthCheck{
32-
handler: healthcheck.NewHandler(),
3338
oidcAuther: oidcAuther,
3439
fakeJWT: fakeJWT,
40+
Server: &http.Server{
41+
Addr: ln.Addr().String(),
42+
ReadTimeout: 8 * time.Second,
43+
WriteTimeout: 8 * time.Second,
44+
MaxHeaderBytes: 1 << 20, // 1 MiB
45+
Handler: handler,
46+
},
3547
}
3648

37-
h.handler.AddReadinessCheck("secure serving", h.Check)
49+
handler.AddReadinessCheck("secure serving", h.Check)
3850

3951
go func() {
40-
for {
41-
err := http.ListenAndServe(net.JoinHostPort("0.0.0.0", port), h.handler)
42-
if err != nil {
43-
klog.Errorf("ready probe listener failed: %s", err)
44-
}
45-
time.Sleep(5 * time.Second)
52+
klog.Infof("serving readiness probe on %s/ready", ln.Addr())
53+
54+
if err := h.Serve(ln); err != nil {
55+
klog.Errorf("failed to serve readiness probe: %s", err)
56+
return
4657
}
4758
}()
4859

49-
return nil
60+
return h, nil
5061
}
5162

5263
func (h *HealthCheck) Check() error {
@@ -71,3 +82,23 @@ func (h *HealthCheck) Check() error {
7182

7283
return nil
7384
}
85+
86+
func (h *HealthCheck) Shutdown() error {
87+
// If readiness probe server is not started than exit early
88+
if h.Server == nil {
89+
return nil
90+
}
91+
92+
klog.Info("shutting down readiness probe server...")
93+
94+
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
95+
defer cancel()
96+
97+
if err := h.Server.Shutdown(ctx); err != nil {
98+
return fmt.Errorf("readiness probe server shutdown failed: %s", err)
99+
}
100+
101+
klog.Info("readines probe server gracefully stopped")
102+
103+
return nil
104+
}

pkg/probe/probe_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,18 @@ func TestRun(t *testing.T) {
4444
t.FailNow()
4545
}
4646

47-
if err := Run(port, fakeJWT, f); err != nil {
47+
readinessHandler, err := Run(port, fakeJWT, f)
48+
if err != nil {
4849
t.Error(err.Error())
4950
t.FailNow()
5051
}
5152

53+
defer func() {
54+
if err := readinessHandler.Shutdown(); err != nil {
55+
t.Error(err)
56+
}
57+
}()
58+
5259
url := fmt.Sprintf("http://0.0.0.0:%s", port)
5360

5461
var resp *http.Response

0 commit comments

Comments
 (0)