-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdiscovery.go
176 lines (163 loc) · 4.28 KB
/
discovery.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
package registry
import (
"fmt"
"net"
"net/url"
"sort"
"strconv"
"strings"
"github.com/hashicorp/consul/api"
)
type discovery struct {
agent *api.Agent
health *api.Health
catalog *api.Catalog
datacenter string
}
func (d *discovery) Register(options ServiceOptions) error {
var (
host = options.Address
port int
)
if strings.HasPrefix(host, "http") {
url, err := url.Parse(host)
if err != nil {
return err
}
host = url.Host
}
if strings.Contains(host, ":") {
h, p, err := net.SplitHostPort(host)
if err != nil {
return err
}
v, err := strconv.ParseUint(p, 10, 32)
if err != nil {
return err
}
host = h
port = int(v)
}
if options.Check.DeregisterAfter == "" {
options.Check.DeregisterAfter = "10m"
}
fmt.Println("Register", options.Name, options.ID)
agentService := api.AgentServiceRegistration{
ID: options.ID,
Name: options.Name,
Address: host,
Port: port,
Tags: append(options.Tags, "DC="+d.datacenter),
EnableTagOverride: true,
Check: nil,
}
if options.Check.HTTP != "" || options.Check.TCP != "" {
agentService.Check = &api.AgentServiceCheck{
CheckID: options.ID,
Name: fmt.Sprintf("%s health status", options.Name),
Interval: options.Check.Interval,
Timeout: options.Check.Timeout,
HTTP: options.Check.HTTP,
TCP: options.Check.TCP,
TTL: options.Check.TTL,
DeregisterCriticalServiceAfter: options.Check.DeregisterAfter,
}
}
return d.agent.ServiceRegister(&agentService)
}
func (d *discovery) Deregister(ident string) error {
return d.agent.ServiceDeregister(ident)
}
type sortServiceByID []Service
func (a sortServiceByID) Len() int { return len(a) }
func (a sortServiceByID) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a sortServiceByID) Less(i, j int) bool { return a[i].ID < a[j].ID }
// Lookup services by filter
//
// If needed to lookup all services around all DCs,
// set DC filter to "all". It takes all DCs from discovery
// and polls services every of them
func (d *discovery) Lookup(filter *Filter) ([]Service, error) {
if filter == nil {
filter = &Filter{}
}
if filter.Datacenter != "all" {
return d.lookup(filter)
}
dcl, err := d.catalog.Datacenters()
if err != nil {
return nil, fmt.Errorf("datacenters list: %s", err)
}
var services []Service
for _, dc := range dcl {
filter.Datacenter = dc
s, err := d.lookup(filter)
if err != nil {
return nil, fmt.Errorf("datacenter %s lookup: %s", dc, err)
}
services = append(services, s...)
}
return services, nil
}
func (d *discovery) lookup(filter *Filter) ([]Service, error) {
var (
result []Service
q = &api.QueryOptions{Datacenter: filter.Datacenter}
)
list, _, err := d.catalog.Services(q)
if err != nil {
return nil, err
}
var names []string
for name := range list {
names = append(names, name)
items, _, err := d.catalog.Service(name, "", q)
if err != nil {
return nil, err
}
for _, item := range items {
var (
srv = Service{
ID: item.ServiceID,
Name: item.ServiceName,
Datacenter: item.Datacenter,
Address: item.ServiceAddress,
Port: item.ServicePort,
Tags: item.ServiceTags,
Status: SERVICE_STATUS_UNDEFINED,
}
)
result = append(result, srv)
}
}
sort.Sort(sortServiceByID(result))
status := func(status string) int8 {
switch status {
case "passing":
return SERVICE_STATUS_PASSING
case "warning":
return SERVICE_STATUS_WARNING
case "critical":
return SERVICE_STATUS_CRITICAL
}
return SERVICE_STATUS_UNDEFINED
}
for _, name := range names {
healthChecks, _, err := d.health.Checks(name, q)
if err != nil {
return nil, err
}
for _, check := range healthChecks {
if i := sort.Search(len(result), func(i int) bool { return result[i].ID >= check.ServiceID }); i < len(result) && result[i].ID == check.ServiceID {
result[i].Status = status(check.Status)
}
}
}
services := make([]Service, 0, len(result))
for _, srv := range result {
if srv.test(filter) {
services = append(services, srv)
}
}
return services, nil
}