github.com/hashicorp/nomad/api@v0.0.0-20240306165712-3193ac204f65/evaluations.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: MPL-2.0 3 4 package api 5 6 import ( 7 "sort" 8 "time" 9 ) 10 11 // Evaluations is used to query the evaluation endpoints. 12 type Evaluations struct { 13 client *Client 14 } 15 16 // Evaluations returns a new handle on the evaluations. 17 func (c *Client) Evaluations() *Evaluations { 18 return &Evaluations{client: c} 19 } 20 21 // List is used to dump all of the evaluations. 22 func (e *Evaluations) List(q *QueryOptions) ([]*Evaluation, *QueryMeta, error) { 23 var resp []*Evaluation 24 qm, err := e.client.query("/v1/evaluations", &resp, q) 25 if err != nil { 26 return nil, nil, err 27 } 28 sort.Sort(EvalIndexSort(resp)) 29 return resp, qm, nil 30 } 31 32 func (e *Evaluations) PrefixList(prefix string) ([]*Evaluation, *QueryMeta, error) { 33 return e.List(&QueryOptions{Prefix: prefix}) 34 } 35 36 // Count is used to get a count of evaluations. 37 func (e *Evaluations) Count(q *QueryOptions) (*EvalCountResponse, *QueryMeta, error) { 38 var resp *EvalCountResponse 39 qm, err := e.client.query("/v1/evaluations/count", &resp, q) 40 if err != nil { 41 return resp, nil, err 42 } 43 return resp, qm, nil 44 } 45 46 // Info is used to query a single evaluation by its ID. 47 func (e *Evaluations) Info(evalID string, q *QueryOptions) (*Evaluation, *QueryMeta, error) { 48 var resp Evaluation 49 qm, err := e.client.query("/v1/evaluation/"+evalID, &resp, q) 50 if err != nil { 51 return nil, nil, err 52 } 53 return &resp, qm, nil 54 } 55 56 // Delete is used to batch delete evaluations using their IDs. 57 func (e *Evaluations) Delete(evalIDs []string, w *WriteOptions) (*WriteMeta, error) { 58 req := EvalDeleteRequest{ 59 EvalIDs: evalIDs, 60 } 61 wm, err := e.client.delete("/v1/evaluations", &req, nil, w) 62 if err != nil { 63 return nil, err 64 } 65 return wm, nil 66 } 67 68 // DeleteOpts is used to batch delete evaluations using a filter. 69 func (e *Evaluations) DeleteOpts(req *EvalDeleteRequest, w *WriteOptions) (*EvalDeleteResponse, *WriteMeta, error) { 70 resp := &EvalDeleteResponse{} 71 wm, err := e.client.delete("/v1/evaluations", &req, resp, w) 72 if err != nil { 73 return nil, nil, err 74 } 75 return resp, wm, nil 76 } 77 78 // Allocations is used to retrieve a set of allocations given 79 // an evaluation ID. 80 func (e *Evaluations) Allocations(evalID string, q *QueryOptions) ([]*AllocationListStub, *QueryMeta, error) { 81 var resp []*AllocationListStub 82 qm, err := e.client.query("/v1/evaluation/"+evalID+"/allocations", &resp, q) 83 if err != nil { 84 return nil, nil, err 85 } 86 sort.Sort(AllocIndexSort(resp)) 87 return resp, qm, nil 88 } 89 90 const ( 91 EvalStatusBlocked = "blocked" 92 EvalStatusPending = "pending" 93 EvalStatusComplete = "complete" 94 EvalStatusFailed = "failed" 95 EvalStatusCancelled = "canceled" 96 ) 97 98 // Evaluation is used to serialize an evaluation. 99 type Evaluation struct { 100 ID string 101 Priority int 102 Type string 103 TriggeredBy string 104 Namespace string 105 JobID string 106 JobModifyIndex uint64 107 NodeID string 108 NodeModifyIndex uint64 109 DeploymentID string 110 Status string 111 StatusDescription string 112 Wait time.Duration 113 WaitUntil time.Time 114 NextEval string 115 PreviousEval string 116 BlockedEval string 117 RelatedEvals []*EvaluationStub 118 FailedTGAllocs map[string]*AllocationMetric 119 ClassEligibility map[string]bool 120 EscapedComputedClass bool 121 QuotaLimitReached string 122 AnnotatePlan bool 123 QueuedAllocations map[string]int 124 SnapshotIndex uint64 125 CreateIndex uint64 126 ModifyIndex uint64 127 CreateTime int64 128 ModifyTime int64 129 } 130 131 // EvaluationStub is used to serialize parts of an evaluation returned in the 132 // RelatedEvals field of an Evaluation. 133 type EvaluationStub struct { 134 ID string 135 Priority int 136 Type string 137 TriggeredBy string 138 Namespace string 139 JobID string 140 NodeID string 141 DeploymentID string 142 Status string 143 StatusDescription string 144 WaitUntil time.Time 145 NextEval string 146 PreviousEval string 147 BlockedEval string 148 CreateIndex uint64 149 ModifyIndex uint64 150 CreateTime int64 151 ModifyTime int64 152 } 153 154 type EvalDeleteRequest struct { 155 EvalIDs []string 156 Filter string 157 WriteRequest 158 } 159 160 type EvalDeleteResponse struct { 161 Count int 162 } 163 164 type EvalCountResponse struct { 165 Count int 166 QueryMeta 167 } 168 169 // EvalIndexSort is a wrapper to sort evaluations by CreateIndex. 170 // We reverse the test so that we get the highest index first. 171 type EvalIndexSort []*Evaluation 172 173 func (e EvalIndexSort) Len() int { 174 return len(e) 175 } 176 177 func (e EvalIndexSort) Less(i, j int) bool { 178 return e[i].CreateIndex > e[j].CreateIndex 179 } 180 181 func (e EvalIndexSort) Swap(i, j int) { 182 e[i], e[j] = e[j], e[i] 183 }