1
1
package v1
2
2
3
3
import (
4
- "errors"
5
4
"fmt"
6
5
"strings"
6
+
7
+ "k8s.io/apimachinery/pkg/api/resource"
7
8
)
8
9
9
10
// BaseResources defines resources that should be tracked at any scoped. The two main exclusions
@@ -15,10 +16,9 @@ type BaseResources struct {
15
16
Volumes int `json:"volumes"`
16
17
Images int `json:"images"`
17
18
18
- // ComputeClasses and VolumeClasses are used to track the amount of compute and volume storage per their
19
- // respective classes
20
- ComputeClasses ComputeClassResources `json:"computeClasses"`
21
- VolumeClasses VolumeClassResources `json:"volumeClasses"`
19
+ VolumeStorage resource.Quantity `json:"volumeStorage"`
20
+ Memory resource.Quantity `json:"memory"`
21
+ CPU resource.Quantity `json:"cpu"`
22
22
}
23
23
24
24
// Add will add the BaseResources of another BaseResources struct into the current one.
@@ -29,14 +29,9 @@ func (current *BaseResources) Add(incoming BaseResources) {
29
29
current .Volumes = Add (current .Volumes , incoming .Volumes )
30
30
current .Images = Add (current .Images , incoming .Images )
31
31
32
- if current .ComputeClasses == nil {
33
- current .ComputeClasses = ComputeClassResources {}
34
- }
35
- if current .VolumeClasses == nil {
36
- current .VolumeClasses = VolumeClassResources {}
37
- }
38
- current .ComputeClasses .Add (incoming .ComputeClasses )
39
- current .VolumeClasses .Add (incoming .VolumeClasses )
32
+ current .VolumeStorage = AddQuantity (current .VolumeStorage , incoming .VolumeStorage )
33
+ current .Memory = AddQuantity (current .Memory , incoming .Memory )
34
+ current .CPU = AddQuantity (current .CPU , incoming .CPU )
40
35
}
41
36
42
37
// Remove will remove the BaseResources of another BaseResources struct from the current one. Calling remove
@@ -47,9 +42,13 @@ func (current *BaseResources) Remove(incoming BaseResources, all bool) {
47
42
current .Jobs = Sub (current .Jobs , incoming .Jobs )
48
43
current .Volumes = Sub (current .Volumes , incoming .Volumes )
49
44
current .Images = Sub (current .Images , incoming .Images )
50
- current .ComputeClasses .Remove (incoming .ComputeClasses )
45
+
46
+ current .Memory = SubQuantity (current .Memory , incoming .Memory )
47
+ current .CPU = SubQuantity (current .CPU , incoming .CPU )
48
+
49
+ // Only remove persistent resources if all is true.
51
50
if all {
52
- current .VolumeClasses . Remove ( incoming .VolumeClasses )
51
+ current .VolumeStorage = SubQuantity ( current . VolumeStorage , incoming .VolumeStorage )
53
52
}
54
53
}
55
54
@@ -59,7 +58,6 @@ func (current *BaseResources) Remove(incoming BaseResources, all bool) {
59
58
// If the current BaseResources defines unlimited, then it will always fit.
60
59
func (current * BaseResources ) Fits (incoming BaseResources ) error {
61
60
var exceededResources []string
62
- var errs []error
63
61
64
62
// Check if any of the resources are exceeded
65
63
for _ , r := range []struct {
@@ -77,51 +75,43 @@ func (current *BaseResources) Fits(incoming BaseResources) error {
77
75
}
78
76
}
79
77
80
- if len (exceededResources ) != 0 {
81
- errs = append (errs , fmt .Errorf ("%w: %s" , ErrExceededResources , strings .Join (exceededResources , ", " )))
82
- }
83
-
84
- if err := current .ComputeClasses .Fits (incoming .ComputeClasses ); err != nil {
85
- errs = append (errs , err )
78
+ // Check if any of the quantity resources are exceeded
79
+ for _ , r := range []struct {
80
+ resource string
81
+ current , incoming resource.Quantity
82
+ }{
83
+ {"VolumeStorage" , current .VolumeStorage , incoming .VolumeStorage },
84
+ {"Memory" , current .Memory , incoming .Memory },
85
+ {"Cpu" , current .CPU , incoming .CPU },
86
+ } {
87
+ if ! FitsQuantity (r .current , r .incoming ) {
88
+ exceededResources = append (exceededResources , r .resource )
89
+ }
86
90
}
87
91
88
- if err := current .VolumeClasses .Fits (incoming .VolumeClasses ); err != nil {
89
- errs = append (errs , err )
92
+ // Build an aggregated error message for the exceeded resources
93
+ if len (exceededResources ) > 0 {
94
+ return fmt .Errorf ("%w: %s" , ErrExceededResources , strings .Join (exceededResources , ", " ))
90
95
}
91
96
92
- // Build an aggregated error message for the exceeded resources
93
- return errors .Join (errs ... )
97
+ return nil
94
98
}
95
99
96
100
// ToString will return a string representation of the BaseResources within the struct.
97
101
func (current * BaseResources ) ToString () string {
98
- // make sure that an empty string doesn't have a comma
99
- result := CountResourcesToString (
102
+ return ResourcesToString (
100
103
map [string ]int {
101
104
"Apps" : current .Apps ,
102
105
"Containers" : current .Containers ,
103
106
"Jobs" : current .Jobs ,
104
107
"Volumes" : current .Volumes ,
105
108
"Images" : current .Images ,
106
109
},
107
- )
108
-
109
- for _ , resource := range []struct {
110
- name string
111
- asString string
112
- }{
113
- {"ComputeClasses" , current .ComputeClasses .ToString ()},
114
- {"VolumeClasses" , current .VolumeClasses .ToString ()},
115
- } {
116
- if result != "" && resource .asString != "" {
117
- result += ", "
118
- }
119
- if resource .asString != "" {
120
- result += fmt .Sprintf ("%s: %s" , resource .name , resource .asString )
121
- }
122
- }
123
-
124
- return result
110
+ map [string ]resource.Quantity {
111
+ "VolumeStorage" : current .VolumeStorage ,
112
+ "Memory" : current .Memory ,
113
+ "Cpu" : current .CPU ,
114
+ })
125
115
}
126
116
127
117
// Equals will check if the current BaseResources struct is equal to another. This is useful
@@ -132,6 +122,7 @@ func (current *BaseResources) Equals(incoming BaseResources) bool {
132
122
current .Jobs == incoming .Jobs &&
133
123
current .Volumes == incoming .Volumes &&
134
124
current .Images == incoming .Images &&
135
- current .ComputeClasses .Equals (incoming .ComputeClasses ) &&
136
- current .VolumeClasses .Equals (incoming .VolumeClasses )
125
+ current .VolumeStorage .Cmp (incoming .VolumeStorage ) == 0 &&
126
+ current .Memory .Cmp (incoming .Memory ) == 0 &&
127
+ current .CPU .Cmp (incoming .CPU ) == 0
137
128
}
0 commit comments