Skip to content
This repository was archived by the owner on Mar 16, 2024. It is now read-only.

Commit 8113ff8

Browse files
committed
Improve readability of Resources helper functions
Signed-off-by: tylerslaton <[email protected]>
1 parent ac95653 commit 8113ff8

File tree

1 file changed

+65
-65
lines changed

1 file changed

+65
-65
lines changed

pkg/apis/internal.admin.acorn.io/v1/resources.go

Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,28 @@ func (current *Resources) Add(incoming Resources) {
4343

4444
// Remove will remove the resources of another Resources struct from the current one.
4545
func (current *Resources) Remove(incoming Resources, all bool) {
46-
current.Apps -= incoming.Apps
47-
current.Containers -= incoming.Containers
48-
current.Jobs -= incoming.Jobs
49-
current.Volumes -= incoming.Volumes
50-
current.Images -= incoming.Images
51-
current.Projects -= incoming.Projects
46+
// Do not allow resources to go below 0
47+
nonNegativeSubtract := func(currentVal, incomingVal int) int {
48+
difference := currentVal - incomingVal
49+
if difference < 0 {
50+
difference = 0
51+
}
52+
return difference
53+
}
54+
55+
current.Apps = nonNegativeSubtract(current.Apps, incoming.Apps)
56+
current.Containers = nonNegativeSubtract(current.Containers, incoming.Containers)
57+
current.Jobs = nonNegativeSubtract(current.Jobs, incoming.Jobs)
58+
current.Volumes = nonNegativeSubtract(current.Volumes, incoming.Volumes)
59+
current.Images = nonNegativeSubtract(current.Images, incoming.Images)
60+
current.Projects = nonNegativeSubtract(current.Projects, incoming.Projects)
5261

5362
current.Memory.Sub(incoming.Memory)
5463
current.CPU.Sub(incoming.CPU)
5564

5665
// Only remove persistent resources if all is true.
5766
if all {
58-
current.Secrets -= incoming.Secrets
67+
current.Secrets = nonNegativeSubtract(current.Secrets, incoming.Secrets)
5968
current.VolumeStorage.Sub(incoming.VolumeStorage)
6069
}
6170
}
@@ -65,38 +74,33 @@ func (current *Resources) Remove(incoming Resources, all bool) {
6574
// an aggregated error will be returned with all exceeded resources.
6675
func (current *Resources) Fits(incoming Resources) error {
6776
exceededResources := []string{}
68-
if current.Apps <= incoming.Apps {
69-
exceededResources = append(exceededResources, "Apps")
70-
}
71-
if current.Containers <= incoming.Containers {
72-
exceededResources = append(exceededResources, "Containers")
73-
}
74-
if current.Jobs <= incoming.Jobs {
75-
exceededResources = append(exceededResources, "Jobs")
76-
}
77-
if current.Volumes <= incoming.Volumes {
78-
exceededResources = append(exceededResources, "Volumes")
79-
}
80-
if current.Secrets <= incoming.Secrets {
81-
exceededResources = append(exceededResources, "Secrets")
82-
}
83-
if current.Images <= incoming.Images {
84-
exceededResources = append(exceededResources, "Images")
85-
}
86-
if current.Projects <= incoming.Projects {
87-
exceededResources = append(exceededResources, "Projects")
88-
}
8977

90-
if current.VolumeStorage.Cmp(incoming.VolumeStorage) <= 0 {
91-
exceededResources = append(exceededResources, "VolumeStorage")
92-
}
93-
if current.Memory.Cmp(incoming.Memory) <= 0 {
94-
exceededResources = append(exceededResources, "Memory")
78+
// Define function for checking int resources to keep code DRY
79+
checkResource := func(resource string, currentVal, incomingVal int) {
80+
if currentVal <= incomingVal {
81+
exceededResources = append(exceededResources, resource)
82+
}
9583
}
96-
if current.CPU.Cmp(incoming.CPU) <= 0 {
97-
exceededResources = append(exceededResources, "Cpu")
84+
85+
// Define function for checking quantity resources to keep code DRY
86+
checkQuantityResource := func(resource string, currentVal, incomingVal resource.Quantity) {
87+
if currentVal.Cmp(incomingVal) <= 0 {
88+
exceededResources = append(exceededResources, resource)
89+
}
9890
}
9991

92+
checkResource("Apps", current.Apps, incoming.Apps)
93+
checkResource("Containers", current.Containers, incoming.Containers)
94+
checkResource("Jobs", current.Jobs, incoming.Jobs)
95+
checkResource("Volumes", current.Volumes, incoming.Volumes)
96+
checkResource("Secrets", current.Secrets, incoming.Secrets)
97+
checkResource("Images", current.Images, incoming.Images)
98+
checkResource("Projects", current.Projects, incoming.Projects)
99+
100+
checkQuantityResource("VolumeStorage", current.VolumeStorage, incoming.VolumeStorage)
101+
checkQuantityResource("Memory", current.Memory, incoming.Memory)
102+
checkQuantityResource("Cpu", current.CPU, incoming.CPU)
103+
100104
// Build an aggregated error message for the exceeded resources
101105
if len(exceededResources) > 0 {
102106
return fmt.Errorf("quota would be exceeded for resources: %s", strings.Join(exceededResources, ", "))
@@ -105,41 +109,37 @@ func (current *Resources) Fits(incoming Resources) error {
105109
return nil
106110
}
107111

108-
// ToString will return a string representation of the Resources struct.
112+
// NonEmptyString will return a string representation of the non-empty
113+
// Resources within the struct.
109114
func (current *Resources) NonEmptyString() string {
110-
resources := []string{}
111-
if current.Apps > 0 {
112-
resources = append(resources, "Apps")
113-
}
114-
if current.Containers > 0 {
115-
resources = append(resources, "Containers")
116-
}
117-
if current.Jobs > 0 {
118-
resources = append(resources, "Jobs")
119-
}
120-
if current.Volumes > 0 {
121-
resources = append(resources, "Volumes")
122-
}
123-
if current.Secrets > 0 {
124-
resources = append(resources, "Secrets")
125-
}
126-
if current.Images > 0 {
127-
resources = append(resources, "Images")
128-
}
129-
if current.Projects > 0 {
130-
resources = append(resources, "Images")
131-
}
115+
var resources []string
132116

133-
if !current.VolumeStorage.IsZero() {
134-
resources = append(resources, "VolumeStorage")
135-
}
136-
if !current.Memory.IsZero() {
137-
resources = append(resources, "Memory")
117+
// Define function for checking int resources to keep code DRY
118+
checkResource := func(resource string, value int) {
119+
if value > 0 {
120+
resources = append(resources, resource)
121+
}
138122
}
139-
if !current.CPU.IsZero() {
140-
resources = append(resources, "CPU")
123+
124+
// Define function for checking quantity resources to keep code DRY
125+
checkQuantityResource := func(resource string, currentVal resource.Quantity) {
126+
if !currentVal.IsZero() {
127+
resources = append(resources, resource)
128+
}
141129
}
142130

131+
checkResource("Apps", current.Apps)
132+
checkResource("Containers", current.Containers)
133+
checkResource("Jobs", current.Jobs)
134+
checkResource("Volumes", current.Volumes)
135+
checkResource("Secrets", current.Secrets)
136+
checkResource("Images", current.Images)
137+
checkResource("Projects", current.Projects)
138+
139+
checkQuantityResource("VolumeStorage", current.VolumeStorage)
140+
checkQuantityResource("Memory", current.Memory)
141+
checkQuantityResource("Cpu", current.CPU)
142+
143143
return strings.Join(resources, ", ")
144144
}
145145

0 commit comments

Comments
 (0)