This repository has been archived by the owner on Mar 14, 2023. It is now read-only.
forked from hashicorp/nomad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
98 lines (78 loc) · 2.24 KB
/
context.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
package scheduler
import (
"log"
"github.com/hashicorp/nomad/nomad/structs"
)
// Context is used to track contextual information used for placement
type Context interface {
// State is used to inspect the current global state
State() State
// Plan returns the current plan
Plan() *structs.Plan
// Logger provides a way to log
Logger() *log.Logger
// Metrics returns the current metrics
Metrics() *structs.AllocMetric
// Reset is invoked after making a placement
Reset()
// ProposedAllocs returns the proposed allocations for a node
// which is the existing allocations, removing evictions, and
// adding any planned placements.
ProposedAllocs(nodeID string) ([]*structs.Allocation, error)
}
// EvalContext is a Context used during an Evaluation
type EvalContext struct {
state State
plan *structs.Plan
logger *log.Logger
metrics *structs.AllocMetric
}
// NewEvalContext constructs a new EvalContext
func NewEvalContext(s State, p *structs.Plan, log *log.Logger) *EvalContext {
ctx := &EvalContext{
state: s,
plan: p,
logger: log,
metrics: new(structs.AllocMetric),
}
return ctx
}
func (e *EvalContext) State() State {
return e.state
}
func (e *EvalContext) Plan() *structs.Plan {
return e.plan
}
func (e *EvalContext) Logger() *log.Logger {
return e.logger
}
func (e *EvalContext) Metrics() *structs.AllocMetric {
return e.metrics
}
func (e *EvalContext) SetState(s State) {
e.state = s
}
func (e *EvalContext) Reset() {
e.metrics = new(structs.AllocMetric)
}
func (e *EvalContext) ProposedAllocs(nodeID string) ([]*structs.Allocation, error) {
// Get the existing allocations
existingAlloc, err := e.state.AllocsByNode(nodeID)
if err != nil {
return nil, err
}
// Filter on alloc state
existingAlloc = structs.FilterTerminalAllocs(existingAlloc)
// Determine the proposed allocation by first removing allocations
// that are planned evictions and adding the new allocations.
proposed := existingAlloc
if update := e.plan.NodeUpdate[nodeID]; len(update) > 0 {
proposed = structs.RemoveAllocs(existingAlloc, update)
}
proposed = append(proposed, e.plan.NodeAllocation[nodeID]...)
// Ensure the return is not nil
if proposed == nil {
proposed = make([]*structs.Allocation, 0)
}
return proposed, nil
}