github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/nomad/structs/checks.go (about) 1 package structs 2 3 import ( 4 "crypto/md5" 5 "fmt" 6 ) 7 8 // The CheckMode of a Nomad check is either Healthiness or Readiness. 9 type CheckMode string 10 11 const ( 12 // A Healthiness check is useful in the context of ensuring a service 13 // is capable of performing its duties. This is an indicator that a check's 14 // on_update configuration is set to "check_result", implying that Deployments 15 // will not move forward while the check is failing. 16 Healthiness CheckMode = "healthiness" 17 18 // A Readiness check is useful in the context of ensuring a service 19 // should be performing its duties (regardless of healthiness). This is an 20 // indicator that the check's on_update configuration is set to "ignore", 21 // implying that Deployments will move forward regardless if the check is 22 // failing. 23 Readiness CheckMode = "readiness" 24 ) 25 26 // GetCheckMode determines whether the check is readiness or healthiness. 27 func GetCheckMode(c *ServiceCheck) CheckMode { 28 if c != nil && c.OnUpdate == OnUpdateIgnore { 29 return Readiness 30 } 31 return Healthiness 32 } 33 34 // An CheckID is unique to a check. 35 type CheckID string 36 37 // A CheckQueryResult represents the outcome of a single execution of a Nomad service 38 // check. It records the result, the output, and when the execution took place. 39 // Advanced check math (e.g. success_before_passing) are left to the calling 40 // context. 41 type CheckQueryResult struct { 42 ID CheckID 43 Mode CheckMode 44 Status CheckStatus 45 StatusCode int `json:",omitempty"` 46 Output string 47 Timestamp int64 48 49 // check coordinates 50 Group string 51 Task string `json:",omitempty"` 52 Service string 53 Check string 54 } 55 56 func (r *CheckQueryResult) String() string { 57 return fmt.Sprintf("(%s %s %s %v)", r.ID, r.Mode, r.Status, r.Timestamp) 58 } 59 60 // A CheckStatus is the result of executing a check. The status of a query is 61 // ternary - success, failure, or pending (not yet executed). Deployments treat 62 // pending and failure as the same - a deployment does not continue until a check 63 // is passing (unless on_update=ignore). 64 type CheckStatus string 65 66 const ( 67 CheckSuccess CheckStatus = "success" 68 CheckFailure CheckStatus = "failure" 69 CheckPending CheckStatus = "pending" 70 ) 71 72 // NomadCheckID returns an ID unique to the nomad service check. 73 // 74 // Checks of group-level services have no task. 75 func NomadCheckID(allocID, group string, c *ServiceCheck) CheckID { 76 sum := md5.New() 77 hashString(sum, allocID) 78 hashString(sum, group) 79 hashString(sum, c.TaskName) 80 hashString(sum, c.Name) 81 hashString(sum, c.Type) 82 hashString(sum, c.PortLabel) 83 hashString(sum, c.OnUpdate) 84 hashString(sum, c.AddressMode) 85 hashDuration(sum, c.Interval) 86 hashDuration(sum, c.Timeout) 87 hashString(sum, c.Protocol) 88 hashString(sum, c.Path) 89 hashString(sum, c.Method) 90 h := sum.Sum(nil) 91 return CheckID(fmt.Sprintf("%x", h)) 92 }