-
Notifications
You must be signed in to change notification settings - Fork 25
/
nomad.go
464 lines (381 loc) · 11.4 KB
/
nomad.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package clients
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/hashicorp/go-hclog"
"github.com/shipyard-run/shipyard/pkg/utils"
"golang.org/x/xerrors"
)
// Nomad defines an interface for a Nomad client
type Nomad interface {
// SetConfig for the client, path is a valid Nomad JSON config file
SetConfig(utils.ClusterConfig, string) error
// Create jobs in the provided files
Create(files []string) error
// Stop jobs in the provided files
Stop(files []string) error
// ParseJob in the given file and return a JSON blob representing the HCL job
ParseJob(file string) ([]byte, error)
// JobRunning returns true if all allocations for a job are running
JobRunning(job string) (bool, error)
// HealthCheckAPI uses the Nomad API to check that all servers and nodes
// are ready. The function will block until either all nodes are healthy or the
// timeout period elapses.
HealthCheckAPI(time.Duration) error
// Endpoints returns a list of endpoints for a cluster
Endpoints(job, group, task string) ([]map[string]string, error)
}
// NomadImpl is an implementation of the Nomad interface
type NomadImpl struct {
httpClient HTTP
l hclog.Logger
c *utils.ClusterConfig
backoff time.Duration
context string
}
// NewNomad creates a new Nomad client
func NewNomad(c HTTP, backoff time.Duration, l hclog.Logger) Nomad {
return &NomadImpl{httpClient: c, l: l, backoff: backoff}
}
type validateRequest struct {
JobHCL string
Canonicalize bool
}
type createRequest struct {
Job string
}
// SetConfig loads the Nomad config from a file
func (n *NomadImpl) SetConfig(nomadconfig utils.ClusterConfig, context string) error {
n.c = &nomadconfig
n.context = context
return nil
}
// HealthCheckAPI executes a HTTP heathcheck for a Nomad cluster
func (n *NomadImpl) HealthCheckAPI(timeout time.Duration) error {
// get the address and the nodecount from the config
address := n.c.APIAddress(utils.Context(n.context))
nodeCount := n.c.NodeCount
n.l.Debug("Performing Nomad health check", "address", address)
st := time.Now()
for {
if time.Now().Sub(st) > timeout {
n.l.Error("Timeout wating for Nomad healthcheck", "address", address)
return fmt.Errorf("Timeout waiting for Nomad healthcheck %s", address)
}
rq, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v1/nodes", address), nil)
if err != nil {
return err
}
resp, err := n.httpClient.Do(rq)
if err == nil && resp.StatusCode == 200 {
nodes := []map[string]interface{}{}
// check number of nodes
json.NewDecoder(resp.Body).Decode(&nodes)
// loop nodes and check ready
readyCount := 0
for _, node := range nodes {
// get the node status
nodeStatus := node["Status"].(string)
nodeName := node["Name"].(string)
nodeEligable := node["SchedulingEligibility"].(string)
n.l.Debug("Node status", "node", nodeName, "status", nodeStatus, "eligible", nodeEligable)
// get the driver status
drivers, ok := node["Drivers"].(map[string]interface{})
if !ok {
continue
}
var driversHealthy = true
for k, v := range drivers {
driver, ok := v.(map[string]interface{})
if !ok {
continue
}
healthy, ok := driver["Healthy"].(bool)
if !ok {
continue
}
detected, ok := driver["Detected"].(bool)
if !ok || !detected {
continue
}
n.l.Debug("Driver status", "node", nodeName, "driver", k, "healthy", healthy)
if !healthy {
driversHealthy = false
}
}
if nodeStatus == "ready" && nodeEligable == "eligible" && driversHealthy {
readyCount++
}
}
if readyCount == nodeCount {
n.l.Debug("Nomad check complete", "address", address)
return nil
}
n.l.Debug("Nodes not ready", "ready", readyCount, "nodes", nodeCount)
}
// backoff
time.Sleep(n.backoff)
}
}
// Create jobs in the Nomad cluster for the given files and wait until all jobs are running
func (n *NomadImpl) Create(files []string) error {
for _, f := range files {
// parse the job
jsonJob, err := n.ParseJob(f)
if err != nil {
return err
}
// submit the job top the API
cr := fmt.Sprintf(`{"Job": %s}`, string(jsonJob))
r, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/v1/jobs", n.c.APIAddress(utils.Context(n.context))), bytes.NewReader([]byte(cr)))
if err != nil {
return xerrors.Errorf("Unable to create http request: %w", err)
}
resp, err := n.httpClient.Do(r)
if err != nil {
return xerrors.Errorf("Unable to submit job: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
// try to read the body for the error
d, _ := ioutil.ReadAll(resp.Body)
return xerrors.Errorf("Error submitting job, got status code %d, error: %s", resp.StatusCode, string(d))
}
}
return nil
}
// Stop the jobs defined in the files for the referenced Nomad cluster
func (n *NomadImpl) Stop(files []string) error {
for _, f := range files {
id, err := n.getJobID(f)
if err != nil {
return err
}
// stop the job
r, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("%s/v1/job/%s", n.c.APIAddress(utils.Context(n.context)), id), nil)
if err != nil {
return xerrors.Errorf("Unable to create http request: %w", err)
}
resp, err := n.httpClient.Do(r)
if err != nil {
return xerrors.Errorf("Unable to submit job: %w", err)
}
if resp.StatusCode != http.StatusOK {
return xerrors.Errorf("Error submitting job, got status code %d", resp.StatusCode)
}
}
return nil
}
// ParseJob validates a HCL job file with the Nomad API and returns a slice of
// bytes representing the JSON payload.
func (n *NomadImpl) ParseJob(file string) ([]byte, error) {
// load the file
d, err := ioutil.ReadFile(file)
if err != nil {
return nil, xerrors.Errorf("Unable to read file %s: %w", file, err)
}
// build the request object
rd := validateRequest{
JobHCL: string(d),
}
jobData, _ := json.Marshal(rd)
// validate the config with the Nomad API
r, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/v1/jobs/parse", n.c.APIAddress(utils.Context(n.context))), bytes.NewReader(jobData))
if err != nil {
return nil, xerrors.Errorf("Unable to create http request: %w", err)
}
resp, err := n.httpClient.Do(r)
if err != nil {
return nil, xerrors.Errorf("Unable to validate job: %w", err)
}
if resp.StatusCode != http.StatusBadRequest && resp.StatusCode != http.StatusOK {
return nil, xerrors.Errorf("Error validating job Nomad API returned an internal error")
}
defer resp.Body.Close()
jsonJob, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, xerrors.Errorf("Unable to read response from Nomad API: %w", err)
}
if resp.StatusCode == http.StatusBadRequest {
return nil, xerrors.Errorf("Error validating job, job file contains errors: %s", jsonJob)
}
return jsonJob, nil
}
// JobRunning returns true when all allocations for a job are running
func (n *NomadImpl) JobRunning(job string) (bool, error) {
jobDetail, err := n.getJobAllocations(job)
if err != nil {
return false, err
}
if len(jobDetail) < 1 {
return false, nil
}
// check the allocations for a running job
running := false
for _, v := range jobDetail {
if v["ClientStatus"].(string) == "running" {
running = true
}
}
// check a second time as any pending jobs should reset status
for _, v := range jobDetail {
if v["ClientStatus"].(string) == "pending" {
running = false
}
}
// job is not running
if !running {
return false, nil
}
return true, nil
}
// Endpoints returns a list of endpoints for a cluster
func (n *NomadImpl) Endpoints(job, group, task string) ([]map[string]string, error) {
jobs, err := n.getJobAllocations(job)
if err != nil {
return nil, err
}
endpoints := []map[string]string{}
// get the allocation details for each endpoint
for _, j := range jobs {
// only find running jobs
if j["ClientStatus"].(string) != "running" {
continue
}
r, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v1/allocation/%s", n.c.APIAddress(utils.Context(n.context)), j["ID"]), nil)
if err != nil {
return nil, xerrors.Errorf("Unable to create http request: %w", err)
}
resp, err := n.httpClient.Do(r)
if err != nil {
return nil, xerrors.Errorf("Unable to get allocation: %w", err)
}
if resp.Body == nil {
return nil, xerrors.Errorf("No body returned from Nomad API")
}
defer resp.Body.Close()
allocDetail := allocation{}
err = json.NewDecoder(resp.Body).Decode(&allocDetail)
if err != nil {
return nil, fmt.Errorf("Error getting endpoints from server: %s: err: %s", n.c.APIAddress(utils.Context(n.context)), err)
}
ports := []string{}
// find the ports used by the task
for _, tg := range allocDetail.Job.TaskGroups {
if tg.Name == group {
// non connect services will have their ports
// coded in the driver config block
for _, t := range tg.Tasks {
if t.Name == task {
ports = append(ports, t.Config.Ports...)
}
}
// connect services will have this coded
// in the groups network block
for _, n := range tg.Networks {
for _, dp := range n.DynamicPorts {
ports = append(ports, dp.Label)
}
for _, dp := range n.ReservedPorts {
ports = append(ports, dp.Label)
}
}
}
}
ep := map[string]string{}
epc := 0
for _, p := range ports {
// lookup the resources for the ports
for _, n := range allocDetail.Resources.Networks {
for _, dp := range n.DynamicPorts {
if dp.Label == p {
ep[p] = fmt.Sprintf("%s:%d", n.IP, dp.Value)
epc++
}
}
for _, dp := range n.ReservedPorts {
if dp.Label == p {
ep[p] = fmt.Sprintf("%s:%d", n.IP, dp.Value)
epc++
}
}
}
}
if epc > 0 {
endpoints = append(endpoints, ep)
}
}
return endpoints, nil
}
func (n *NomadImpl) getJobAllocations(job string) ([]map[string]interface{}, error) {
// get the allocations for the job
r, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/v1/job/%s/allocations", n.c.APIAddress(utils.Context(n.context)), job), nil)
if err != nil {
return nil, xerrors.Errorf("Unable to create http request: %w", err)
}
resp, err := n.httpClient.Do(r)
if err != nil {
return nil, xerrors.Errorf("Unable to query job: %w", err)
}
if resp.Body == nil {
return nil, xerrors.Errorf("No body returned from Nomad API")
}
defer resp.Body.Close()
jobDetail := make([]map[string]interface{}, 0)
err = json.NewDecoder(resp.Body).Decode(&jobDetail)
if err != nil {
return nil, fmt.Errorf("Unable to query jobs in Nomad server: %s: %s", n.c.APIAddress(utils.Context(n.context)), err)
}
return jobDetail, err
}
func (n *NomadImpl) getJobID(file string) (string, error) {
// parse the job
jsonJob, err := n.ParseJob(file)
if err != nil {
return "", err
}
// convert to a map to read the ID
jobMap := make(map[string]interface{})
err = json.Unmarshal(jsonJob, &jobMap)
if err != nil {
return "", err
}
return jobMap["ID"].(string), nil
}
type allocation struct {
ID string
Job job
Resources resource
}
type job struct {
Name string
TaskGroups []taskGroup
}
type taskGroup struct {
Name string
Tasks []task
Networks []allocNetwork
}
type task struct {
Name string
Config taskConfig
}
type taskConfig struct {
Ports []string
}
type resource struct {
Networks []allocNetwork
}
type allocNetwork struct {
IP string
DynamicPorts []port
ReservedPorts []port
}
type port struct {
Label string
Value int
}