Skip to content

Commit

Permalink
Fix fits function to check that total values are non-negative (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis authored Jan 31, 2023
1 parent 53e783d commit 558e8c5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/cloudprovider/fake/cloudprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (c *CloudProvider) Create(ctx context.Context, machine *v1alpha5.Machine) (
instanceTypes := lo.Filter(lo.Must(c.GetInstanceTypes(ctx, &v1alpha5.Provisioner{})), func(i *cloudprovider.InstanceType, _ int) bool {
return reqs.Compatible(i.Requirements) == nil &&
len(i.Offerings.Requirements(reqs).Available()) > 0 &&
resources.Fits(resources.Merge(machine.Spec.Resources.Requests, i.Overhead.Total()), i.Capacity)
resources.Fits(machine.Spec.Resources.Requests, i.Allocatable())
})
// Order instance types so that we get the cheapest instance types of the available offerings
sort.Slice(instanceTypes, func(i, j int) bool {
Expand Down
6 changes: 6 additions & 0 deletions pkg/utils/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ func Cmp(lhs resource.Quantity, rhs resource.Quantity) int {

// Fits returns true if the candidate set of resources is less than or equal to the total set of resources.
func Fits(candidate, total v1.ResourceList) bool {
// If any of the total resource values are negative then the resource will never fit
for _, quantity := range total {
if Cmp(resource.MustParse("0"), quantity) > 0 {
return false
}
}
for resourceName, quantity := range candidate {
if Cmp(quantity, total[resourceName]) > 0 {
return false
Expand Down

0 comments on commit 558e8c5

Please sign in to comment.