-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
result.go
108 lines (94 loc) · 2.98 KB
/
result.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
package checks
import (
"net/http"
"time"
"github.com/hashicorp/nomad/nomad/structs"
"golang.org/x/exp/maps"
)
// GetCheckQuery extracts the needed info from c to actually execute the check.
func GetCheckQuery(c *structs.ServiceCheck) *Query {
var protocol = c.Protocol // ensure appropriate default
if c.Type == "http" && protocol == "" {
protocol = "http"
}
return &Query{
Mode: structs.GetCheckMode(c),
Type: c.Type,
Timeout: c.Timeout,
AddressMode: c.AddressMode,
PortLabel: c.PortLabel,
Protocol: protocol,
Path: c.Path,
Method: c.Method,
Headers: maps.Clone(c.Header),
Body: c.Body,
}
}
// A Query is derived from a ServiceCheck and contains the minimal
// amount of information needed to actually execute that check.
type Query struct {
Mode structs.CheckMode // readiness or healthiness
Type string // tcp or http
Timeout time.Duration // connection / request timeout
AddressMode string // host, driver, or alloc
PortLabel string // label or value
Protocol string // http checks only (http or https)
Path string // http checks only
Method string // http checks only
Headers http.Header // http checks only
Body string // http checks only
}
// A QueryContext contains allocation and service parameters necessary for
// address resolution.
type QueryContext struct {
ID structs.CheckID
CustomAddress string
ServicePortLabel string
Networks structs.Networks
NetworkStatus structs.NetworkStatus
Ports structs.AllocatedPorts
Group string
Task string
Service string
Check string
}
// Stub creates a temporary QueryResult for the check of ID in the Pending state
// so we can represent the status of not being checked yet.
func Stub(
id structs.CheckID, kind structs.CheckMode, now int64,
group, task, service, check string,
) *structs.CheckQueryResult {
return &structs.CheckQueryResult{
ID: id,
Mode: kind,
Status: structs.CheckPending,
Output: "nomad: waiting to run",
Timestamp: now,
Group: group,
Task: task,
Service: service,
Check: check,
}
}
// AllocationResults is a view of the check_id -> latest result for group and task
// checks in an allocation.
type AllocationResults map[structs.CheckID]*structs.CheckQueryResult
// diff returns the set of IDs in ids that are not in m.
func (m AllocationResults) diff(ids []structs.CheckID) []structs.CheckID {
var missing []structs.CheckID
for _, id := range ids {
if _, exists := m[id]; !exists {
missing = append(missing, id)
}
}
return missing
}
// ClientResults is a holistic view of alloc_id -> check_id -> latest result
// group and task checks across all allocations on a client.
type ClientResults map[string]AllocationResults
func (cr ClientResults) Insert(allocID string, result *structs.CheckQueryResult) {
if _, exists := cr[allocID]; !exists {
cr[allocID] = make(AllocationResults)
}
cr[allocID][result.ID] = result
}