@@ -19,34 +19,45 @@ const (
19
19
)
20
20
21
21
type HealthCheck struct {
22
- handler healthcheck.Handler
23
-
22
+ * http.Server
24
23
oidcAuther authenticator.Token
25
24
fakeJWT string
26
25
27
26
ready bool
28
27
}
29
28
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
+
31
37
h := & HealthCheck {
32
- handler : healthcheck .NewHandler (),
33
38
oidcAuther : oidcAuther ,
34
39
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
+ },
35
47
}
36
48
37
- h . handler .AddReadinessCheck ("secure serving" , h .Check )
49
+ handler .AddReadinessCheck ("secure serving" , h .Check )
38
50
39
51
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
46
57
}
47
58
}()
48
59
49
- return nil
60
+ return h , nil
50
61
}
51
62
52
63
func (h * HealthCheck ) Check () error {
@@ -71,3 +82,23 @@ func (h *HealthCheck) Check() error {
71
82
72
83
return nil
73
84
}
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
+ }
0 commit comments