Skip to content

Commit

Permalink
Switch helm values delim to ${ }
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario Manno committed Dec 6, 2022
1 parent a19c93f commit dcab6f0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
4 changes: 2 additions & 2 deletions pkg/target/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ func processTemplateValues(helmValues map[string]interface{}, templateContext ma
return nil, fmt.Errorf("failed to unmarshal helm values template: %w", err)
}

tmpl := template.New("values").Funcs(tplFuncMap()).Option("missingkey=error")
tmpl := template.New("values").Funcs(tplFuncMap()).Option("missingkey=error").Delims("${", "}")
tmpl, err = tmpl.Parse(string(data))
if err != nil {
return nil, fmt.Errorf("failed to parse helm values template: %w", err)
Expand All @@ -624,7 +624,7 @@ func processTemplateValues(helmValues map[string]interface{}, templateContext ma
}

var renderedValues map[string]interface{}
err = yaml.Unmarshal(b.Bytes(), &renderedValues)
err = kyaml.Unmarshal(b.Bytes(), &renderedValues)
if err != nil {
return nil, fmt.Errorf("failed to interpret rendered template as helm values: %#v, %v", renderedValues, err)
}
Expand Down
49 changes: 27 additions & 22 deletions pkg/target/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,23 +110,28 @@ const bundleYamlWithTemplate = `namespace: default
helm:
releaseName: labels
values:
clusterName: "{{ .ClusterLabels.name }}"
fromAnnotation: "{{ .ClusterAnnotations.testAnnotation }}"
clusterNamespace: "{{ .ClusterNamespace }}"
fleetClusterName: "{{ .ClusterName }}"
reallyLongClusterName: kubernets.io/cluster/{{ index .ClusterLabels "really-long-label-name-with-many-many-characters-in-it" }}
clusterName: "${ .ClusterLabels.name }"
fromAnnotation: "${ .ClusterAnnotations.testAnnotation }"
clusterNamespace: "${ .ClusterNamespace }"
fleetClusterName: "${ .ClusterName }"
reallyLongClusterName: kubernets.io/cluster/${ index .ClusterLabels "really-long-label-name-with-many-many-characters-in-it" }
missingLabel: |-
{{ if hasKey .ClusterLabels "missing" }}{{ .ClusterLabels.missing }}{{ else }}missing{{ end}}
#list: {{ list 1 2 3 }}
${ if hasKey .ClusterLabels "missing" }${ .ClusterLabels.missing }${ else }missing${ end}
list: ${ list 1 2 3 | toJson }
listb: |-
${- range $key, $val := .ClusterLabels }
- name: ${ $key }
value: ${ $val | quote }
${- end}
customStruct:
- name: "{{ .ClusterValues.topLevel }}"
- name: "${ .ClusterValues.topLevel }"
key1: value1
key2: value2
- element2: "{{ .ClusterValues.nested.secondTier.thirdTier }}"
- "element3_{{ .ClusterLabels.envType }}": "{{ .ClusterLabels.name }}"
- element2: "${ .ClusterValues.nested.secondTier.thirdTier }"
- "element3_${ .ClusterLabels.envType }": "${ .ClusterLabels.name }"
funcs:
upper: "{{ .ClusterValues.topLevel | upper }}_test"
join: '{{ .ClusterValues.list | join "," }}'
upper: "${ .ClusterValues.topLevel | upper }_test"
join: '${ .ClusterValues.list | join "," }'
diff:
comparePatches:
- apiVersion: networking.k8s.io/v1
Expand Down Expand Up @@ -341,10 +346,10 @@ helm:
disablePreprocess: true
releaseName: labels
values:
clusterName: "{{ .ClusterName }}"
clusterContext: "{{ .Values.someKey }}"
templateFn: '{{ index .ClusterLabels "testLabel" }}'
syntaxError: "{{ non_existent_function }}"
clusterName: "${ .ClusterName }"
clusterContext: "${ .Values.someKey }"
templateFn: '${ index .ClusterLabels "testLabel" }'
syntaxError: "${ non_existent_function }"
`

func TestDisablePreProcessFlagEnabled(t *testing.T) {
Expand All @@ -366,19 +371,19 @@ func TestDisablePreProcessFlagEnabled(t *testing.T) {
}{
{
Key: "clusterName",
ExpectedValue: "{{ .ClusterName }}",
ExpectedValue: "${ .ClusterName }",
},
{
Key: "clusterContext",
ExpectedValue: "{{ .Values.someKey }}",
ExpectedValue: "${ .Values.someKey }",
},
{
Key: "templateFn",
ExpectedValue: "{{ index .ClusterLabels \"testLabel\" }}",
ExpectedValue: "${ index .ClusterLabels \"testLabel\" }",
},
{
Key: "syntaxError",
ExpectedValue: "{{ non_existent_function }}",
ExpectedValue: "${ non_existent_function }",
},
} {
if field, ok := valuesObj[testCase.Key]; !ok {
Expand All @@ -398,7 +403,7 @@ helm:
disablePreprocess: false
releaseName: labels
values:
clusterName: "{{ .ClusterName }}"
clusterName: "${ .ClusterName }"
`

func TestDisablePreProcessFlagDisabled(t *testing.T) {
Expand Down Expand Up @@ -431,7 +436,7 @@ const bundleYamlWithDisablePreProcessMissing = `namespace: default
helm:
releaseName: labels
values:
clusterName: "{{ .ClusterName }}"
clusterName: "${ .ClusterName }"
`

func TestDisablePreProcessFlagMissing(t *testing.T) {
Expand Down

0 comments on commit dcab6f0

Please sign in to comment.