Debian Codegraph @

pkg/server/healthz.go

raw
1 /*
2 Copyright 2016 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package server
18
19 import (
20 "fmt"
21 "net/http"
22 "sync"
23 "time"
24
25 "k8s.io/apiserver/pkg/server/healthz"
26 "k8s.io/utils/clock"
27 )
28
29 // healthMux is an interface describing the methods InstallHandler requires.
30 type healthMux interface {
31 Handle(pattern string, handler http.Handler)
32 }
33
34 type healthCheckRegistry struct {
35 path string
36 lock sync.Mutex
37 checks []healthz.HealthChecker
38 checksInstalled bool
39 clock clock.Clock
40 }
41
42 func (reg *healthCheckRegistry) addHealthChecks(checks ...healthz.HealthChecker) error {
43 return reg.addDelayedHealthChecks(0, checks...)
44 }
45
46 func (reg *healthCheckRegistry) addDelayedHealthChecks(delay time.Duration, checks ...healthz.HealthChecker) error {
47 if delay > 0 && reg.clock == nil {
48 return fmt.Errorf("nil clock in healthCheckRegistry for %s endpoint", reg.path)
49 }
50 reg.lock.Lock()
51 defer reg.lock.Unlock()
52 if reg.checksInstalled {
53 return fmt.Errorf("unable to add because the %s endpoint has already been created", reg.path)
54 }
55 if delay > 0 {
56 for _, check := range checks {
57 reg.checks = append(reg.checks, delayedHealthCheck(check, reg.clock, delay))
58 }
59 } else {
60 reg.checks = append(reg.checks, checks...)
61 }
62 return nil
63 }
64
65 func (reg *healthCheckRegistry) installHandler(mux healthMux) {
66 reg.installHandlerWithHealthyFunc(mux, nil)
67 }
68
69 func (reg *healthCheckRegistry) installHandlerWithHealthyFunc(mux healthMux, firstTimeHealthy func()) {
70 reg.lock.Lock()
71 defer reg.lock.Unlock()
72 reg.checksInstalled = true
73 healthz.InstallPathHandlerWithHealthyFunc(mux, reg.path, firstTimeHealthy, reg.checks...)
74 }
75
76 // AddHealthChecks adds HealthCheck(s) to health endpoints (healthz, livez, readyz) but
77 // configures the liveness grace period to be zero, which means we expect this health check
78 // to immediately indicate that the apiserver is unhealthy.
79 func (s *GenericAPIServer) AddHealthChecks(checks ...healthz.HealthChecker) error {
80 // we opt for a delay of zero here, because this entrypoint adds generic health checks
81 // and not health checks which are specifically related to kube-apiserver boot-sequences.
82 return s.addHealthChecks(0, checks...)
83 }
84
85 // AddBootSequenceHealthChecks adds health checks to the old healthz endpoint (for backwards compatibility reasons)
86 // as well as livez and readyz. The livez grace period is defined by the value of the
87 // command-line flag --livez-grace-period; before the grace period elapses, the livez health checks
88 // will default to healthy. One may want to set a grace period in order to prevent the kubelet from restarting
89 // the kube-apiserver due to long-ish boot sequences. Readyz health checks, on the other hand, have no grace period,
90 // since readyz should fail until boot fully completes.
91 func (s *GenericAPIServer) AddBootSequenceHealthChecks(checks ...healthz.HealthChecker) error {
92 return s.addHealthChecks(s.livezGracePeriod, checks...)
93 }
94
95 // addHealthChecks adds health checks to healthz, livez, and readyz. The delay passed in will set
96 // a corresponding grace period on livez.
97 func (s *GenericAPIServer) addHealthChecks(livezGracePeriod time.Duration, checks ...healthz.HealthChecker) error {
98 if err := s.healthzRegistry.addHealthChecks(checks...); err != nil {
99 return err
100 }
101 if err := s.livezRegistry.addDelayedHealthChecks(livezGracePeriod, checks...); err != nil {
102 return err
103 }
104 return s.readyzRegistry.addHealthChecks(checks...)
105 }
106
107 // AddReadyzChecks allows you to add a HealthCheck to readyz.
108 func (s *GenericAPIServer) AddReadyzChecks(checks ...healthz.HealthChecker) error {
109 return s.readyzRegistry.addHealthChecks(checks...)
110 }
111
112 // AddLivezChecks allows you to add a HealthCheck to livez.
113 func (s *GenericAPIServer) AddLivezChecks(delay time.Duration, checks ...healthz.HealthChecker) error {
114 return s.livezRegistry.addDelayedHealthChecks(delay, checks...)
115 }
116
117 // addReadyzShutdownCheck is a convenience function for adding a readyz shutdown check, so
118 // that we can register that the api-server is no longer ready while we attempt to gracefully
119 // shutdown.
120 func (s *GenericAPIServer) addReadyzShutdownCheck(stopCh <-chan struct{}) error {
121 return s.AddReadyzChecks(healthz.NewShutdownHealthz(stopCh))
122 }
123
124 // installHealthz creates the healthz endpoint for this server
125 func (s *GenericAPIServer) installHealthz() {
126 s.healthzRegistry.installHandler(s.Handler.NonGoRestfulMux)
127 }
128
129 // installReadyz creates the readyz endpoint for this server.
130 func (s *GenericAPIServer) installReadyz() {
131 s.readyzRegistry.installHandlerWithHealthyFunc(s.Handler.NonGoRestfulMux, func() {
132 // note: installHandlerWithHealthyFunc guarantees that this is called only once
133 s.lifecycleSignals.HasBeenReady.Signal()
134 })
135 }
136
137 // installLivez creates the livez endpoint for this server.
138 func (s *GenericAPIServer) installLivez() {
139 s.livezRegistry.installHandler(s.Handler.NonGoRestfulMux)
140 }
141
142 // delayedHealthCheck wraps a health check which will not fail until the explicitly defined delay has elapsed. This
143 // is intended for use primarily for livez health checks.
144 func delayedHealthCheck(check healthz.HealthChecker, clock clock.Clock, delay time.Duration) healthz.HealthChecker {
145 return delayedLivezCheck{
146 check,
147 clock.Now().Add(delay),
148 clock,
149 }
150 }
151
152 type delayedLivezCheck struct {
153 check healthz.HealthChecker
154 startCheck time.Time
155 clock clock.Clock
156 }
157
158 func (c delayedLivezCheck) Name() string {
159 return c.check.Name()
160 }
161
162 func (c delayedLivezCheck) Check(req *http.Request) error {
163 if c.clock.Now().After(c.startCheck) {
164 return c.check.Check(req)
165 }
166 return nil
167 }