-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhealthchecks.go
More file actions
295 lines (247 loc) · 8.66 KB
/
healthchecks.go
File metadata and controls
295 lines (247 loc) · 8.66 KB
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
package parser
import (
"fmt"
"strconv"
"strings"
"github.com/drycc/workflow-cli/cmd"
docopt "github.com/docopt/docopt-go"
"github.com/drycc/controller-sdk-go/api"
)
// TODO: This is for supporting backward compatibility and should be removed
// in future when next major version will be released.
const (
defaultProcType string = "web"
)
// Healthchecks routes ealthcheck commands to their specific function
func Healthchecks(argv []string, cmdr cmd.Commander) error {
usage := `
Valid commands for healthchecks:
healthchecks:list list healthchecks for an app
healthchecks:set set healthchecks for an app
healthchecks:unset unset healthchecks for an app
Use 'drycc help [command]' to learn more.
`
switch argv[0] {
case "healthchecks:list":
return healthchecksList(argv, cmdr)
case "healthchecks:set":
return healthchecksSet(argv, cmdr)
case "healthchecks:unset":
return healthchecksUnset(argv, cmdr)
default:
if printHelp(argv, usage) {
return nil
}
if argv[0] == "healthchecks" {
argv[0] = "healthchecks:list"
return healthchecksList(argv, cmdr)
}
PrintUsage(cmdr)
return nil
}
}
func healthchecksList(argv []string, cmdr cmd.Commander) error {
usage := `
Lists healthchecks for an application.
Usage: drycc healthchecks:list [options]
Options:
-a --app=<app>
the uniquely identifiable name of the application.
--type=<type>
the procType for which the health check needs to be listed.
`
args, err := docopt.ParseArgs(usage, argv, "")
if err != nil {
return err
}
app := safeGetString(args, "--app")
procType := safeGetString(args, "--type")
return cmdr.HealthchecksList(app, procType)
}
func healthchecksSet(argv []string, cmdr cmd.Commander) error {
usage := `
Sets healthchecks for an application.
By default, Workflow only checks that the application starts in their Container. A health
check may be added by configuring a health check probe for the application. The health
checks are implemented as Kubernetes Container Probes. A 'startupProbe' 'livenessProbe'
and a 'readinessProbe' can be configured, and each probe can be of type 'httpGet', 'exec'
or 'tcpSocket' depending on the type of probe the Container requires.
A 'startupProbe' indicates whether the application within the container is started.
All other probes are disabled if a startup probe is provided, until it succeeds.
If the startup probe fails, the container is subjected to its restart policy.
A 'livenessProbe' is useful for applications running for long periods of time, eventually
transitioning to broken states and cannot recover except by restarting them.
Other times, a 'readinessProbe' is useful when the Container is only temporarily unable
to serve, and will recover on its own. In this case, if a Container fails its 'readinessProbe'
, the Container will not be shut down, but rather the Container will stop receiving
incoming requests.
'httpGet' probes are just as it sounds: it performs a HTTP GET operation on the Container.
A response code inside the 200-399 range is considered a pass. 'httpGet' probes accept a
port number to perform the HTTP GET operation on the Container.
'exec' probes run a command inside the Container to determine its health. An exit code of
zero is considered a pass, while a non-zero status code is considered a fail. 'exec'
probes accept a string of arguments to be run inside the Container.
'tcpSocket' probes attempt to open a socket in the Container. The Container is only
considered healthy if the check can establish a connection. 'tcpSocket' probes accept a
port number to perform the socket connection on the Container.
Usage: drycc healthchecks:set <health-type> <probe-type> [options] [--] <args>...
Arguments:
<health-type>
the healthcheck type, such as 'startupProbe' 'livenessProbe' or 'readinessProbe'.
<probe-type>
the healthcheck probe type, such as 'httpGet', 'exec' or 'tcpSocket'.
<args>
The arguments required for the healthcheck probe. 'exec', accepts a list of arguments;
'httpGet' and 'tcpSocket' accept a port number.
Options:
-a --app=<app>
the uniquely identifiable name for the application.
-p --path=<path>
the relative URL path for 'httpGet' probes. [default: /]
--type=<type>
the procType for which the health check needs to be applied.
--headers=<headers>...
the HTTP headers to send for 'httpGet' probes, separated by commas.
--initial-delay-timeout=<initial-delay-timeout>
the initial delay timeout for the probe [default: 50]
--timeout-seconds=<timeout-seconds>
the number of seconds after which the probe times out [default: 50]
--period-seconds=<period-seconds>
how often (in seconds) to perform the probe [default: 10]
--success-threshold=<success-threshold>
minimum consecutive successes for the probe to be considered successful after having failed [default: 1]
--failure-threshold=<failure-threshold>
minimum consecutive successes for the probe to be considered failed after having succeeded [default: 3]
`
args, err := docopt.ParseArgs(usage, argv, "")
if err != nil {
return err
}
app := safeGetString(args, "--app")
path := safeGetString(args, "--path")
procType := safeGetString(args, "--type")
initialDelayTimeout := safeGetInt(args, "--initial-delay-timeout")
timeoutSeconds := safeGetInt(args, "--timeout-seconds")
periodSeconds := safeGetInt(args, "--period-seconds")
successThreshold := safeGetInt(args, "--success-threshold")
failureThreshold := safeGetInt(args, "--failure-threshold")
headers := []string{}
if args["--headers"] != nil {
headers = strings.Split(args["--headers"].(string), ",")
}
if procType == "" {
procType = defaultProcType
}
healthcheckType := args["<health-type>"].(string)
probeType := args["<probe-type>"].(string)
probeArgs := args["<args>"].([]string)
if err := checkProbeType(healthcheckType); err != nil {
return err
}
probe := &api.Healthcheck{
InitialDelaySeconds: initialDelayTimeout,
TimeoutSeconds: timeoutSeconds,
PeriodSeconds: periodSeconds,
SuccessThreshold: successThreshold,
FailureThreshold: failureThreshold,
}
switch probeType {
case "httpGet":
parsedHeaders, err := parseHeaders(headers)
if err != nil {
return fmt.Errorf("could not parse headers: %s", err)
}
port, err := strconv.Atoi(probeArgs[0])
if err != nil {
return fmt.Errorf("could not parse port: %s", err)
}
probe.HTTPGet = &api.HTTPGetProbe{
Path: path,
Port: port,
HTTPHeaders: parsedHeaders,
}
case "exec":
probe.Exec = &api.ExecProbe{
Command: probeArgs,
}
case "tcpSocket":
port, err := strconv.Atoi(probeArgs[0])
if err != nil {
return fmt.Errorf("could not parse port: %s", err)
}
probe.TCPSocket = &api.TCPSocketProbe{
Port: port,
}
default:
return fmt.Errorf("invalid probe type. Must be one of: \"httpGet\", \"exec\"")
}
return cmdr.HealthchecksSet(app, healthcheckType, procType, probe)
}
func healthchecksUnset(argv []string, cmdr cmd.Commander) error {
usage := `
Unsets healthchecks for an application.
Usage: drycc healthchecks:unset [options] <health-type>...
Arguments:
<health-type>
the healthcheck type, such as 'liveness' or 'readiness'.
Options:
-a --app=<app>
the uniquely identifiable name for the application.
--type=<type>
the procType for which the health check needs to be removed.
`
args, err := docopt.ParseArgs(usage, argv, "")
if err != nil {
return err
}
app := safeGetString(args, "--app")
healthchecks := args["<health-type>"].([]string)
procType := safeGetString(args, "--type")
if procType == "" {
procType = defaultProcType
}
for healthcheck := range healthchecks {
if err := checkProbeType(healthchecks[healthcheck]); err != nil {
return err
}
}
return cmdr.HealthchecksUnset(app, procType, healthchecks)
}
func parseHeaders(headers []string) ([]*api.KVPair, error) {
var parsedHeaders []*api.KVPair
for _, header := range headers {
parsedHeader, err := parseHeader(header)
if err != nil {
return nil, err
}
parsedHeaders = append(parsedHeaders, parsedHeader)
}
return parsedHeaders, nil
}
func parseHeader(header string) (*api.KVPair, error) {
headerParts := strings.SplitN(header, ":", 2)
if len(headerParts) != 2 {
return nil, fmt.Errorf("could not find separator in header (%s)", header)
}
return &api.KVPair{
Name: strings.TrimSpace(headerParts[0]),
Value: strings.TrimSpace(headerParts[1]),
}, nil
}
func checkProbeType(probe string) error {
var found bool
probeTypes := []string{
"startupProbe",
"livenessProbe",
"readinessProbe",
}
for _, ptype := range probeTypes {
if probe == ptype {
found = true
}
}
if !found {
return fmt.Errorf("probe type %s is invalid. Must be one of %s", probe, probeTypes)
}
return nil
}