-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
resources.go
329 lines (281 loc) · 8.65 KB
/
resources.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package api
import (
"slices"
"strconv"
)
// Resources encapsulates the required resources of
// a given task or task group.
type Resources struct {
CPU *int `hcl:"cpu,optional"`
Cores *int `hcl:"cores,optional"`
MemoryMB *int `mapstructure:"memory" hcl:"memory,optional"`
MemoryMaxMB *int `mapstructure:"memory_max" hcl:"memory_max,optional"`
DiskMB *int `mapstructure:"disk" hcl:"disk,optional"`
Networks []*NetworkResource `hcl:"network,block"`
Devices []*RequestedDevice `hcl:"device,block"`
NUMA *NUMAResource `hcl:"numa,block"`
SecretsMB *int `mapstructure:"secrets" hcl:"secrets,optional"`
// COMPAT(0.10)
// XXX Deprecated. Please do not use. The field will be removed in Nomad
// 0.10 and is only being kept to allow any references to be removed before
// then.
IOPS *int `hcl:"iops,optional"`
}
// Canonicalize will supply missing values in the cases
// where they are not provided.
func (r *Resources) Canonicalize() {
defaultResources := DefaultResources()
if r.Cores == nil {
r.Cores = defaultResources.Cores
// only set cpu to the default value if it and cores is not defined
if r.CPU == nil {
r.CPU = defaultResources.CPU
}
}
// CPU will be set to the default if cores is nil above.
// If cpu is nil here then cores has been set and cpu should be 0
if r.CPU == nil {
r.CPU = pointerOf(0)
}
if r.MemoryMB == nil {
r.MemoryMB = defaultResources.MemoryMB
}
for _, d := range r.Devices {
d.Canonicalize()
}
r.NUMA.Canonicalize()
}
// DefaultResources is a small resources object that contains the
// default resources requests that we will provide to an object.
// --- THIS FUNCTION IS REPLICATED IN nomad/structs/structs.go
// and should be kept in sync.
func DefaultResources() *Resources {
return &Resources{
CPU: pointerOf(100),
Cores: pointerOf(0),
MemoryMB: pointerOf(300),
}
}
// MinResources is a small resources object that contains the
// absolute minimum resources that we will provide to an object.
// This should not be confused with the defaults which are
// provided in DefaultResources() --- THIS LOGIC IS REPLICATED
// IN nomad/structs/structs.go and should be kept in sync.
func MinResources() *Resources {
return &Resources{
CPU: pointerOf(1),
Cores: pointerOf(0),
MemoryMB: pointerOf(10),
}
}
// Merge merges this resource with another resource.
func (r *Resources) Merge(other *Resources) {
if other == nil {
return
}
if other.CPU != nil {
r.CPU = other.CPU
}
if other.MemoryMB != nil {
r.MemoryMB = other.MemoryMB
}
if other.DiskMB != nil {
r.DiskMB = other.DiskMB
}
if len(other.Networks) != 0 {
r.Networks = other.Networks
}
if len(other.Devices) != 0 {
r.Devices = other.Devices
}
if other.NUMA != nil {
r.NUMA = other.NUMA.Copy()
}
if other.SecretsMB != nil {
r.SecretsMB = other.SecretsMB
}
}
// NUMAResource contains the NUMA affinity request for scheduling purposes.
//
// Applies only to Nomad Enterprise.
type NUMAResource struct {
// Affinity must be one of "none", "prefer", "require".
Affinity string `hcl:"affinity,optional"`
// Devices is the subset of devices requested by the task that must share
// the same numa node, along with the tasks reserved cpu cores.
Devices []string `hcl:"devices,optional"`
}
func (n *NUMAResource) Copy() *NUMAResource {
if n == nil {
return nil
}
return &NUMAResource{
Affinity: n.Affinity,
Devices: slices.Clone(n.Devices),
}
}
func (n *NUMAResource) Canonicalize() {
if n == nil {
return
}
if n.Affinity == "" {
n.Affinity = "none"
}
if len(n.Devices) == 0 {
n.Devices = nil
}
}
type Port struct {
Label string `hcl:",label"`
Value int `hcl:"static,optional"`
To int `hcl:"to,optional"`
HostNetwork string `hcl:"host_network,optional"`
}
type DNSConfig struct {
Servers []string `mapstructure:"servers" hcl:"servers,optional"`
Searches []string `mapstructure:"searches" hcl:"searches,optional"`
Options []string `mapstructure:"options" hcl:"options,optional"`
}
type CNIConfig struct {
Args map[string]string `hcl:"args,optional"`
}
// NetworkResource is used to describe required network
// resources of a given task.
type NetworkResource struct {
Mode string `hcl:"mode,optional"`
Device string `hcl:"device,optional"`
CIDR string `hcl:"cidr,optional"`
IP string `hcl:"ip,optional"`
DNS *DNSConfig `hcl:"dns,block"`
ReservedPorts []Port `hcl:"reserved_ports,block"`
DynamicPorts []Port `hcl:"port,block"`
Hostname string `hcl:"hostname,optional"`
// COMPAT(0.13)
// XXX Deprecated. Please do not use. The field will be removed in Nomad
// 0.13 and is only being kept to allow any references to be removed before
// then.
MBits *int `hcl:"mbits,optional"`
CNI *CNIConfig `hcl:"cni,block"`
}
// COMPAT(0.13)
// XXX Deprecated. Please do not use. The method will be removed in Nomad
// 0.13 and is only being kept to allow any references to be removed before
// then.
func (n *NetworkResource) Megabits() int {
if n == nil || n.MBits == nil {
return 0
}
return *n.MBits
}
func (n *NetworkResource) Canonicalize() {
// COMPAT(0.13)
// Noop to maintain backwards compatibility
}
func (n *NetworkResource) HasPorts() bool {
if n == nil {
return false
}
return len(n.ReservedPorts)+len(n.DynamicPorts) > 0
}
// NodeDeviceResource captures a set of devices sharing a common
// vendor/type/device_name tuple.
type NodeDeviceResource struct {
// Vendor specifies the vendor of device
Vendor string
// Type specifies the type of the device
Type string
// Name specifies the specific model of the device
Name string
// Instances are list of the devices matching the vendor/type/name
Instances []*NodeDevice
Attributes map[string]*Attribute
}
func (r NodeDeviceResource) ID() string {
return r.Vendor + "/" + r.Type + "/" + r.Name
}
// NodeDevice is an instance of a particular device.
type NodeDevice struct {
// ID is the ID of the device.
ID string
// Healthy captures whether the device is healthy.
Healthy bool
// HealthDescription is used to provide a human readable description of why
// the device may be unhealthy.
HealthDescription string
// Locality stores HW locality information for the node to optionally be
// used when making placement decisions.
Locality *NodeDeviceLocality
}
// Attribute is used to describe the value of an attribute, optionally
// specifying units
type Attribute struct {
// Float is the float value for the attribute
FloatVal *float64 `json:"Float,omitempty"`
// Int is the int value for the attribute
IntVal *int64 `json:"Int,omitempty"`
// String is the string value for the attribute
StringVal *string `json:"String,omitempty"`
// Bool is the bool value for the attribute
BoolVal *bool `json:"Bool,omitempty"`
// Unit is the optional unit for the set int or float value
Unit string
}
func (a Attribute) String() string {
switch {
case a.FloatVal != nil:
str := formatFloat(*a.FloatVal, 3)
if a.Unit != "" {
str += " " + a.Unit
}
return str
case a.IntVal != nil:
str := strconv.FormatInt(*a.IntVal, 10)
if a.Unit != "" {
str += " " + a.Unit
}
return str
case a.StringVal != nil:
return *a.StringVal
case a.BoolVal != nil:
return strconv.FormatBool(*a.BoolVal)
default:
return "<unknown>"
}
}
// NodeDeviceLocality stores information about the devices hardware locality on
// the node.
type NodeDeviceLocality struct {
// PciBusID is the PCI Bus ID for the device.
PciBusID string
}
// RequestedDevice is used to request a device for a task.
type RequestedDevice struct {
// Name is the request name. The possible values are as follows:
// * <type>: A single value only specifies the type of request.
// * <vendor>/<type>: A single slash delimiter assumes the vendor and type of device is specified.
// * <vendor>/<type>/<name>: Two slash delimiters assume vendor, type and specific model are specified.
//
// Examples are as follows:
// * "gpu"
// * "nvidia/gpu"
// * "nvidia/gpu/GTX2080Ti"
Name string `hcl:",label"`
// Count is the number of requested devices
Count *uint64 `hcl:"count,optional"`
// Constraints are a set of constraints to apply when selecting the device
// to use.
Constraints []*Constraint `hcl:"constraint,block"`
// Affinities are a set of affinites to apply when selecting the device
// to use.
Affinities []*Affinity `hcl:"affinity,block"`
}
func (d *RequestedDevice) Canonicalize() {
if d.Count == nil {
d.Count = pointerOf(uint64(1))
}
for _, a := range d.Affinities {
a.Canonicalize()
}
}