Skip to content

Commit

Permalink
fix: Ensure that resource limits that exactly equal capacity are allo…
Browse files Browse the repository at this point in the history
…wed (#200)

* Ensure that resource limits that exactly equal capacity are allowed

Change the definition of ExceededBy to allow exact resource
matches (this can happen if a Provisioner has
`spec.resources.limits.cpu` set to a value identical to a node's
CPU count (or a multiple of a node's CPU count))

* Add test case for limits ExceededBy method
  • Loading branch information
willthames authored Feb 2, 2023
1 parent 9766dd1 commit 2018a5e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/apis/v1alpha5/limits.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (l *Limits) ExceededBy(resources v1.ResourceList) error {
}
for resourceName, usage := range resources {
if limit, ok := l.Resources[resourceName]; ok {
if usage.Cmp(limit) >= 0 {
if usage.Cmp(limit) > 0 {
return fmt.Errorf("%s resource usage of %v exceeds limit of %v", resourceName, usage.AsDec(), limit.AsDec())
}
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/apis/v1alpha5/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,3 +482,33 @@ var _ = Describe("Validation", func() {
})
})
})

var _ = Describe("Limits", func() {
var provisioner *Provisioner

BeforeEach(func() {
provisioner = &Provisioner{
ObjectMeta: metav1.ObjectMeta{Name: strings.ToLower(randomdata.SillyName())},
Spec: ProvisionerSpec{
Limits: &Limits{
Resources: v1.ResourceList{
"cpu": resource.MustParse("16"),
},
},
},
}
})

It("should work when usage is lower than limit", func() {
provisioner.Status.Resources = v1.ResourceList{"cpu": resource.MustParse("15")}
Expect(provisioner.Spec.Limits.ExceededBy(provisioner.Status.Resources)).To(Succeed())
})
It("should work when usage is equal to limit", func() {
provisioner.Status.Resources = v1.ResourceList{"cpu": resource.MustParse("16")}
Expect(provisioner.Spec.Limits.ExceededBy(provisioner.Status.Resources)).To(Succeed())
})
It("should fail when usage is higher than limit", func() {
provisioner.Status.Resources = v1.ResourceList{"cpu": resource.MustParse("17")}
Expect(provisioner.Spec.Limits.ExceededBy(provisioner.Status.Resources)).To(MatchError("cpu resource usage of 17 exceeds limit of 16"))
})
})

0 comments on commit 2018a5e

Please sign in to comment.