kuberneteskubernetes-helmconfigmap

Put yaml (as data) into a config map


Is there a way to store yaml data in a config map?

In my values.yaml I have something like the below

config:
  filters:
    - kind: Pod
      apiVersion: v1
...

In my config map, I'm currently doing

...
data:
  config.yaml: |-
    {{ .Values.config }}

But in the resulting configmap the data is "inlined" and formatted as this

...
data:
  config.yaml: >-
    map[filters:[map[apiVersion:v1...

Which isn't yaml and therefore can't be parsed by the app reading it.


Solution

  • Let's say you have a demo-chart:

    .
    └── demo
        ├── charts
        ├── Chart.yaml
        ├── templates
        │   ├── configmap.yaml
        │   ├── _helpers.tpl
        │   └── NOTES.txt
        └── values.yaml
    
    

    values.yaml:

    config:
      filters:
        - kind: Pod
          apiVersion: v1
    

    configmap.yaml:

    {{- if .Values.config }}
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: demo-name
    data:
      config.yml: |- {{- toYaml .Values.config | nindent 4 }}
    {{- end }}
    

    Explanation: toYaml parse the data loaded from the values.yaml into YAML and nindent put 4 spaces in front of every line.

    Check:

    $ helm template demo
    ---
    # Source: demo/templates/configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: demo-name
    data:
      config.yml: |-
        filters:
        - apiVersion: v1
          kind: Pod