From 2018a5ef0835590ec1d93ce59d9835972e74e18a Mon Sep 17 00:00:00 2001 From: Will Thames Date: Fri, 3 Feb 2023 09:51:52 +1000 Subject: [PATCH] fix: Ensure that resource limits that exactly equal capacity are allowed (#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 --- pkg/apis/v1alpha5/limits.go | 2 +- pkg/apis/v1alpha5/suite_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/pkg/apis/v1alpha5/limits.go b/pkg/apis/v1alpha5/limits.go index 49da47da54ac..4dcee0217080 100644 --- a/pkg/apis/v1alpha5/limits.go +++ b/pkg/apis/v1alpha5/limits.go @@ -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()) } } diff --git a/pkg/apis/v1alpha5/suite_test.go b/pkg/apis/v1alpha5/suite_test.go index 3d3340cdebcd..9782dd29165e 100644 --- a/pkg/apis/v1alpha5/suite_test.go +++ b/pkg/apis/v1alpha5/suite_test.go @@ -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")) + }) +})