-
Notifications
You must be signed in to change notification settings - Fork 785
/
health_service.go
274 lines (237 loc) · 6.84 KB
/
health_service.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
package dependency
import (
"encoding/gob"
"fmt"
"log"
"net/url"
"regexp"
"sort"
"strings"
"github.com/hashicorp/consul/api"
"github.com/pkg/errors"
)
const (
HealthAny = "any"
HealthPassing = "passing"
HealthWarning = "warning"
HealthCritical = "critical"
HealthMaint = "maintenance"
NodeMaint = "_node_maintenance"
ServiceMaint = "_service_maintenance:"
)
var (
// Ensure implements
_ Dependency = (*HealthServiceQuery)(nil)
// HealthServiceQueryRe is the regular expression to use.
HealthServiceQueryRe = regexp.MustCompile(`\A` + tagRe + serviceNameRe + dcRe + nearRe + filterRe + `\z`)
)
func init() {
gob.Register([]*HealthService{})
}
// HealthService is a service entry in Consul.
type HealthService struct {
Node string
NodeID string
NodeAddress string
NodeTaggedAddresses map[string]string
NodeMeta map[string]string
ServiceMeta map[string]string
Address string
ServiceTaggedAddresses map[string]api.ServiceAddress
ID string
Name string
Tags ServiceTags
Checks api.HealthChecks
Status string
Port int
Weights api.AgentWeights
}
// HealthServiceQuery is the representation of all a service query in Consul.
type HealthServiceQuery struct {
stopCh chan struct{}
dc string
filters []string
name string
near string
tag string
connect bool
}
// NewHealthServiceQuery processes the strings to build a service dependency.
func NewHealthServiceQuery(s string) (*HealthServiceQuery, error) {
return healthServiceQuery(s, false)
}
// NewHealthConnect Query processes the strings to build a connect dependency.
func NewHealthConnectQuery(s string) (*HealthServiceQuery, error) {
return healthServiceQuery(s, true)
}
func healthServiceQuery(s string, connect bool) (*HealthServiceQuery, error) {
if !HealthServiceQueryRe.MatchString(s) {
return nil, fmt.Errorf("health.service: invalid format: %q", s)
}
m := regexpMatch(HealthServiceQueryRe, s)
var filters []string
if filter := m["filter"]; filter != "" {
split := strings.Split(filter, ",")
for _, f := range split {
f = strings.TrimSpace(f)
switch f {
case HealthAny,
HealthPassing,
HealthWarning,
HealthCritical,
HealthMaint:
filters = append(filters, f)
case "":
default:
return nil, fmt.Errorf(
"health.service: invalid filter: %q in %q", f, s)
}
}
sort.Strings(filters)
} else {
filters = []string{HealthPassing}
}
return &HealthServiceQuery{
stopCh: make(chan struct{}, 1),
dc: m["dc"],
filters: filters,
name: m["name"],
near: m["near"],
tag: m["tag"],
connect: connect,
}, nil
}
// Fetch queries the Consul API defined by the given client and returns a slice
// of HealthService objects.
func (d *HealthServiceQuery) Fetch(clients *ClientSet, opts *QueryOptions) (interface{}, *ResponseMetadata, error) {
select {
case <-d.stopCh:
return nil, nil, ErrStopped
default:
}
opts = opts.Merge(&QueryOptions{
Datacenter: d.dc,
Near: d.near,
})
u := &url.URL{
Path: "/v1/health/service/" + d.name,
RawQuery: opts.String(),
}
if d.tag != "" {
q := u.Query()
q.Set("tag", d.tag)
u.RawQuery = q.Encode()
}
log.Printf("[TRACE] %s: GET %s", d, u)
// Check if a user-supplied filter was given. If so, we may be querying for
// more than healthy services, so we need to implement client-side
// filtering.
passingOnly := len(d.filters) == 1 && d.filters[0] == HealthPassing
nodes := clients.Consul().Health().Service
if d.connect {
nodes = clients.Consul().Health().Connect
}
entries, qm, err := nodes(d.name, d.tag, passingOnly, opts.ToConsulOpts())
if err != nil {
return nil, nil, errors.Wrap(err, d.String())
}
log.Printf("[TRACE] %s: returned %d results", d, len(entries))
list := make([]*HealthService, 0, len(entries))
for _, entry := range entries {
// Get the status of this service from its checks.
status := entry.Checks.AggregatedStatus()
// If we are not checking only healthy services, filter out services
// that do not match the given filter.
if !acceptStatus(d.filters, status) {
continue
}
// Get the address of the service, falling back to the address of the
// node.
address := entry.Service.Address
if address == "" {
address = entry.Node.Address
}
list = append(list, &HealthService{
Node: entry.Node.Node,
NodeID: entry.Node.ID,
NodeAddress: entry.Node.Address,
NodeTaggedAddresses: entry.Node.TaggedAddresses,
NodeMeta: entry.Node.Meta,
ServiceMeta: entry.Service.Meta,
Address: address,
ServiceTaggedAddresses: entry.Service.TaggedAddresses,
ID: entry.Service.ID,
Name: entry.Service.Service,
Tags: ServiceTags(
deepCopyAndSortTags(entry.Service.Tags)),
Status: status,
Checks: entry.Checks,
Port: entry.Service.Port,
Weights: entry.Service.Weights,
})
}
log.Printf("[TRACE] %s: returned %d results after filtering", d, len(list))
// Sort unless the user explicitly asked for nearness
if d.near == "" {
sort.Stable(ByNodeThenID(list))
}
rm := &ResponseMetadata{
LastIndex: qm.LastIndex,
LastContact: qm.LastContact,
}
return list, rm, nil
}
// CanShare returns a boolean if this dependency is shareable.
func (d *HealthServiceQuery) CanShare() bool {
return true
}
// Stop halts the dependency's fetch function.
func (d *HealthServiceQuery) Stop() {
close(d.stopCh)
}
// String returns the human-friendly version of this dependency.
func (d *HealthServiceQuery) String() string {
name := d.name
if d.tag != "" {
name = d.tag + "." + name
}
if d.dc != "" {
name = name + "@" + d.dc
}
if d.near != "" {
name = name + "~" + d.near
}
if len(d.filters) > 0 {
name = name + "|" + strings.Join(d.filters, ",")
}
if d.connect {
return fmt.Sprintf("health.connect(%s)", name)
}
return fmt.Sprintf("health.service(%s)", name)
}
// Type returns the type of this dependency.
func (d *HealthServiceQuery) Type() Type {
return TypeConsul
}
// acceptStatus allows us to check if a slice of health checks pass this filter.
func acceptStatus(list []string, s string) bool {
for _, status := range list {
if status == s || status == HealthAny {
return true
}
}
return false
}
// ByNodeThenID is a sortable slice of Service
type ByNodeThenID []*HealthService
// Len, Swap, and Less are used to implement the sort.Sort interface.
func (s ByNodeThenID) Len() int { return len(s) }
func (s ByNodeThenID) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s ByNodeThenID) Less(i, j int) bool {
if s[i].Node < s[j].Node {
return true
} else if s[i].Node == s[j].Node {
return s[i].ID < s[j].ID
}
return false
}