helm configmap include client script in values.yaml

4/26/2019

I have to include the client script file as configmap and mount to pod how to create configmap for below structure in values.yaml

app:
  server:
    client-cli1.sh: |
      #!/bin/bash
      echo "Hello World"
    client-cli2.sh: |
      #!/bin/bash
      echo "Hello World"

this is configmap file

apiVersion: v1
kind: ConfigMap
metadata:
  name: cli-config
data: 
{{ range  $key, $val:= .Values.app.server }}
  {{ $key }}: |
    {{ $val  }}
{{ end }}

i am getting error "error converting YAML to JSON: yaml: line 14: could not find expected ':'" Note: cant change structure and cant use File function because the build happen somewhere else only values.ymal will be provided.

how to parse this.

-- user1184777
kubernetes
kubernetes-helm

2 Answers

4/26/2019

You can use helm template with the same -f and --set flags to see what the output of your template is. This is key to debugging this sort of problem, and you might try this before reading on to understand what's going wrong and if you've fixed it.

The YAML files that Kubernetes consumes are incredibly sensitive to indentation. Helm's Go text/template engine isn't especially aware of YAML structure. What you will likely see is that the echo on the second line of the script isn't indented at all, which breaks YAML's indentation rules.

There's two answers to this:

  1. Use the indent template function to indent the entire body of what you're injecting. Note that this includes the very first line, so you need to not indent the line in the YAML template.

    data: 
    {{ range  $key, $val:= .Values.app.server }}
      {{ $key }}: |
    {{ $val | indent 4 }}
    {{ end }}
  2. Helm has several undocumented extensions. In particular you can use toJson to convert the string to a JSON string, including details like escaping the newlines. YAML 1.2 is intended to be a superset of JSON, so this should work:

    data: 
    {{ range  $key, $val:= .Values.app.server }}
      {{ $key }}: {{ $val | toJson }}
    {{ end }}

    For that matter, you can use toYaml to make Helm do the hard work for you:

    data:
    {{ .Values.app.server | toYaml | indent 2 }}

Check with helm template to make sure these are outputting what you expect. The first option will produce a more readable ConfigMap YAML; the second a more concise template; but both should be equivalent.

-- David Maze
Source: StackOverflow

4/26/2019

Try this:

apiVersion: v1
kind: ConfigMap
metadata:
  name: cli-config
data: 
{{ toYaml .Values.app.server | indent 2 }}
-- Vasily Angapov
Source: StackOverflow