fix(api): read a null value as absent in a reservation declaration

Helm deletes a key its values set to null, so a chart treats
`resources: {cpu: null}` as if cpu were not set. The evaluator's
defaulting replaced a null only where the schema had a default to put
in its place, so elsewhere the key stayed present: has() reported it,
and an expression reading it failed, for instance with "no such
overload: matches", on a spec the chart renders.

Nulls are dropped after defaulting, so a schema default still fills a
nulled key the way it fills an absent one.

Tested with a nulled key that has no default.

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:
Mattia Eleuteri committed 2026-11-23 08:03:49 +01:00
1 parent 6f705fe0e9
commit a069636a4e
2 files changed
+107

No files matched your search

+64
View File
@@ -191,9 +191,19 @@ func (e *Evaluator) Evaluate(values map[string]any, env Environment) (ResourceLi
if err := checkTypes(defaulted, e.schema, ""); err != nil {
return nil, err
}
// Helm deletes a key its values set to null, the chart's own default
// included, so a nulled scalar is kept out after defaulting. A nulled
// object or list keeps its default: charts read those through
// `default dict` and `default list`, which renders the same as the empty
// default the schema puts back.
nulled := takeNulls(defaulted, nil)
if err := defaulting.Apply(&defaulted, e.schema); err != nil {
return nil, fmt.Errorf("default values: %w", err)
}
for _, path := range nulled {
deleteScalarAt(defaulted, path)
}
dropNulls(defaulted)
// The environment differs per call, so instanceType and preset are
// declared with a late binding and bound here, per program; cel.Functions
@@ -471,3 +481,57 @@ func normalizeNumbers(v any) any {
return v
}
}
// takeNulls removes, in place, every null-valued key of the objects nested in
// m through objects, and returns their paths.
func takeNulls(m map[string]any, prefix []string) [][]string {
var paths [][]string
for k, v := range m {
path := append(append([]string(nil), prefix...), k)
switch t := v.(type) {
case nil:
delete(m, k)
paths = append(paths, path)
case map[string]any:
paths = append(paths, takeNulls(t, path)...)
}
}
return paths
}
// deleteScalarAt removes the key at path when it holds neither an object nor
// a list, if every object on the way exists.
func deleteScalarAt(m map[string]any, path []string) {
for _, k := range path[:len(path)-1] {
next, ok := m[k].(map[string]any)
if !ok {
return
}
m = next
}
last := path[len(path)-1]
switch m[last].(type) {
case map[string]any, []any:
return
}
delete(m, last)
}
// dropNulls removes, in place, every null-valued key of m and of the objects
// nested in it, lists included.
func dropNulls(v any) {
switch t := v.(type) {
case map[string]any:
for k, vv := range t {
if vv == nil {
delete(t, k)
continue
}
dropNulls(vv)
}
case []any:
for _, vv := range t {
dropNulls(vv)
}
}
}
+43
View File
@@ -495,6 +495,49 @@ func TestEvaluateMulKeepsExactProducts(t *testing.T) {
}
}
// A null is absent to Helm, which deletes the key it nulls, so it must be
// absent to an expression too, including where the schema has no default to
// put in its place.
func TestEvaluateTreatsNullAsAbsent(t *testing.T) {
env := testEnvironment(t)
ev, err := Compile(&Declaration{Resources: map[string]string{
"cpu": `has(values.r.cpu) ? 1 : 2`,
}}, nil)
if err != nil {
t.Fatal(err)
}
got, err := ev.Evaluate(map[string]any{"r": map[string]any{"cpu": nil}}, env)
if err != nil {
t.Fatal(err)
}
assertList(t, "null", got, rl("cpu", "2"))
}
// Helm deletes a nulled key from the chart's defaults as well, so a null must
// not be replaced by the schema default either.
func TestEvaluateNullOverridesDefault(t *testing.T) {
env := testEnvironment(t)
ev := mustCompile(t, &Declaration{Resources: map[string]string{
"cpu": `has(values.instanceType) ? 1 : 2`,
}}, "vm-instance")
for name, tc := range map[string]struct {
values string
want string
}{
"omitted takes the default": {`{}`, "1"},
"null deletes the default": {`{"instanceType": null}`, "2"},
"nested null": {`{"resources": null}`, "1"},
} {
t.Run(name, func(t *testing.T) {
got, err := ev.EvaluateJSON([]byte(tc.values), env)
if err != nil {
t.Fatal(err)
}
assertList(t, name, got, rl("cpu", tc.want))
})
}
}
func TestEvaluateIsConcurrencySafe(t *testing.T) {
env := testEnvironment(t)
vm := mustCompile(t, vmInstanceDecl, "vm-instance")