-
Notifications
You must be signed in to change notification settings - Fork 2k
/
checks.go
92 lines (80 loc) · 2.73 KB
/
checks.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
package structs
import (
"crypto/md5"
"fmt"
)
// The CheckMode of a Nomad check is either Healthiness or Readiness.
type CheckMode string
const (
// A Healthiness check is useful in the context of ensuring a service
// is capable of performing its duties. This is an indicator that a check's
// on_update configuration is set to "check_result", implying that Deployments
// will not move forward while the check is failing.
Healthiness CheckMode = "healthiness"
// A Readiness check is useful in the context of ensuring a service
// should be performing its duties (regardless of healthiness). This is an
// indicator that the check's on_update configuration is set to "ignore",
// implying that Deployments will move forward regardless if the check is
// failing.
Readiness CheckMode = "readiness"
)
// GetCheckMode determines whether the check is readiness or healthiness.
func GetCheckMode(c *ServiceCheck) CheckMode {
if c != nil && c.OnUpdate == OnUpdateIgnore {
return Readiness
}
return Healthiness
}
// An CheckID is unique to a check.
type CheckID string
// A CheckQueryResult represents the outcome of a single execution of a Nomad service
// check. It records the result, the output, and when the execution took place.
// Advanced check math (e.g. success_before_passing) are left to the calling
// context.
type CheckQueryResult struct {
ID CheckID
Mode CheckMode
Status CheckStatus
StatusCode int `json:",omitempty"`
Output string
Timestamp int64
// check coordinates
Group string
Task string `json:",omitempty"`
Service string
Check string
}
func (r *CheckQueryResult) String() string {
return fmt.Sprintf("(%s %s %s %v)", r.ID, r.Mode, r.Status, r.Timestamp)
}
// A CheckStatus is the result of executing a check. The status of a query is
// ternary - success, failure, or pending (not yet executed). Deployments treat
// pending and failure as the same - a deployment does not continue until a check
// is passing (unless on_update=ignore).
type CheckStatus string
const (
CheckSuccess CheckStatus = "success"
CheckFailure CheckStatus = "failure"
CheckPending CheckStatus = "pending"
)
// NomadCheckID returns an ID unique to the nomad service check.
//
// Checks of group-level services have no task.
func NomadCheckID(allocID, group string, c *ServiceCheck) CheckID {
sum := md5.New()
hashString(sum, allocID)
hashString(sum, group)
hashString(sum, c.TaskName)
hashString(sum, c.Name)
hashString(sum, c.Type)
hashString(sum, c.PortLabel)
hashString(sum, c.OnUpdate)
hashString(sum, c.AddressMode)
hashDuration(sum, c.Interval)
hashDuration(sum, c.Timeout)
hashString(sum, c.Protocol)
hashString(sum, c.Path)
hashString(sum, c.Method)
h := sum.Sum(nil)
return CheckID(fmt.Sprintf("%x", h))
}