-
Notifications
You must be signed in to change notification settings - Fork 129
/
resourcelist.go
346 lines (307 loc) · 9.28 KB
/
resourcelist.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package schedulerobjects
import (
"fmt"
"strings"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
)
func ResourceListFromV1ResourceList(rl v1.ResourceList) ResourceList {
rv := ResourceList{
Resources: make(map[string]resource.Quantity),
}
for t, q := range rl {
rv.Resources[string(t)] = q
}
return rv
}
func V1ResourceListFromResourceList(rl ResourceList) v1.ResourceList {
rv := make(v1.ResourceList)
for t, q := range rl.Resources {
rv[v1.ResourceName(t)] = q.DeepCopy()
}
return rv
}
type QuantityByPriorityAndResourceType map[int32]ResourceList
func (a QuantityByPriorityAndResourceType) DeepCopy() QuantityByPriorityAndResourceType {
rv := make(QuantityByPriorityAndResourceType)
for p, rl := range a {
rv[p] = rl.DeepCopy()
}
return rv
}
func (a QuantityByPriorityAndResourceType) String() string {
var sb strings.Builder
i := 0
sb.WriteString("{")
for p, rl := range a {
if i < len(a)-1 {
sb.WriteString(fmt.Sprintf("%d: %s, ", p, rl.CompactString()))
} else {
sb.WriteString(fmt.Sprintf("%d: %s", p, rl.CompactString()))
}
}
sb.WriteString("}")
return sb.String()
}
func (a QuantityByPriorityAndResourceType) Add(b QuantityByPriorityAndResourceType) {
for p, rlb := range b {
a.AddResourceList(p, rlb)
}
}
func (a QuantityByPriorityAndResourceType) Sub(b QuantityByPriorityAndResourceType) {
for p, rlb := range b {
a.SubResourceList(p, rlb)
}
}
func (a QuantityByPriorityAndResourceType) AddResourceList(priority int32, rlb ResourceList) {
rla := a[priority]
rla.Add(rlb)
a[priority] = rla
}
func (a QuantityByPriorityAndResourceType) SubResourceList(priority int32, rlb ResourceList) {
rla := a[priority]
rla.Sub(rlb)
a[priority] = rla
}
func (a QuantityByPriorityAndResourceType) Equal(b QuantityByPriorityAndResourceType) bool {
for p, rla := range a {
if !rla.Equal(b[p]) {
return false
}
}
for p, rlb := range b {
if !rlb.Equal(a[p]) {
return false
}
}
return true
}
// IsZero returns true if all quantities in a are zero.
func (a QuantityByPriorityAndResourceType) IsZero() bool {
for _, rl := range a {
if !rl.IsZero() {
return false
}
}
return true
}
// IsStrictlyNonNegative returns true if there are no quantities in a with value less than zero.
func (a QuantityByPriorityAndResourceType) IsStrictlyNonNegative() bool {
for _, rl := range a {
if !rl.IsStrictlyNonNegative() {
return false
}
}
return true
}
func (a QuantityByPriorityAndResourceType) AggregateByResource() ResourceList {
rv := ResourceList{
Resources: make(map[string]resource.Quantity),
}
for _, rl := range a {
rv.Add(rl)
}
return rv
}
// MaxAggregatedByResource updates a in-place such that for each resource type t
// a[p1][t] + ... + a[pn][t] = max(a[p1][t] + ... + a[pn][t], rl[t]),
// where p1, ..., pn are the priorities in a, for each resource set explicitly in rl.
//
// If necessary to add resources to make up the difference, those resources are added at priority p.
func (a QuantityByPriorityAndResourceType) MaxAggregatedByResource(p int32, rl ResourceList) {
aggregate := a.AggregateByResource()
var difference ResourceList
for t, q := range rl.Resources {
q = q.DeepCopy()
q.Sub(aggregate.Get(t))
if q.Cmp(resource.Quantity{}) == 1 {
difference.AddQuantity(t, q)
}
}
if len(difference.Resources) > 0 {
a.AddResourceList(p, difference)
}
}
func (a *ResourceList) Get(resourceType string) resource.Quantity {
if a.Resources == nil {
return resource.Quantity{}
}
return a.Resources[resourceType]
}
func (a *ResourceList) Add(b ResourceList) {
if a.Resources == nil {
a.Resources = make(map[string]resource.Quantity)
}
for t, qb := range b.Resources {
qa := a.Resources[t]
qa.Add(qb)
a.Resources[t] = qa
}
}
func (rl *ResourceList) AddQuantity(resourceType string, quantity resource.Quantity) {
rl.initialise()
q := rl.Resources[resourceType]
q.Add(quantity)
rl.Resources[resourceType] = q
}
func (a *ResourceList) Sub(b ResourceList) {
a.initialise()
for t, qb := range b.Resources {
qa := a.Resources[t]
qa.Sub(qb)
a.Resources[t] = qa
}
}
func (rl *ResourceList) SubQuantity(resourceType string, quantity resource.Quantity) {
rl.initialise()
q := rl.Resources[resourceType]
q.Sub(quantity)
rl.Resources[resourceType] = q
}
func (rl ResourceList) DeepCopy() ResourceList {
if rl.Resources == nil {
return ResourceList{}
}
rv := ResourceList{
Resources: make(map[string]resource.Quantity, len(rl.Resources)),
}
for t, q := range rl.Resources {
rv.Resources[t] = q.DeepCopy()
}
return rv
}
func (a ResourceList) IsZero() bool {
for _, q := range a.Resources {
if !q.IsZero() {
return false
}
}
return true
}
func (a ResourceList) Equal(b ResourceList) bool {
for t, qa := range a.Resources {
if qa.Cmp(b.Get(t)) != 0 {
return false
}
}
for t, qb := range b.Resources {
if qb.Cmp(a.Get(t)) != 0 {
return false
}
}
return true
}
// IsStrictlyNonNegative returns true if there is no quantity less than zero.
func (a ResourceList) IsStrictlyNonNegative() bool {
for _, q := range a.Resources {
if q.Cmp(resource.Quantity{}) == -1 {
return false
}
}
return true
}
// IsStrictlyLessOrEqual returns true if all quantities in a are strictly less or equal than those in b.
func (a ResourceList) IsStrictlyLessOrEqual(b ResourceList) bool {
for t, q := range b.Resources {
if q.Cmp(a.Get(t)) == -1 {
return false
}
}
return true
}
func (rl ResourceList) CompactString() string {
var sb strings.Builder
sb.WriteString("{")
i := 0
for t, q := range rl.Resources {
if i < len(rl.Resources)-1 {
sb.WriteString(fmt.Sprintf("%s: %s, ", t, q.String()))
} else {
sb.WriteString(fmt.Sprintf("%s: %s", t, q.String()))
}
i++
}
sb.WriteString("}")
return sb.String()
}
func (rl *ResourceList) initialise() {
if rl.Resources == nil {
rl.Resources = make(map[string]resource.Quantity)
}
}
// AllocatableByPriorityAndResourceType accounts for resources that can be allocated to pods of a given priority.
// E.g., AllocatableByPriorityAndResourceType[5]["cpu"] is the amount of CPU available to pods with priority 5,
// where alloctable resources = unused resources + resources allocated to lower-priority pods.
type AllocatableByPriorityAndResourceType QuantityByPriorityAndResourceType
func NewAllocatableByPriorityAndResourceType(priorities []int32, rl ResourceList) AllocatableByPriorityAndResourceType {
rv := make(AllocatableByPriorityAndResourceType)
for _, priority := range priorities {
rv[priority] = rl.DeepCopy()
}
return rv
}
func (m AllocatableByPriorityAndResourceType) DeepCopy() AllocatableByPriorityAndResourceType {
rv := make(AllocatableByPriorityAndResourceType, len(m))
for priority, resourcesAtPriority := range m {
rv[priority] = resourcesAtPriority.DeepCopy()
}
return rv
}
// MarkAllocated indicates resources have been allocated to pods of priority p,
// hence reducing the resources allocatable to pods of priority p or lower.
func (m AllocatableByPriorityAndResourceType) MarkAllocated(p int32, rs ResourceList) {
for priority, allocatableResourcesAtPriority := range m {
if priority <= p {
allocatableResourcesAtPriority.Sub(rs)
}
}
}
// MarkAllocatable indicates resources have been released by pods of priority p,
// thus increasing the resources allocatable to pods of priority p or lower.
func (m AllocatableByPriorityAndResourceType) MarkAllocatable(p int32, rs ResourceList) {
for priority, allocatableResourcesAtPriority := range m {
if priority <= p {
allocatableResourcesAtPriority.Add(rs)
}
}
}
// AllocatedByPriorityAndResourceType accounts for resources allocated to pods of a given priority or lower.
// E.g., AllocatedByPriorityAndResourceType[5]["cpu"] is the amount of CPU allocated to pods with priority 5 or lower.
type AllocatedByPriorityAndResourceType QuantityByPriorityAndResourceType
func NewAllocatedByPriorityAndResourceType(priorities []int32) AllocatedByPriorityAndResourceType {
rv := make(AllocatedByPriorityAndResourceType)
for _, priority := range priorities {
rv[priority] = ResourceList{Resources: make(map[string]resource.Quantity)}
}
return rv
}
// MarkAllocated increases the resources allocated to pods of priority p or lower.
func (m AllocatedByPriorityAndResourceType) MarkAllocated(p int32, rs ResourceList) {
for priority, allocatedResourcesAtPriority := range m {
if priority <= p {
allocatedResourcesAtPriority.Add(rs)
}
}
}
// MarkAllocatable reduces the resources allocated to pods of priority p or lower.
func (m AllocatedByPriorityAndResourceType) MarkAllocatable(p int32, rs ResourceList) {
for priority, allocatedResourcesAtPriority := range m {
if priority <= p {
allocatedResourcesAtPriority.Sub(rs)
}
}
}
func (allocatableByPriorityAndResourceType AllocatableByPriorityAndResourceType) Get(priority int32, resourceType string) resource.Quantity {
if allocatableByPriorityAndResourceType == nil {
return resource.Quantity{}
}
quantityByResourceType := allocatableByPriorityAndResourceType[priority]
return quantityByResourceType.Get(resourceType)
}
func (assignedByPriorityAndResourceType AllocatedByPriorityAndResourceType) Get(priority int32, resourceType string) resource.Quantity {
if assignedByPriorityAndResourceType == nil {
return resource.Quantity{}
}
quantityByResourceType := assignedByPriorityAndResourceType[priority]
return quantityByResourceType.Get(resourceType)
}