-
Notifications
You must be signed in to change notification settings - Fork 618
/
Copy pathpassing.go
75 lines (65 loc) · 1.93 KB
/
passing.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
package consul
import (
"log"
"strings"
"github.com/hashicorp/consul/api"
)
// passingServices takes a list of Consul Health Checks and only returns ones where the overall health of
// the Service Instance is passing. This includes the health of the Node that the Services Instance runs on.
func passingServices(checks []*api.HealthCheck, status []string, strict bool) []*api.HealthCheck {
var p []*api.HealthCheck
CHECKS:
for _, svc := range checks {
if !isServiceCheck(svc) {
continue
}
var total, passing int
for _, c := range checks {
if svc.Node == c.Node {
if svc.ServiceID == c.ServiceID {
total++
if hasStatus(c, status) {
passing++
}
}
if c.CheckID == "serfHealth" && c.Status == "critical" {
log.Printf("[DEBUG] consul: Skipping service %q since agent on node %q is down: %s", c.ServiceID, c.Node, c.Output)
continue CHECKS
}
if c.CheckID == "_node_maintenance" {
log.Printf("[DEBUG] consul: Skipping service %q since node %q is in maintenance mode: %s", c.ServiceID, c.Node, c.Output)
continue CHECKS
}
if c.CheckID == "_service_maintenance:"+svc.ServiceID && c.Status == "critical" {
log.Printf("[DEBUG] consul: Skipping service %q since it is in maintenance mode: %s", svc.ServiceID, c.Output)
continue CHECKS
}
}
}
if passing == 0 {
continue
}
if strict && total != passing {
continue
}
p = append(p, svc)
}
return p
}
// isServiceCheck returns true if the health check is a valid service check.
func isServiceCheck(c *api.HealthCheck) bool {
return c.ServiceID != "" &&
c.CheckID != "serfHealth" &&
c.CheckID != "_node_maintenance" &&
!strings.HasPrefix(c.CheckID, "_service_maintenance:")
}
// hasStatus returns true if the health check status is one of the given
// values.
func hasStatus(c *api.HealthCheck, status []string) bool {
for _, s := range status {
if c.Status == s {
return true
}
}
return false
}