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

Commit

Permalink
Merge pull request #2471 from tylerslaton/revert-cc-quota
Browse files Browse the repository at this point in the history
Revert "enhancement: add ComputeClasses and VolumeClasses fields to BaseResources"
  • Loading branch information
tylerslaton authored Feb 1, 2024
2 parents c73cd69 + 8b44ad3 commit 92d24ac
Show file tree
Hide file tree
Showing 16 changed files with 412 additions and 1,708 deletions.
87 changes: 39 additions & 48 deletions pkg/apis/internal.admin.acorn.io/v1/baseresources.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package v1

import (
"errors"
"fmt"
"strings"

"k8s.io/apimachinery/pkg/api/resource"
)

// BaseResources defines resources that should be tracked at any scoped. The two main exclusions
Expand All @@ -15,10 +16,9 @@ type BaseResources struct {
Volumes int `json:"volumes"`
Images int `json:"images"`

// ComputeClasses and VolumeClasses are used to track the amount of compute and volume storage per their
// respective classes
ComputeClasses ComputeClassResources `json:"computeClasses"`
VolumeClasses VolumeClassResources `json:"volumeClasses"`
VolumeStorage resource.Quantity `json:"volumeStorage"`
Memory resource.Quantity `json:"memory"`
CPU resource.Quantity `json:"cpu"`
}

// Add will add the BaseResources of another BaseResources struct into the current one.
Expand All @@ -29,14 +29,9 @@ func (current *BaseResources) Add(incoming BaseResources) {
current.Volumes = Add(current.Volumes, incoming.Volumes)
current.Images = Add(current.Images, incoming.Images)

if current.ComputeClasses == nil {
current.ComputeClasses = ComputeClassResources{}
}
if current.VolumeClasses == nil {
current.VolumeClasses = VolumeClassResources{}
}
current.ComputeClasses.Add(incoming.ComputeClasses)
current.VolumeClasses.Add(incoming.VolumeClasses)
current.VolumeStorage = AddQuantity(current.VolumeStorage, incoming.VolumeStorage)
current.Memory = AddQuantity(current.Memory, incoming.Memory)
current.CPU = AddQuantity(current.CPU, incoming.CPU)
}

// Remove will remove the BaseResources of another BaseResources struct from the current one. Calling remove
Expand All @@ -47,9 +42,13 @@ func (current *BaseResources) Remove(incoming BaseResources, all bool) {
current.Jobs = Sub(current.Jobs, incoming.Jobs)
current.Volumes = Sub(current.Volumes, incoming.Volumes)
current.Images = Sub(current.Images, incoming.Images)
current.ComputeClasses.Remove(incoming.ComputeClasses)

current.Memory = SubQuantity(current.Memory, incoming.Memory)
current.CPU = SubQuantity(current.CPU, incoming.CPU)

// Only remove persistent resources if all is true.
if all {
current.VolumeClasses.Remove(incoming.VolumeClasses)
current.VolumeStorage = SubQuantity(current.VolumeStorage, incoming.VolumeStorage)
}
}

Expand All @@ -59,7 +58,6 @@ func (current *BaseResources) Remove(incoming BaseResources, all bool) {
// If the current BaseResources defines unlimited, then it will always fit.
func (current *BaseResources) Fits(incoming BaseResources) error {
var exceededResources []string
var errs []error

// Check if any of the resources are exceeded
for _, r := range []struct {
Expand All @@ -77,51 +75,43 @@ func (current *BaseResources) Fits(incoming BaseResources) error {
}
}

if len(exceededResources) != 0 {
errs = append(errs, fmt.Errorf("%w: %s", ErrExceededResources, strings.Join(exceededResources, ", ")))
}

if err := current.ComputeClasses.Fits(incoming.ComputeClasses); err != nil {
errs = append(errs, err)
// Check if any of the quantity resources are exceeded
for _, r := range []struct {
resource string
current, incoming resource.Quantity
}{
{"VolumeStorage", current.VolumeStorage, incoming.VolumeStorage},
{"Memory", current.Memory, incoming.Memory},
{"Cpu", current.CPU, incoming.CPU},
} {
if !FitsQuantity(r.current, r.incoming) {
exceededResources = append(exceededResources, r.resource)
}
}

if err := current.VolumeClasses.Fits(incoming.VolumeClasses); err != nil {
errs = append(errs, err)
// Build an aggregated error message for the exceeded resources
if len(exceededResources) > 0 {
return fmt.Errorf("%w: %s", ErrExceededResources, strings.Join(exceededResources, ", "))
}

// Build an aggregated error message for the exceeded resources
return errors.Join(errs...)
return nil
}

// ToString will return a string representation of the BaseResources within the struct.
func (current *BaseResources) ToString() string {
// make sure that an empty string doesn't have a comma
result := CountResourcesToString(
return ResourcesToString(
map[string]int{
"Apps": current.Apps,
"Containers": current.Containers,
"Jobs": current.Jobs,
"Volumes": current.Volumes,
"Images": current.Images,
},
)

for _, resource := range []struct {
name string
asString string
}{
{"ComputeClasses", current.ComputeClasses.ToString()},
{"VolumeClasses", current.VolumeClasses.ToString()},
} {
if result != "" && resource.asString != "" {
result += ", "
}
if resource.asString != "" {
result += fmt.Sprintf("%s: %s", resource.name, resource.asString)
}
}

return result
map[string]resource.Quantity{
"VolumeStorage": current.VolumeStorage,
"Memory": current.Memory,
"Cpu": current.CPU,
})
}

// Equals will check if the current BaseResources struct is equal to another. This is useful
Expand All @@ -132,6 +122,7 @@ func (current *BaseResources) Equals(incoming BaseResources) bool {
current.Jobs == incoming.Jobs &&
current.Volumes == incoming.Volumes &&
current.Images == incoming.Images &&
current.ComputeClasses.Equals(incoming.ComputeClasses) &&
current.VolumeClasses.Equals(incoming.VolumeClasses)
current.VolumeStorage.Cmp(incoming.VolumeStorage) == 0 &&
current.Memory.Cmp(incoming.Memory) == 0 &&
current.CPU.Cmp(incoming.CPU) == 0
}
Loading

0 comments on commit 92d24ac

Please sign in to comment.