diff --git a/pkg/engine/funcs.go b/pkg/engine/funcs.go index a97f8f104..927ac3975 100644 --- a/pkg/engine/funcs.go +++ b/pkg/engine/funcs.go @@ -61,6 +61,8 @@ func funcMap() template.FuncMap { "fromJson": fromJSON, "fromJsonArray": fromJSONArray, + "omitted": omitted, + // This is a placeholder for the "include" function, which is // late-bound to a template. By declaring it here, we preserve the // integrity of the linter. @@ -232,3 +234,10 @@ func fromJSONArray(str string) []interface{} { } return a } + +func omitted(a, b interface{}) interface{} { + if b == nil { + return a + } + return b +} diff --git a/pkg/engine/funcs_test.go b/pkg/engine/funcs_test.go index 71a72e2e4..cde09eabb 100644 --- a/pkg/engine/funcs_test.go +++ b/pkg/engine/funcs_test.go @@ -122,6 +122,14 @@ keyInElement1 = "valueInElement1"`, tpl: `{{ fromYamlArray . }}`, expect: `[error unmarshaling JSON: while decoding JSON: json: cannot unmarshal object into Go value of type []interface {}]`, vars: `hello: world`, + }, { + tpl: `{{ .hello | omitted 'yes' }}`, + expect: `false`, + vars: map[string]bool{"hello": false}, + }, { + tpl: `{{ .value | omitted 'yes' }}`, + expect: `yes`, + vars: map[string]bool{"hello": false}, }, { // This should never result in a network lookup. Regression for #7955 tpl: `{{ lookup "v1" "Namespace" "" "unlikelynamespace99999999" }}`,