-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
331 lines (285 loc) · 9.27 KB
/
main.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
"regexp"
"strconv"
"strings"
"time"
)
// CLI args and globals
var (
port = flag.String("shutdown-handler-port", "9001", "port to listen on")
host = flag.String("envoy-admin-host", "localhost", "envoy admin interface host")
admin = flag.String("envoy-admin-port", "9901", "envoy admin interface port")
scheme = flag.String("envoy-admin-scheme", "http", "envoy admin interface HTTP/S scheme")
delay = flag.Int("initial-delay-seconds", 0, "delay in seconds before starting shutdown")
period = flag.Int("check-period-seconds", 5, "period in seconds to pause while checking for active connections")
deadline = flag.Int("check-deadline-seconds", 300, "deadline in seconds to wait for active connections to close")
force = flag.Bool("force", false, "force shutdown when active connections are drained")
statsRegex = regexp.MustCompile("http[.]envoy[.]downstream_cx_active:[ ]+([0-9]+)")
complete chan bool // make(chan bool, 1)
adminBaseUrl string
)
// Helper methods
func failEnvoyHealthCheck() error {
resp, err := http.Post(adminBaseUrl+"/healthcheck/fail", "text/plain", nil)
if err != nil {
return fmt.Errorf("failed to send shutdown request to envoy admin interface: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to send shutdown request to envoy admin interface: %v", resp.Status)
}
return nil
}
func countDownstreamCnx() (int, error) {
resp, err := http.Get(adminBaseUrl + "/stats?filter=http.envoy.downstream_cx_active")
if err != nil {
log.Printf("Failed to get envoy stats: %v", err)
return -1, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return -1, fmt.Errorf("failed to get envoy stats: %v", resp.Status)
}
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return -1, err
}
bodyString := strings.TrimSpace(string(bodyBytes))
count, err := parseDownstreamCnx(bodyString)
if err != nil {
log.Printf("Failed to query or parse envoy stats: %v", err)
} else {
log.Printf("Received envoy stats: %v downstream connections open", count)
}
return count, err
}
func parseDownstreamCnx(body string) (int, error) {
matches := statsRegex.FindStringSubmatch(body)
if len(matches) != 2 {
return -1, fmt.Errorf("failed to parse envoy downstream connections from string: \"%v\"", body)
}
return strconv.Atoi(matches[1])
}
func startGracefulDraining() error {
resp, err := http.Post(adminBaseUrl+"/drain_listeners?graceful", "text/plain", nil)
if err != nil {
return fmt.Errorf("failed to start graceful draining: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to start graceful draining: %v", resp.Status)
}
return nil
}
func forceEnvoyShutdown() error {
resp, err := http.Post(adminBaseUrl+"/quitquitquit", "text/plain", nil)
if err != nil {
return fmt.Errorf("failed to send force shutdown request to envoy admin interface: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to send force shutdown request to envoy admin interface: %v", resp.Status)
}
return nil
}
func defaultIntFromQuery(r *http.Request, key string, def int) (int, error) {
if vals, ok := r.URL.Query()[key]; ok {
if vals[0] != "" {
val, err := strconv.Atoi(vals[0])
if err == nil {
return val, nil
}
return def, err
}
}
return def, nil
}
func defaultNonNegIntFromQuery(r *http.Request, key string, def int) (int, error) {
val, err := defaultIntFromQuery(r, key, def)
if err != nil {
return val, err
}
if val < 0 {
return def, fmt.Errorf("invalid negative value for %v: %d", key, val)
}
return val, nil
}
func parseCommonQueryParams(r *http.Request) (int, int, int, error) {
qDelay, err := defaultNonNegIntFromQuery(r, "delay", *delay)
if err != nil {
return -1, -1, -1, err
}
qPeriod, err := defaultNonNegIntFromQuery(r, "period", *period)
if err != nil {
return -1, -1, -1, err
}
qDeadline, err := defaultNonNegIntFromQuery(r, "deadline", *deadline)
if err != nil {
return -1, -1, -1, err
}
return qDelay, qPeriod, qDeadline, nil
}
// HTTP handlers
func alive(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
func ready(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
func checkStats(w http.ResponseWriter, r *http.Request) {
log.Print("Stats check request received")
c, err := countDownstreamCnx()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("error getting downstream connections: %v\n", err)))
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(fmt.Sprintf("http.envoy.downstream_cx_active: %v\n", c)))
}
func shutdown(w http.ResponseWriter, r *http.Request) {
var err error
log.Print("Shutdown request received")
qDelay, qPeriod, qDeadline, err := parseCommonQueryParams(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("error: %v\n", err)))
return
}
if qDeadline < qDelay+qPeriod {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("error: deadline (%d) too short relative to delay (%d) and period (%d)\n", qDeadline, qDelay, qPeriod)))
return
}
started := time.Now()
complete = make(chan bool, 1)
// tell envoy to fail it's healthchecks first
err = failEnvoyHealthCheck()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("error: %v\n", err)))
complete <- false
return
}
// delay if asked, to allow for healthcheck failures to be observed
if qDelay > 0 {
log.Printf("Delaying graceful shutdown by %v seconds", qDelay)
time.Sleep(time.Duration(qDelay) * time.Second)
}
// check for active connections, if there are none immediate shutdown is safe
c, err := countDownstreamCnx()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("error: %v\n", err)))
complete <- false
return
}
if c == 0 {
log.Print("No active downstream connections, can shut down")
w.WriteHeader(http.StatusOK)
w.Write([]byte("No active downstream connections, can shut down"))
complete <- true
return
}
// there were active connections, so start graceful draining
err = startGracefulDraining()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("error: %v\n", err)))
complete <- false
return
}
log.Printf("Waiting for %v active downstream connections to close", c)
for {
c, err := countDownstreamCnx()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(fmt.Sprintf("error: %v\n", err)))
complete <- false
return
}
if c == 0 {
log.Print("All downstream connections closed, can shut down")
break
}
if time.Since(started) > time.Duration(qDeadline)*time.Second {
log.Print("Timeout waiting for downstream connections to close")
w.WriteHeader(http.StatusRequestTimeout)
w.Write([]byte("Timeout waiting for downstream connections to close\n"))
complete <- true
return
}
time.Sleep(time.Duration(qPeriod) * time.Second)
}
// shut envoy down now explicitly if "force" flag is set
if *force {
err = forceEnvoyShutdown()
if err != nil {
log.Printf("Failed to force envoy shutdown: %v", err)
}
}
// return to allow SIGTERMs to be sent to the containers
w.WriteHeader(http.StatusOK)
w.Write([]byte("All downstream connections closed, can shut down\n"))
// signal that the shutdown is complete to any channel listeners
complete <- true
}
func waitForShutdown(w http.ResponseWriter, r *http.Request) {
log.Print("Waiting for shutdown")
qDelay, qPeriod, qDeadline, err := parseCommonQueryParams(r)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("error: %v\n", err)))
return
}
if qDeadline < qDelay+qPeriod {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(fmt.Sprintf("error: deadline (%d) too short relative to delay (%d) and period (%d)\n", qDeadline, qDelay, qPeriod)))
return
}
started := time.Now()
// delay if asked, to allow for healthcheck failures to be observed
if qDelay > 0 {
log.Printf("Delaying wait for shutdown by %v seconds", qDelay)
time.Sleep(time.Duration(qDelay) * time.Second)
}
for complete == nil {
time.Sleep(time.Duration(qPeriod) * time.Second)
if time.Since(started) > time.Duration(qDeadline)*time.Second {
log.Print("Timeout waiting for shutdown to complete")
w.WriteHeader(http.StatusRequestTimeout)
w.Write([]byte("Timeout waiting for shutdown to complete\n"))
return
}
}
success := <-complete
if success {
w.WriteHeader(http.StatusOK)
w.Write([]byte("shutdown completed\n"))
} else {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("an error occurred during shutdown\n"))
}
complete = nil
}
// Runtime (no signal handling)
func main() {
flag.Parse()
adminBaseUrl = *scheme + "://" + *host + ":" + *admin
log.Print("Running envoy shutdown handler server on " + *port)
http.HandleFunc("/health/alive", alive)
http.HandleFunc("/health/ready", ready)
http.HandleFunc("/check/stats", checkStats)
http.HandleFunc("/shutdown", shutdown)
http.HandleFunc("/waitforshutdown", waitForShutdown)
err := http.ListenAndServe(":"+*port, nil)
log.Printf("%v", err)
}