From 6f705fe0e9d321aa5f5beba5b189f613ee89c303 Mon Sep 17 00:00:00 2001 From: Mattia Eleuteri Date: Wed, 18 Nov 2026 15:58:39 +0100 Subject: [PATCH] fix(api): keep exact quantity products the int64 coefficient cannot hold resource.Quantity.Mul returns false when the product no longer fits an int64 coefficient, not when it is wrong: it carries on in arbitrary precision. The evaluator read that as an overflow and failed with "quantity 20992Mi * 3 overflows" for any fractional binary quantity above about 8.5Gi, since such a quantity is stored at a scale whose coefficient leaves int64 long before its value does. Declared sizes like 20.5Gi are ordinary, so every pool or disk using one could not be reserved. Tested with fractional Gi and Ti quantities scaled directly and after a sum. Fixes: 89aba6e104c7 ("feat(api): add the reservation evaluator for tenant quotas") Assisted-by: LLM Signed-off-by: Mattia Eleuteri --- pkg/reservation/evaluator.go | 8 +++++--- pkg/reservation/evaluator_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/pkg/reservation/evaluator.go b/pkg/reservation/evaluator.go index 04344501c..1bd376051 100644 --- a/pkg/reservation/evaluator.go +++ b/pkg/reservation/evaluator.go @@ -285,9 +285,11 @@ func mulQuantity(q, n ref.Val) ref.Val { return types.MaybeNoSuchOverloadErr(n) } out := qq.DeepCopy() - if !out.Mul(int64(nn)) { - return types.NewErr("quantity %s * %d overflows", qq.String(), int64(nn)) - } + // Mul reports whether the product still fits an int64 coefficient, not + // whether it is exact: past that it carries on in arbitrary precision, + // which a fractional binary quantity such as 20.5Gi reaches at any size + // worth reserving. + out.Mul(int64(nn)) return apiservercel.Quantity{Quantity: &out} } diff --git a/pkg/reservation/evaluator_test.go b/pkg/reservation/evaluator_test.go index 6dc48fb0e..01f57d55e 100644 --- a/pkg/reservation/evaluator_test.go +++ b/pkg/reservation/evaluator_test.go @@ -467,6 +467,34 @@ func TestEvaluateQuantityArithmetic(t *testing.T) { } } +// A fractional binary quantity is stored at a scale whose coefficient leaves +// int64 well before its value does, which is where resource.Quantity.Mul +// reports an inexact int64 for a product it still holds exactly. +func TestEvaluateMulKeepsExactProducts(t *testing.T) { + env := testEnvironment(t) + cases := map[string]struct { + expr string + want string + }{ + "fractional Gi": {`quantity('20.5Gi').mul(3)`, "62976Mi"}, + "fractional Ti": {`quantity('1.5Ti').mul(3)`, "4608Gi"}, + "after a sum": {`quantity('8.5Gi').add(quantity('1Gi')).mul(2)`, "19Gi"}, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + ev, err := Compile(&Declaration{Resources: map[string]string{"x": tc.expr}}, nil) + if err != nil { + t.Fatal(err) + } + got, err := ev.Evaluate(map[string]any{}, env) + if err != nil { + t.Fatal(err) + } + assertList(t, name, got, rl("x", tc.want)) + }) + } +} + func TestEvaluateIsConcurrencySafe(t *testing.T) { env := testEnvironment(t) vm := mustCompile(t, vmInstanceDecl, "vm-instance")