-
Notifications
You must be signed in to change notification settings - Fork 127
/
node.go
151 lines (126 loc) · 4.38 KB
/
node.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package internaltypes
import (
"golang.org/x/exp/maps"
v1 "k8s.io/api/core/v1"
armadamaps "github.com/armadaproject/armada/internal/common/maps"
"github.com/armadaproject/armada/internal/scheduler/kubernetesobjects/label"
koTaint "github.com/armadaproject/armada/internal/scheduler/kubernetesobjects/taint"
"github.com/armadaproject/armada/internal/scheduler/schedulerobjects"
)
type Node struct {
// Unique id and index of this node.
// TODO(albin): Having both id and index is redundant.
// Currently, the id is "cluster name" + "node name" and index an integer assigned on node creation.
id string
index uint64
// Executor this node belongs to and node name, which must be unique per executor.
executor string
name string
nodeTypeId uint64
// We need to store taints and labels separately from the node type: the latter only includes
// indexed taints and labels, but we need all of them when checking pod requirements.
taints []v1.Taint
labels map[string]string
TotalResources schedulerobjects.ResourceList
// This field is set when inserting the Node into a NodeDb.
Keys [][]byte
AllocatableByPriority schedulerobjects.AllocatableByPriorityAndResourceType
AllocatedByQueue map[string]schedulerobjects.ResourceList
AllocatedByJobId map[string]schedulerobjects.ResourceList
EvictedJobRunIds map[string]bool
}
func CreateNode(
id string,
nodeTypeId uint64,
index uint64,
executor string,
name string,
taints []v1.Taint,
labels map[string]string,
totalResources schedulerobjects.ResourceList,
allocatableByPriority schedulerobjects.AllocatableByPriorityAndResourceType,
allocatedByQueue map[string]schedulerobjects.ResourceList,
allocatedByJobId map[string]schedulerobjects.ResourceList,
evictedJobRunIds map[string]bool,
keys [][]byte,
) *Node {
return &Node{
id: id,
nodeTypeId: nodeTypeId,
index: index,
executor: executor,
name: name,
taints: koTaint.DeepCopyTaints(taints),
labels: deepCopyLabels(labels),
TotalResources: totalResources,
AllocatableByPriority: allocatableByPriority,
AllocatedByQueue: allocatedByQueue,
AllocatedByJobId: allocatedByJobId,
EvictedJobRunIds: evictedJobRunIds,
Keys: keys,
}
}
func (node *Node) GetId() string {
return node.id
}
func (node *Node) GetName() string {
return node.name
}
func (node *Node) GetIndex() uint64 {
return node.index
}
func (node *Node) GetExecutor() string {
return node.executor
}
func (node *Node) GetNodeTypeId() uint64 {
return node.nodeTypeId
}
func (node *Node) GetLabels() map[string]string {
return deepCopyLabels(node.labels)
}
func (node *Node) GetLabelValue(key string) (string, bool) {
val, ok := node.labels[key]
return val, ok
}
func (node *Node) GetTaints() []v1.Taint {
return koTaint.DeepCopyTaints(node.taints)
}
func (node *Node) FindMatchingUntoleratedTaint(tolerations ...[]v1.Toleration) (v1.Taint, bool) {
return koTaint.FindMatchingUntoleratedTaint(node.taints, tolerations...)
}
func (node *Node) MatchNodeSelectorTerms(nodeSelector *v1.NodeSelector) (bool, error) {
return label.MatchNodeSelectorTerms(node.labels, nodeSelector)
}
func (node *Node) GetTolerationsForTaints() []v1.Toleration {
var tolerations []v1.Toleration
for _, taint := range node.taints {
tolerations = append(tolerations, v1.Toleration{Key: taint.Key, Value: taint.Value, Effect: taint.Effect})
}
return tolerations
}
// UnsafeCopy returns a pointer to a new value of type Node; it is unsafe because it only makes
// shallow copies of fields that are not mutated by methods of NodeDb.
func (node *Node) UnsafeCopy() *Node {
return &Node{
id: node.id,
index: node.index,
executor: node.executor,
name: node.name,
nodeTypeId: node.nodeTypeId,
taints: node.taints,
labels: node.labels,
TotalResources: node.TotalResources,
Keys: nil,
AllocatableByPriority: armadamaps.DeepCopy(node.AllocatableByPriority),
AllocatedByQueue: armadamaps.DeepCopy(node.AllocatedByQueue),
AllocatedByJobId: armadamaps.DeepCopy(node.AllocatedByJobId),
EvictedJobRunIds: maps.Clone(node.EvictedJobRunIds),
}
}
func deepCopyLabels(labels map[string]string) map[string]string {
result := make(map[string]string, len(labels))
for k, v := range labels {
result[k] = v
}
return result
}