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