diff --git a/pkg/reservation/evaluator.go b/pkg/reservation/evaluator.go index 1bd376051..6d121b090 100644 --- a/pkg/reservation/evaluator.go +++ b/pkg/reservation/evaluator.go @@ -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) + } + } +} diff --git a/pkg/reservation/evaluator_test.go b/pkg/reservation/evaluator_test.go index 01f57d55e..afc8c0851 100644 --- a/pkg/reservation/evaluator_test.go +++ b/pkg/reservation/evaluator_test.go @@ -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")