-
Notifications
You must be signed in to change notification settings - Fork 0
/
nomad.go
117 lines (91 loc) · 2.25 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
package main
import (
"log"
"time"
nomad "github.com/hashicorp/nomad/api"
)
type TaskInfo struct {
jobid string
ip string
}
type NodeInfo map[string]string
type NomadSpec struct {
uri string
token string
}
func periodicallySyncNomad(dns_spec *CloudDNSSpec, nomadSpec *NomadSpec, interval int, pruneMissing *bool) {
syncNomad(dns_spec, nomadSpec, pruneMissing)
if interval >= 0 {
for {
log.Printf("Waiting %d seconds.", interval)
time.Sleep(time.Duration(interval) * time.Second)
syncNomad(dns_spec, nomadSpec, pruneMissing)
}
}
}
func syncNomad(dnsSpec *CloudDNSSpec, nomadSpec *NomadSpec, pruneMissing *bool) {
//c := make(<-chan *dns.Change)
jobLocs := getNomadTaskLocations(nomadSpec)
log.Printf("Found %d nomad jobs", len(jobLocs))
change, err := buildNomadDnsChange(dnsSpec, jobLocs, *pruneMissing)
if err != nil {
log.Fatal("Building DNS change from nomad info:", err)
}
err = processCloudDnsChange(dnsSpec, change)
if err != nil {
log.Fatal("Updating Cloud DNS from nomad:", err)
}
dnsTotalRecordCount.Set(float64(len(jobLocs)))
}
func getNomadNodesList(nomadSpec *NomadSpec) NodeInfo {
conf := &nomad.Config{
Address: nomadSpec.uri,
}
c, err := nomad.NewClient(conf)
if err != nil {
log.Fatal("Talking to Nomad: ", err)
}
nodes, _, err := c.Nodes().List(nil)
if err != nil {
log.Fatal("Getting Allocs from nomad: ", err)
}
ret := NodeInfo{}
for _, n := range nodes {
ret[n.Name] = n.Address
}
return ret
}
func getNomadAllocsList(nomadSpec *NomadSpec) []*nomad.AllocationListStub {
conf := &nomad.Config{
Address: nomadSpec.uri,
}
c, err := nomad.NewClient(conf)
if err != nil {
log.Fatal("Talking to Nomad: ", err)
}
allocs, _, err := c.Allocations().List(nil)
if err != nil {
log.Fatal("Getting Allocs from nomad: ", err)
}
return allocs
}
func getNomadTaskLocations(nomadSpec *NomadSpec) []TaskInfo {
ret := []TaskInfo{}
allocs := getNomadAllocsList(nomadSpec)
nodes := getNomadNodesList(nomadSpec)
for _, a := range allocs {
if a.ClientStatus != "running" {
continue
}
ip, ok := nodes[a.NodeName]
if !ok {
log.Printf("Unknown node %s for running alloc %s", a.NodeName, a.ID)
continue
}
ret = append(ret, TaskInfo{
jobid: a.JobID,
ip: ip,
})
}
return ret
}