mirror of
https://github.com/cozystack/cozystack.git
synced 2026-11-30 04:06:34 +09:00
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: 89aba6e104 ("feat(api): add the reservation evaluator for tenant quotas")
Assisted-by: LLM
Signed-off-by: Mattia Eleuteri <mattia@hidora.io>
This commit is contained in:
2 files changed
+51
-3
No files matched your search
@@ -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:
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in new issue
Block a user