Assign list to a key within a chart

2/8/2019

Deployment.yaml

...
env: {{ .Values.env}}
...

Values.yaml:

env:
- name: "DELFI_DB_USER"
  value: "yyy"
- name: "DELFI_DB_PASSWORD"
  value: "xxx"
- name: "DELFI_DB_CLASS"
  value: "com.mysql.jdbc.Driver"
- name: "DELFI_DB_URL"
  value: "jdbc:sqlserver://dockersqlserver:1433;databaseName=ddbeta;sendStringParametersAsUnicode=false"

feels like I'm missing something obvious.
linter says: ok
template says:

env: [map[name:DELFI_DB_USER value:yyy] map[name:DELFI_DB_PASSWORD value:xxx] map[name:DELFI_DB_CLASS value:com.mysql.jdbc.Driver] map[value:jdbc:mysql://dockersqlserver.{{ .Release.Namespace }}.svc.cluster.local:3306/ddbeta\?\&\;useSSL=true\&\;requireSSL=false name:DELFI_DB_URL]]

upgrade says:

Error: UPGRADE FAILED: YAML parse error on xxx/templates/deployment.yaml: error converting YAML to JSON: yaml: line 35: found unexpected ':'

solution:

env:
{{- range .Values.env }}
- name: {{ .name | quote }}
  value: {{ .value | quote }}
{{- end }}
-- 4c74356b41
kubernetes
kubernetes-helm
templates

2 Answers

2/8/2019

The current Go template expansion will give output which is not YAML:

env: {{ .Values.env}}

becomes:

env: env: [Some Go type stuff that isn't YAML]...

The Helm Go template needs to loop over the keys of the source YAML dictionary. This is described in the Helm docs.

The correct Deployment.yaml is:

...
env:
{{- range .Values.env }}
  - name: {{ .name | quote }}
    value: {{ .value | quote }}
{{- end }}
...
-- Paul Annetts
Source: StackOverflow

2/9/2019

Helm includes undocumented toYaml and toJson template functions; either will work here (because valid JSON is valid YAML). A shorter path could be

env: {{- .Values.env | toYaml | nindent 2 }}

Note that you need to be a little careful with the indentation, particularly if you're setting any additional environment variables that aren't in that list. In this example I've asked Helm to indent the YAML list two steps more, so additional environment values need to follow that too

env: {{- .Values.env | toYaml | nindent 2 }}
  - name: OTHER_SERVICE_URL
    value: "http://other-service.default.svc.cluster.local"
-- David Maze
Source: StackOverflow