/
marathon.go
407 lines (350 loc) · 11 KB
/
marathon.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package marathon
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"sort"
"strings"
"github.com/QubitProducts/bamboo/configuration"
)
const taskStateRunning = "TASK_RUNNING"
// Describes an app process running
type Task struct {
Id string
Host string
Port int
Ports []int
Alive bool
State string
Ready bool
}
// A health check on the application
type HealthCheck struct {
// One of TCP, HTTP or COMMAND
Protocol string
// The path (if Protocol is HTTP)
Path string
// The position of the port targeted in the ports array
PortIndex int
}
// An app may have multiple processes
type App struct {
Id string
MesosDnsId string
EscapedId string
HealthCheckPath string
HealthCheckProtocol string
HealthChecks []HealthCheck
ReadinessCheckPath string
Tasks []Task
ServicePort int
ServicePorts []int
Env map[string]string
Labels map[string]string
SplitId []string
IpAddress AppIpAddress `json:"ipAddress"`
}
type AppIpAddress struct {
Discovery Discovery `json:"discovery"`
}
type Discovery struct {
Ports []Port `json:"ports"`
}
type Port struct {
Number int `json:"number"`
Name string `json:"name"`
Protocol string `json:"protocol"`
}
type TaskIpAddress struct {
IpAddress string `json:"ipAddress"`
Protocol string `json:"protocol"`
}
type AppList []App
func (slice AppList) Len() int {
return len(slice)
}
func (slice AppList) Less(i, j int) bool {
return slice[i].Id < slice[j].Id
}
func (slice AppList) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
type HealthCheckResult struct {
Alive bool
}
type marathonTask struct {
AppId string
Id string
Host string
Ports []int
ServicePorts []int
State string
StartedAt string
StagedAt string
Version string
IpAddresses []TaskIpAddress `json:"IpAddresses"`
HealthCheckResults []HealthCheckResult
}
type marathonTaskList []marathonTask
func (slice marathonTaskList) Len() int {
return len(slice)
}
func (slice marathonTaskList) Less(i, j int) bool {
return slice[i].Id < slice[j].Id
}
func (slice marathonTaskList) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}
type marathonApps struct {
Apps []marathonApp `json:"apps"`
}
type marathonApp struct {
Id string `json:"id"`
HealthChecks []marathonHealthCheck `json:"healthChecks"`
Ports []int `json:"ports"`
Env map[string]string `json:"env"`
Labels map[string]string `json:"labels"`
Deployments []deployment `json:"deployments"`
Tasks marathonTaskList `json:"tasks"`
ReadinessChecks []marathonReadinessCheck `json:"readinessChecks"`
ReadinessCheckResults []readinessCheckResult `json:"readinessCheckResults"`
IpAddress AppIpAddress `json:"ipAddress"`
}
type marathonHealthCheck struct {
Path string `json:"path"`
Protocol string `json:"protocol"`
PortIndex int `json:"portIndex"`
}
type marathonReadinessCheck struct {
Path string `json:"path"`
}
type deployment struct {
ID string `json:"id"`
}
type readinessCheckResult struct {
TaskID string `json:"taskId"`
Ready bool `json:"ready"`
}
/*
Apps returns a struct that describes Marathon current app and their
sub tasks information.
Parameters:
endpoint: Marathon HTTP endpoint, e.g. http://localhost:8080
*/
func FetchApps(maraconf configuration.Marathon, conf *configuration.Configuration) (AppList, error) {
var marathonApps []marathonApp
var err error
// Try all configured endpoints until one succeeds or we exhaust the list,
// whichever comes first.
for _, url := range maraconf.Endpoints() {
marathonApps, err = fetchMarathonApps(url, conf)
if err == nil {
for _, marathonApp := range marathonApps {
sort.Sort(marathonApp.Tasks)
}
apps := createApps(marathonApps)
sort.Sort(apps)
return apps, nil
}
}
// return last error
return nil, err
}
func fetchMarathonApps(endpoint string, conf *configuration.Configuration) ([]marathonApp, error) {
var appResponse marathonApps
if err := parseJSON(endpoint+"/v2/apps?embed=app.tasks&embed=app.deployments&embed=app.readiness", conf, &appResponse); err != nil {
return nil, err
}
return appResponse.Apps, nil
}
func parseJSON(url string, conf *configuration.Configuration, out interface{}) error {
client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
if len(conf.Marathon.User) > 0 && len(conf.Marathon.Password) > 0 {
req.SetBasicAuth(conf.Marathon.User, conf.Marathon.Password)
}
response, err := client.Do(req)
if err != nil {
return err
}
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
err = json.Unmarshal(contents, &out)
if err != nil {
return err
}
return nil
}
func createApps(marathonApps []marathonApp) AppList {
apps := AppList{}
for _, mApp := range marathonApps {
appId := mApp.Id
// Try to handle old app id format without slashes
appPath := "/" + strings.TrimPrefix(mApp.Id, "/")
// build App from marathonApp
app := App{
Id: appPath,
MesosDnsId: getMesosDnsId(appPath),
EscapedId: strings.Replace(appId, "/", "::", -1),
HealthCheckPath: parseHealthCheckPath(mApp.HealthChecks),
HealthCheckProtocol: parseHealthCheckProtocol(mApp.HealthChecks),
ReadinessCheckPath: parseReadinessCheckPath(mApp.ReadinessChecks),
Env: mApp.Env,
Labels: mApp.Labels,
SplitId: strings.Split(appId, "/"),
IpAddress: mApp.IpAddress,
}
app.HealthChecks = make([]HealthCheck, 0, len(mApp.HealthChecks))
for _, marathonCheck := range mApp.HealthChecks {
check := HealthCheck{
Protocol: marathonCheck.Protocol,
Path: marathonCheck.Path,
PortIndex: marathonCheck.PortIndex,
}
app.HealthChecks = append(app.HealthChecks, check)
}
if len(mApp.Ports) > 0 {
app.ServicePort = mApp.Ports[0]
app.ServicePorts = mApp.Ports
}
// build Tasks for this App
tasks := []Task{}
for _, mTask := range mApp.Tasks {
var host string
var port int
if len(mTask.Ports) > 0 {
host = mTask.Host
port = mTask.Ports[0]
}
if len(app.IpAddress.Discovery.Ports) > 0 {
if len(mTask.IpAddresses) > 0 {
host = mTask.IpAddresses[0].IpAddress
port = app.IpAddress.Discovery.Ports[0].Number
}
}
if host != "" && port != 0 {
t := Task{
Id: mTask.Id,
Host: host,
Port: port,
Ports: mTask.Ports,
Alive: calculateTaskHealth(mTask.HealthCheckResults, mApp.HealthChecks),
State: mTask.State,
Ready: calculateReadiness(mTask, mApp),
}
tasks = append(tasks, t)
}
}
app.Tasks = tasks
apps = append(apps, app)
}
return apps
}
func getMesosDnsId(appPath string) string {
// split up groups and recombine for how mesos-dns/consul/etc use service name
// "/nested/group/app" -> "app-group-nested"
groups := strings.Split(appPath, "/")
reverseGroups := []string{}
for i := len(groups) - 1; i >= 0; i-- {
if groups[i] != "" {
reverseGroups = append(reverseGroups, groups[i])
}
}
return strings.Join(reverseGroups, "-")
}
func parseHealthCheckPath(checks []marathonHealthCheck) string {
for _, check := range checks {
if check.Protocol != "HTTP" && check.Protocol != "HTTPS" {
continue
}
return check.Path
}
return ""
}
/* maybe combine this with the above? */
func parseHealthCheckProtocol(checks []marathonHealthCheck) string {
for _, check := range checks {
if check.Protocol != "HTTP" && check.Protocol != "HTTPS" {
continue
}
return check.Protocol
}
return ""
}
func parseReadinessCheckPath(checks []marathonReadinessCheck) string {
if len(checks) > 0 {
return checks[0].Path
}
return ""
}
func calculateTaskHealth(healthCheckResults []HealthCheckResult, healthChecks []marathonHealthCheck) bool {
// If we don't even have health check results for every health check, don't
// count the task as healthy.
if len(healthChecks) > len(healthCheckResults) {
return false
}
for _, healthCheck := range healthCheckResults {
if !healthCheck.Alive {
return false
}
}
return true
}
func calculateReadiness(task marathonTask, maraApp marathonApp) bool {
switch {
case task.State != taskStateRunning:
// By definition, a task not running cannot be ready.
log.Printf("task %s app %s: ready = false [task state %s != required state %s]", task.Id, maraApp.Id, task.State, taskStateRunning)
return false
case len(maraApp.Deployments) == 0:
// We only care about readiness during deployments; post-deployment readiness
// should be covered by a separate HAProxy health check definition.
log.Printf("task %s app %s: ready = true [no deployment ongoing]", task.Id, maraApp.Id)
return true
case len(maraApp.ReadinessChecks) == 0:
// Applications without configured readiness checks are always considered
// ready.
log.Printf("task %s app %s: ready = true [no readiness checks on app]", task.Id, maraApp.Id)
return true
}
// Loop through all readiness check results and return the results for
// matching task IDs.
for _, readinessCheckResult := range maraApp.ReadinessCheckResults {
if readinessCheckResult.TaskID == task.Id {
log.Printf("task %s app %s: ready = %t [evaluating readiness check ready state]", task.Id, maraApp.Id, readinessCheckResult.Ready)
return readinessCheckResult.Ready
}
}
// There's a corner case sometimes hit where the first new task of a
// deployment goes from TASK_STAGING to TASK_RUNNING without a corresponding
// health check result being included in the API response. This only happens
// in a very short (yet unlucky) time frame and does not repeat for subsequent
// tasks of the same deployment.
// We identify this situation by checking that we are looking at a part of the
// deployment representing a new task (i.e., it has the most recent version
// timestamp while other timestamps exist as well). If that's the case, we
// err on the side of caution and mark it as non-ready.
versions := map[string]bool{}
var maxVersion string
for _, task := range maraApp.Tasks {
versions[task.Version] = true
if maxVersion == "" || maxVersion < task.Version {
maxVersion = task.Version
}
}
if len(versions) > 1 && task.Version == maxVersion {
log.Printf("task %s app %s: ready = false [new task with version %s not included in readiness check results yet]", task.Id, maraApp.Id, maxVersion)
return false
}
// Finally, we can be certain this task is not part of the deployment (i.e.,
// it's an old task that's going to transition into the TASK_KILLING and/or
// TASK_KILLED state as new tasks' readiness checks gradually turn green.)
log.Printf("task %s app %s: ready = true [task not involved in deployment]", task.Id, maraApp.Id)
return true
}