Skip to content

Commit 14cfebe

Browse files
fix: improve arithmetic safety, parsing validation, and test reliability in quota enforcer and controller logic
1 parent 6c5c2da commit 14cfebe

6 files changed

Lines changed: 55 additions & 27 deletions

File tree

‎api/v1alpha1/error_rate.go‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ limitations under the License.
1616

1717
package v1alpha1
1818

19-
import "fmt"
19+
import (
20+
"fmt"
21+
"math"
22+
"strconv"
23+
)
2024

2125
// ParseErrorRate parses a percentage string like "2%" and returns the float64
2226
// value (e.g. 0.02 for "2%"). Returns an error if the format is invalid.
@@ -28,10 +32,17 @@ func ParseErrorRate(s string) (float64, error) {
2832
if s[len(s)-1] != '%' {
2933
return 0, fmt.Errorf("error rate must end with '%%': got %q", s)
3034
}
31-
var pct float64
32-
if _, err := fmt.Sscanf(s[:len(s)-1], "%f", &pct); err != nil {
35+
// strconv.ParseFloat rejects trailing garbage (e.g. "5x") and leading
36+
// whitespace (e.g. " 5"), unlike fmt.Sscanf which silently ignores them.
37+
pct, err := strconv.ParseFloat(s[:len(s)-1], 64)
38+
if err != nil {
3339
return 0, fmt.Errorf("parsing error rate %q: %w", s, err)
3440
}
41+
// Reject non-finite values (NaN, ±Inf) that ParseFloat may return for
42+
// inputs like "NaN" or "Inf".
43+
if math.IsNaN(pct) || math.IsInf(pct, 0) {
44+
return 0, fmt.Errorf("error rate %q is not a finite number", s)
45+
}
3546
if pct < 0 || pct > 100 {
3647
return 0, fmt.Errorf("error rate %q out of range [0, 100]", s)
3748
}

‎api/v1alpha1/error_rate_test.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ func TestParseErrorRate(t *testing.T) {
3838
{"-1%", 0, true},
3939
{"101%", 0, true},
4040
{"abc%", 0, true},
41+
// trailing garbage — strconv.ParseFloat must reject these
42+
{"5x%", 0, true},
43+
// non-finite numeric input
44+
{"NaN%", 0, true},
45+
// leading whitespace — strconv.ParseFloat must reject " 5"
46+
{" 5%", 0, true},
4147
}
4248
for _, tc := range tests {
4349
tc := tc

‎internal/controller/suite_test.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,5 +166,7 @@ var _ = AfterSuite(func() {
166166
cancel()
167167
// Wait for the manager goroutine to finish before stopping envtest.
168168
<-mgrDone
169+
// Stop the shared enforcer's background sweep goroutine.
170+
testEnforcer.Stop()
169171
Expect(testEnv.Stop()).To(Succeed())
170172
})

‎internal/controller/tenantquota_controller_test.go‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,9 @@ var _ = Describe("TenantQuota Controller", func() {
210210
f := &agentraxv1alpha1.TenantQuota{}
211211
g.Expect(k8sClient.Get(ctx, namespacedName("tq-clearoq", tqNS), f)).To(Succeed())
212212
cond := apimeta.FindStatusCondition(f.Status.Conditions, agentraxv1alpha1.ConditionOverQuota)
213-
// Condition should be absent or False once usage normalises.
214-
if cond != nil {
215-
g.Expect(cond.Status).To(Equal(metav1.ConditionFalse))
216-
}
213+
// The reconciler calls RemoveStatusCondition, so the condition
214+
// must be fully absent once usage normalises.
215+
g.Expect(cond).To(BeNil())
217216
}, timeout, interval).Should(Succeed())
218217
})
219218

@@ -264,6 +263,17 @@ var _ = Describe("TenantQuota Controller", func() {
264263
ad1 := makeBasicAD("ad-reject-1", tqNS, "tq-reject", 2)
265264
Expect(k8sClient.Create(ctx, ad1)).To(Succeed())
266265

266+
// Wait for the reconciler to commit ad1's usage into TQ status before
267+
// testing ad2. Without this, ad2 may be tried while the in-flight
268+
// reservation is still live and the quota arithmetic has not yet been
269+
// persisted, causing the rejection to rely solely on the reservation
270+
// TTL which may have already expired.
271+
Eventually(func(g Gomega) {
272+
f := &agentraxv1alpha1.TenantQuota{}
273+
g.Expect(k8sClient.Get(ctx, namespacedName("tq-reject", tqNS), f)).To(Succeed())
274+
g.Expect(f.Status.UsedAgents).To(BeNumerically("==", 1))
275+
}, timeout, interval).Should(Succeed())
276+
267277
// Second AD would push usedAgents to 2 > maxAgents=1 → rejected.
268278
ad2 := makeBasicAD("ad-reject-2", tqNS, "tq-reject", 2)
269279
err := k8sClient.Create(ctx, ad2)

‎internal/quota/enforcer.go‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package quota
2020

2121
import (
2222
"fmt"
23+
"math"
2324
"sync"
2425
"time"
2526

@@ -52,7 +53,9 @@ type Enforcer struct {
5253
reservations map[string]*reservationEntry // keyed by "namespace/adName"
5354

5455
// done is closed by Stop() to terminate the background sweep goroutine.
55-
done chan struct{}
56+
// stopOnce ensures close(done) is called exactly once.
57+
done chan struct{}
58+
stopOnce sync.Once
5659

5760
// nowFn is overridden in tests to control time.
5861
nowFn func() time.Time
@@ -76,10 +79,10 @@ func NewEnforcer(gpuResourceName string) *Enforcer {
7679
return e
7780
}
7881

79-
// Stop terminates the background sweep goroutine. Call this when the Enforcer
80-
// is no longer needed (e.g. in test teardown) to avoid goroutine leaks.
82+
// Stop terminates the background sweep goroutine. It is idempotent; repeated
83+
// calls are safe and will not panic.
8184
func (e *Enforcer) Stop() {
82-
close(e.done)
85+
e.stopOnce.Do(func() { close(e.done) })
8386
}
8487

8588
// sweepLoop removes expired in-flight reservations every second.
@@ -124,10 +127,16 @@ func (e *Enforcer) extractGPUs(resources corev1.ResourceRequirements) int64 {
124127
}
125128

126129
// gpusForAD returns the total GPU units for one AgentDeployment:
127-
// gpuPerReplica × spec.replicas.max.
130+
// gpuPerReplica × spec.replicas.max. The multiplication is performed in
131+
// int64 to prevent overflow, then clamped to the int32 range.
128132
func (e *Enforcer) gpusForAD(ad agentraxv1alpha1.AgentDeploymentSpec) int32 {
129133
perReplica := e.extractGPUs(ad.Resources)
130-
return int32(perReplica) * ad.Replicas.Max
134+
total := perReplica * int64(ad.Replicas.Max)
135+
const maxInt32 = int64(math.MaxInt32)
136+
if total > maxInt32 {
137+
return math.MaxInt32
138+
}
139+
return int32(total)
131140
}
132141

133142
// ComputeUsage aggregates resource usage across a slice of AgentDeployment specs

‎internal/quota/enforcer_test.go‎

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package quota_test
1818

1919
import (
20+
"strings"
2021
"testing"
2122
"time"
2223

@@ -156,7 +157,7 @@ func TestCanAdmit_Create(t *testing.T) {
156157
if !tc.wantAdmit && tc.wantContain != "" {
157158
if reason == "" {
158159
t.Errorf("CanAdmit() denied but returned empty reason")
159-
} else if !containsSubstring(reason, tc.wantContain) {
160+
} else if !strings.Contains(reason, tc.wantContain) {
160161
t.Errorf("CanAdmit() reason %q does not contain %q", reason, tc.wantContain)
161162
}
162163
}
@@ -222,7 +223,7 @@ func TestCanAdmit_Update_MaxReplicasPerAgent_Downgrade(t *testing.T) {
222223
if ok3 {
223224
t.Errorf("update increasing replicas.max beyond maxReplicasPerAgent should be rejected; got reason: %q", reason3)
224225
}
225-
if !containsSubstring(reason3, "maxReplicasPerAgent") {
226+
if !strings.Contains(reason3, "maxReplicasPerAgent") {
226227
t.Errorf("denial reason %q should mention maxReplicasPerAgent", reason3)
227228
}
228229
}
@@ -336,7 +337,7 @@ func TestIsOverQuota(t *testing.T) {
336337
if over != tc.wantOQ {
337338
t.Errorf("IsOverQuota() = %v, want %v; msg=%q", over, tc.wantOQ, msg)
338339
}
339-
if tc.wantOQ && !containsSubstring(msg, tc.wantMsg) {
340+
if tc.wantOQ && !strings.Contains(msg, tc.wantMsg) {
340341
t.Errorf("IsOverQuota() msg %q does not contain %q", msg, tc.wantMsg)
341342
}
342343
})
@@ -361,17 +362,6 @@ func TestParseErrorRate_ViaV1alpha1(t *testing.T) {
361362

362363
// ── helpers ───────────────────────────────────────────────────────────────────
363364

364-
func containsSubstring(s, sub string) bool {
365-
return len(sub) == 0 || (len(s) >= len(sub) && func() bool {
366-
for i := 0; i <= len(s)-len(sub); i++ {
367-
if s[i:i+len(sub)] == sub {
368-
return true
369-
}
370-
}
371-
return false
372-
}())
373-
}
374-
375365
func absFloat(f float64) float64 {
376366
if f < 0 {
377367
return -f

0 commit comments

Comments
 (0)