Skip to content

Commit 011f355

Browse files
authored
replace <no value> with empty string in all template outputs (#227)
1 parent ef05e65 commit 011f355

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

internal/helm/chart.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ func (c *Chart) render(name string, t0 *template.Template, capabilities *Capabil
362362
return nil, err
363363
}
364364

365-
decoder := utilyaml.NewYAMLToJSONDecoder(&buf)
365+
decoder := utilyaml.NewYAMLToJSONDecoder(bytes.NewBuffer(templatex.AdjustTemplateOutput(buf.Bytes())))
366366
for {
367367
object := &unstructured.Unstructured{}
368368
if err := decoder.Decode(&object.Object); err != nil {

internal/templatex/functions.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,15 +249,15 @@ func formatIPv4Address(data any) (string, error) {
249249

250250
func makeFuncInclude(t *template.Template) func(string, any) (string, error) {
251251
return func(name string, data any) (string, error) {
252-
var buf strings.Builder
252+
var buf bytes.Buffer
253253
err := t.ExecuteTemplate(&buf, name, data)
254-
return buf.String(), err
254+
return string(AdjustTemplateOutput(buf.Bytes())), err
255255
}
256256
}
257257

258258
func makeFuncTpl(t *template.Template) func(string, any) (string, error) {
259259
return func(text string, data any) (string, error) {
260-
var buf strings.Builder
260+
var buf bytes.Buffer
261261
_t, err := t.Clone()
262262
if err != nil {
263263
// Clone() should never produce an error
@@ -268,7 +268,7 @@ func makeFuncTpl(t *template.Template) func(string, any) (string, error) {
268268
return "", err
269269
}
270270
err = _t.Execute(&buf, data)
271-
return buf.String(), err
271+
return string(AdjustTemplateOutput(buf.Bytes())), err
272272
}
273273
}
274274

internal/templatex/util.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and component-operator-runtime contributors
3+
SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package templatex
7+
8+
import "bytes"
9+
10+
// This function mimics the Helm behaviour. Background: values passed to the built-in generators
11+
// are of type map[string]any. Of course, templates are rendered with the missingkey=zero option.
12+
// But still, if a key is missing in the values, the empty value of 'any' (returned in this case)
13+
// makes the go templating engine return '<no value>' in that case.
14+
// Helm decided to override that by replacing all occurrences of the string '<no value>' in any template output
15+
// by the empty string. We are following that approach.
16+
func AdjustTemplateOutput(data []byte) []byte {
17+
return bytes.ReplaceAll(data, []byte("<no value>"), []byte(""))
18+
}

pkg/manifests/kustomize/generator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func (g *KustomizeGenerator) Generate(ctx context.Context, namespace string, nam
242242
if err := t0.ExecuteTemplate(&buf, t.Name(), data); err != nil {
243243
return nil, err
244244
}
245-
if err := fsys.WriteFile(n, buf.Bytes()); err != nil {
245+
if err := fsys.WriteFile(n, templatex.AdjustTemplateOutput(buf.Bytes())); err != nil {
246246
return nil, err
247247
}
248248
}

pkg/manifests/transformer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (t *TemplateParameterTransformer) TransformParameters(namespace string, nam
6565
return nil, err
6666
}
6767
var transformedParameters types.UnstructurableMap
68-
if err := kyaml.Unmarshal(buf.Bytes(), &transformedParameters); err != nil {
68+
if err := kyaml.Unmarshal(templatex.AdjustTemplateOutput(buf.Bytes()), &transformedParameters); err != nil {
6969
return nil, err
7070
}
7171
return transformedParameters, nil

0 commit comments

Comments
 (0)