forked from projectcalico/libcalico-go
/
health.go
256 lines (226 loc) · 8.6 KB
/
health.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright (c) 2017-2019 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package health
import (
"fmt"
"net/http"
"sync"
"time"
log "github.com/sirupsen/logrus"
)
// The HealthReport struct has slots for the levels of health that we monitor and aggregate.
type HealthReport struct {
Live bool
Ready bool
}
type reporterState struct {
// The reporter's name.
name string
// The health indicators that this reporter reports.
reports HealthReport
// Expiry time for this reporter's reports. Zero means that reports never expire.
timeout time.Duration
// The most recent report.
latest HealthReport
// Time of that most recent report.
timestamp time.Time
}
// TimedOut checks whether the reporter is due for another report. This is the case when
// the reports are configured to expire and the time since the last report exceeds the report timeout duration.
func (r *reporterState) TimedOut() bool {
return r.timeout != 0 && time.Since(r.timestamp) > r.timeout
}
// A HealthAggregator receives health reports from individual reporters (which are typically
// components of a particular daemon or application) and aggregates them into an overall health
// summary. For each monitored kind of health, all of the reporters that report that need to say
// that it is good; for example, to be 'ready' overall, all of the reporters that report readiness
// need to have recently said 'Ready: true'.
type HealthAggregator struct {
// Mutex to protect concurrent access to this health aggregator.
mutex *sync.Mutex
// The previous health summary report which is cached so that we log only when the overall health report changes.
lastReport *HealthReport
// Map from reporter name to corresponding state.
reporters map[string]*reporterState
// HTTP server mux. This is where we register handlers for particular URLs.
httpServeMux *http.ServeMux
// HTTP server. Non-nil when there should be a server running.
httpServer *http.Server
}
// RegisterReporter registers a reporter with a HealthAggregator. The aggregator uses NAME to
// identify the reporter. REPORTS indicates the kinds of health that this reporter will report.
// TIMEOUT is the expiry time for this reporter's reports; the implication of which is that the
// reporter should normally refresh its reports well before this time has expired.
func (aggregator *HealthAggregator) RegisterReporter(name string, reports *HealthReport, timeout time.Duration) {
aggregator.mutex.Lock()
defer aggregator.mutex.Unlock()
aggregator.reporters[name] = &reporterState{
name: name,
reports: *reports,
timeout: timeout,
latest: HealthReport{Live: true},
timestamp: time.Now(),
}
return
}
// Report reports current health from a reporter to a HealthAggregator. NAME is the reporter's name
// and REPORTS conveys the current status, for each kind of health that the reporter said it was
// going to report when it called RegisterReporter.
func (aggregator *HealthAggregator) Report(name string, report *HealthReport) {
aggregator.mutex.Lock()
defer aggregator.mutex.Unlock()
reporter := aggregator.reporters[name]
reporter.latest = *report
reporter.timestamp = time.Now()
return
}
func NewHealthAggregator() *HealthAggregator {
aggregator := &HealthAggregator{
mutex: &sync.Mutex{},
lastReport: &HealthReport{},
reporters: map[string]*reporterState{},
httpServeMux: http.NewServeMux(),
}
aggregator.httpServeMux.HandleFunc("/readiness", func(rsp http.ResponseWriter, req *http.Request) {
log.Debug("GET /readiness")
status := StatusBad
if aggregator.Summary().Ready {
log.Debug("Felix is ready")
status = StatusGood
}
rsp.WriteHeader(status)
})
aggregator.httpServeMux.HandleFunc("/liveness", func(rsp http.ResponseWriter, req *http.Request) {
log.Debug("GET /liveness")
status := StatusBad
if aggregator.Summary().Live {
log.Debug("Felix is live")
status = StatusGood
}
rsp.WriteHeader(status)
})
return aggregator
}
// Summary calculates the current overall health for a HealthAggregator.
func (aggregator *HealthAggregator) Summary() *HealthReport {
aggregator.mutex.Lock()
defer aggregator.mutex.Unlock()
var failedLivenessChecks []*reporterState
var failedReadinessChecks []*reporterState
// In the absence of any reporters, default to indicating that we are both live and ready.
summary := &HealthReport{Live: true, Ready: true}
// Now for each reporter...
for _, reporter := range aggregator.reporters {
// Reset Live to false if that reporter is registered to report liveness and hasn't
// recently said that it is live.
stillLive := reporter.latest.Live && !reporter.TimedOut()
if summary.Live && reporter.reports.Live && !stillLive {
summary.Live = false
}
// Reset Ready to false if that reporter is registered to report readiness and
// hasn't recently said that it is ready.
stillReady := reporter.latest.Ready && !reporter.TimedOut()
if summary.Ready && reporter.reports.Ready && !stillReady {
summary.Ready = false
}
if reporter.reports.Live && !stillLive {
failedLivenessChecks = append(failedLivenessChecks, reporter)
}
if reporter.reports.Ready && !stillReady {
failedReadinessChecks = append(failedReadinessChecks, reporter)
}
if reporter.reports.Live && reporter.reports.Ready && stillLive && stillReady {
log.WithFields(log.Fields{
"name": reporter.name,
"reporter-state": reporter,
}).Debug("Reporter is healthy")
}
}
// Summary status has changed so update previous status and log.
if summary.Live != aggregator.lastReport.Live || summary.Ready != aggregator.lastReport.Ready {
aggregator.lastReport = summary
log.WithField("lastSummary", summary).Info("Overall health status changed")
for _, reporter := range failedLivenessChecks {
log.WithFields(log.Fields{
"name": reporter.name,
"reporter-state": reporter,
}).Warn("Reporter failed liveness checks")
}
for _, reporter := range failedReadinessChecks {
log.WithFields(log.Fields{
"name": reporter.name,
"reporter-state": reporter,
}).Warn("Reporter failed readiness checks")
}
}
return summary
}
const (
// The HTTP status that we use for 'ready' or 'live'. 204 means "No Content: The server
// successfully processed the request and is not returning any content." (Kubernetes
// interpets any 200<=status<400 as 'good'.)
StatusGood = 204
// The HTTP status that we use for 'not ready' or 'not live'. 503 means "Service
// Unavailable: The server is currently unavailable (because it is overloaded or down for
// maintenance). Generally, this is a temporary state." (Kubernetes interpets any
// status>=400 as 'bad'.)
StatusBad = 503
)
// ServeHTTP publishes the current overall liveness and readiness at http://HOST:PORT/liveness and
// http://HOST:PORT/readiness respectively. A GET request on those URLs returns StatusGood or
// StatusBad, according to the current overall liveness or readiness. These endpoints are designed
// for use by Kubernetes liveness and readiness probes.
func (aggregator *HealthAggregator) ServeHTTP(enabled bool, host string, port int) {
aggregator.mutex.Lock()
defer aggregator.mutex.Unlock()
if enabled {
logCxt := log.WithFields(log.Fields{
"host": host,
"port": port,
})
if aggregator.httpServer != nil {
logCxt.Info("Health enabled. Server is already running.")
return
}
logCxt.Info("Health enabled. Starting server.")
aggregator.httpServer = &http.Server{
Addr: fmt.Sprintf("%s:%v", host, port),
Handler: aggregator.httpServeMux,
}
go func() {
for {
server := aggregator.getHTTPServer()
if server == nil {
// HTTP serving is now disabled.
break
}
err := server.ListenAndServe()
log.WithError(err).Error(
"Health endpoint failed, trying to restart it...")
time.Sleep(1 * time.Second)
}
}()
} else {
if aggregator.httpServer != nil {
log.Info("Health disabled. Stopping server.")
aggregator.httpServer.Close()
aggregator.httpServer = nil
}
}
}
func (aggregator *HealthAggregator) getHTTPServer() *http.Server {
aggregator.mutex.Lock()
defer aggregator.mutex.Unlock()
return aggregator.httpServer
}