-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathresourcetypes.go
102 lines (83 loc) · 2.25 KB
/
resourcetypes.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
package kube
import (
"math"
sdk "github.com/cosmos/cosmos-sdk/types"
"k8s.io/apimachinery/pkg/api/resource"
types "github.com/akash-network/akash-api/go/node/types/v1beta3"
)
type resourcePair struct {
allocatable resource.Quantity
allocated resource.Quantity
}
// type clusterStorage map[string]*resourcePair
//
// func (cs clusterStorage) dup() clusterStorage {
// res := make(clusterStorage)
// for class, resources := range cs {
// res[class] = resources.dup()
// }
//
// return res
// }
//
// func newResourcePair(allocatable, allocated resource.Quantity) resourcePair {
// rp := resourcePair{
// allocatable: allocatable,
// allocated: allocated,
// }
//
// return rp
// }
//
// func rpNewFromAkash(res crd.ResourcePair) *resourcePair {
// return &resourcePair{
// allocatable: *resource.NewQuantity(int64(res.Allocatable), resource.DecimalSI),
// allocated: *resource.NewQuantity(int64(res.Allocated), resource.DecimalSI),
// }
// }
//
// func (rp *resourcePair) dup() *resourcePair {
// return &resourcePair{
// allocatable: rp.allocatable.DeepCopy(),
// allocated: rp.allocated.DeepCopy(),
// }
// }
func (rp *resourcePair) subMilliNLZ(val types.ResourceValue) bool {
avail := rp.available()
res := sdk.NewInt(avail.MilliValue())
res = res.Sub(val.Val)
if res.IsNegative() {
return false
}
allocated := rp.allocated.DeepCopy()
allocated.Add(*resource.NewMilliQuantity(int64(val.Value()), resource.DecimalSI)) // nolint: gosec
*rp = resourcePair{
allocatable: rp.allocatable.DeepCopy(),
allocated: allocated,
}
return true
}
func (rp *resourcePair) subNLZ(val types.ResourceValue) bool {
avail := rp.available()
res := sdk.NewInt(avail.Value())
res = res.Sub(val.Val)
if res.IsNegative() {
return false
}
allocated := rp.allocated.DeepCopy()
allocated.Add(*resource.NewQuantity(int64(val.Value()), resource.DecimalSI)) // nolint: gosec
*rp = resourcePair{
allocatable: rp.allocatable.DeepCopy(),
allocated: allocated,
}
return true
}
func (rp *resourcePair) available() resource.Quantity {
result := rp.allocatable.DeepCopy()
if result.Value() == -1 {
result = *resource.NewQuantity(math.MaxInt64, resource.DecimalSI)
}
// Modifies the value in place
(&result).Sub(rp.allocated)
return result
}