From a9245ec165cdecab4b9bff4bfadae69c56e4831a Mon Sep 17 00:00:00 2001 From: Mattia Eleuteri Date: Wed, 18 Nov 2026 15:38:13 +0100 Subject: [PATCH] fix(api): accept the quantities the CEL add and sub return The Kubernetes CEL quantity library returns a pointer from add and sub, while quantity() and the evaluator's own mul return a value. The evaluator matched only the value form, so any declaration that summed quantities failed with "expression returned kubernetes.Quantity, expected an int, a double, a string or a quantity", or with "no such overload" when the sum was then scaled with mul. A kind whose reservation is a sum of components, such as a control plane, could not be declared at all. Both forms are matched by type rather than read through Value(), which would first materialize any other result, a list included, outside the expression's cost limit. Tested with add, sub, add of an int, and mul on either side of 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 | 24 +++++++++++++++++++++--- pkg/reservation/evaluator_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/pkg/reservation/evaluator.go b/pkg/reservation/evaluator.go index 40d5c2ced..04344501c 100644 --- a/pkg/reservation/evaluator.go +++ b/pkg/reservation/evaluator.go @@ -276,7 +276,7 @@ func lookup(resolve func(string) (ResourceList, error), zeroKeys []string) funct } func mulQuantity(q, n ref.Val) ref.Val { - qq, ok := q.(apiservercel.Quantity) + qq, ok := asQuantity(q) if !ok { return types.MaybeNoSuchOverloadErr(q) } @@ -291,10 +291,28 @@ func mulQuantity(q, n ref.Val) ref.Val { return apiservercel.Quantity{Quantity: &out} } -func toQuantity(v ref.Val) (resource.Quantity, error) { +// asQuantity reads a CEL quantity whichever form it takes: the Kubernetes +// library returns a pointer from add and sub, and a value from quantity(). +// It matches the types rather than calling Value(), which would first +// materialize any other result, a list included, outside the cost limit. +func asQuantity(v ref.Val) (*resource.Quantity, bool) { switch t := v.(type) { case apiservercel.Quantity: - return t.DeepCopy(), nil + return t.Quantity, t.Quantity != nil + case *apiservercel.Quantity: + if t == nil || t.Quantity == nil { + return nil, false + } + return t.Quantity, true + } + return nil, false +} + +func toQuantity(v ref.Val) (resource.Quantity, error) { + if q, ok := asQuantity(v); ok { + return q.DeepCopy(), nil + } + switch t := v.(type) { case types.Int: return *resource.NewQuantity(int64(t), resource.DecimalSI), nil case types.Uint: diff --git a/pkg/reservation/evaluator_test.go b/pkg/reservation/evaluator_test.go index 7aa7ef9a7..6dc48fb0e 100644 --- a/pkg/reservation/evaluator_test.go +++ b/pkg/reservation/evaluator_test.go @@ -437,6 +437,36 @@ func TestCompile(t *testing.T) { } } +// The Kubernetes quantity library returns a pointer from add and sub, where +// quantity() and mul return a value; a declaration summing several components +// mixes both. +func TestEvaluateQuantityArithmetic(t *testing.T) { + env := testEnvironment(t) + cases := map[string]struct { + expr string + want string + }{ + "add": {`quantity('1').add(quantity('500m'))`, "1500m"}, + "add an int": {`quantity('1').add(2)`, "3"}, + "sub": {`quantity('2Gi').sub(quantity('512Mi'))`, "1536Mi"}, + "mul after add": {`preset('t1.micro').cpu.add(preset('t1.nano').cpu).mul(2)`, "1500m"}, + "add after mul": {`quantity('1Gi').mul(2).add(quantity('1Gi'))`, "3Gi"}, + } + 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")