Reputation: 41
I would like to be able from Helm template file as below to insert template with toJson helm function as explained in the documentation :
value: {{ include "mytpl" . | lower | quote }}
https://helm.sh/docs/howto/charts_tips_and_tricks/#know-your-template-functions
My configuration : _helper.tpl
{{- define "my_tpl" -}}
key1: value1
key2: value2
{{- end -}}
dep.yaml
template:
metadata:
annotations:
test: >-
{{ include "my_tpl" . | toJson }}
This should return
template:
metadata:
annotations:
test: >-
{"key1":"value1","key2":"value2"}
but it return
template:
metadata:
annotations:
test: >-
"key1:value1\nkey2:value2"
I'm using Helm v3. Anyone have an idea please ?
Upvotes: 4
Views: 21626
Reputation: 159040
A define
d template always produces a string; the Helm-specific include
function always returns a string.
In your example, you have a string that happens to be valid YAML. Helm has an undocumented fromYaml
function that converts the string to object form, and then you can serialize that again with toJson
.
{{ include "my_tpl" . | fromYaml | toJson }}
You may find it easier to have the template itself produce the correct JSON serialization. That could look something like
{{- define "my_tpl" -}}
{{- $dict := dict "key1" "value1" "key2" "value2" -}}
{{- toJson $dict -}}
{{- end -}}
{{ include "my_tpl" . }}
where the "key1"
, "value1"
, etc. can be any valid template expression (you do not need nested {{ ... }}
).
Upvotes: 11