Looping over unquoted config values

3/16/2019

I'm trying to create a config map from a list of values by doing the following

{{- if index .Values "environmentVariables" }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "some-env.fullname" . }}
data:
{{- range $key, $value := .Values.environmentVariables }}
  {{ $key }}: {{ $value }}
{{- end }}
{{- end }}

With the below values

environmentVariables:
  SERVER_CONTEXT_PATH: /some/where/v2
  SERVER_PORT: 8080

But that results in the following error message

Error: release my-chart-env-v2-some-env-test failed: ConfigMap in version "v1" cannot be handled as a ConfigMap: v1.ConfigMap: Data: ReadString: expects " or n, parsing 106 ...ER_PORT":8... at {"apiVersion":"v1","data":{"SERVER_CONTEXT_PATH":"/dokument-redskaber/my-chart-app/v2","SERVER_PORT":8080},"kind":"ConfigMap","metadata":{"labels":{"app.kubernetes.io/instance":"my-chart-env-v2-some-env-test","app.kubernetes.io/managed-by":"Tiller","app.kubernetes.io/name":"some-env","helm.sh/chart":"some-env-0.1.0"},"name":"my-chart-env-v2","namespace":"some-env-test"}}

If I do

  {{ $key }}: {{ $value | quote }}

it works. But I don't (think I) want to quote all my values. And simply quoting my input value doesn't work. Any suggestions?

-- user672009
go-templates
kubernetes
kubernetes-helm
yaml

1 Answer

4/4/2019

ConfigMap's object data feild requires for all values to be strings. When we have value like 8080 it reads as int.

What we can do to build ConfigMap object here:

1. Define all values as strings using quote function:

values.yaml:

environmentVariables:
  SERVER_CONTEXT_PATH: /some/where/v2
  SERVER_PORT: 8080

part of templates/configmap.yaml:

data:
{{- range $key, $value := .Values.environmentVariables }}
  {{ $key }}: {{ $value | quote }}
{{- end }}

2. Define all values as strings using double quotes:

values.yaml:

environmentVariables:
  SERVER_CONTEXT_PATH: /some/where/v2
  SERVER_PORT: 8080

part of templates/configmap.yaml:

data:
{{- range $key, $value := .Values.environmentVariables }}
  {{ $key }}: "{{ $value }}"
{{- end }}

3. Define all int values as string values in values.yaml:

values.yaml:

environmentVariables:
  SERVER_CONTEXT_PATH: /some/where/v2
  SERVER_PORT: '"8080"'

part of templates/configmap.yaml:

data:
{{- range $key, $value := .Values.environmentVariables }}
  {{ $key }}: {{ $value }}
{{- end }}

Here is some trick: when value is taken, it's basically taken as "8080", which can be set exactly as a string value.

-- nickgryg
Source: StackOverflow