Including shell script in ConfigMap using Helm ends in YAML errors

mit*_*man 3 kubernetes kubernetes-helm

I want to mount a shell script template into a container.

I have the following configmap.yaml:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "myservice-chart.fullname" . }}--scripts-configmap
  labels:
    app: {{ template "myservice-chart.name" . }}
    chart: {{ template "myservice-chart.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
data:
  setup: |
    {{ include "setup" . | indent 4 }}
Run Code Online (Sandbox Code Playgroud)

And this is my setup.tpl:

{{- define "setup" }}
#!/bin/bash
echo "Hello world!"
{{- end }}
Run Code Online (Sandbox Code Playgroud)

When I do a Helm dry-run, Helm generates this (valid) YAML:

...
apiVersion: v1
kind: ConfigMap
metadata:
  name: listening-donkey-myservice-chart-quorum-scripts-configmap
  labels:
    app: myservice-chart
    chart: myservice-chart-0.1.0
    release: listening-donkey
    heritage: Tiller
data:
  setup: |

    #!/bin/bash
    echo "Hello world!"
...
Run Code Online (Sandbox Code Playgroud)

When I run it without --dry-run, it generates this error:

configmap.yaml: error converting YAML to JSON: yaml: line 13: did not find expected key

Nic*_*lay 6

根据掌舵图模板指南

模板声明的花括号语法可以用特殊字符修改,以告诉模板引擎截取空格。{{-(加上破折号和空格)表示应该向左压缩空白,而 -}} 表示应该消耗右边的空白。当心!换行符是空格!

因此,为了防止下面出现无用的空行setup: |configmap.yaml应该是以下内容:

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "myservice-chart.fullname" . }}--scripts-configmap
  labels:
    app: {{ template "myservice-chart.name" . }}
    chart: {{ template "myservice-chart.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
data:
  setup: |
    {{- include "setup" . | indent 4 }}
Run Code Online (Sandbox Code Playgroud)