Using a multi-line shell script as a helm value

4/28/2020

I have a helm 3 template with the following yaml which works perfectly fine. It has a multi-line shell script as part of the initContainers and it works as needed.

  initContainers:
  - name: check-crd
    image: 'bitnami/kubectl:1.12'
    env:
      - name: CRDs
        value: {{ .Values.CRDs.list }}
    command:
    - "/bin/bash"
    - "-c"
    - |
      set -x
      for i in $(echo $CRDs | tr ":" "\n")
      do 
        kubectl get -o json $i
      done

However, when I try to move the shell script into a helm value I get an error. The new yaml and the values:

  initContainers:
  - name: check-crd
    image: 'bitnami/kubectl:1.12'
    env:
      - name: CRDs
        value: {{ .Values.CRDs.list }}
    command:
    - "/bin/bash"
    - "-c"
    - {{ .Values.CRDs.script }}

the values.yaml file is:

CRDs:
  list: 'mycrd1s.example.com:mycrd2s.example.com'
  script: |
          set -x
          for i in $(echo $CRDs | tr ":" "\n")
          do 
            kubectl get -o json $i
          done

When I try to helm3 install this, I get an error:

 error converting YAML to JSON: yaml: line 30: could not find expected ':'

I understand that this is some kind of YAML multi-line string issue but could not understand how to fix this. I have tried with various combinations of {{, {{-, toYaml etc. but could not resolve this issue. Any way to make use of a multi-line shell script as a helm value ?

PS: I understand that I can create a ConfigMap with the shell script as the data and achieve the same, but I want everything to be done just via the yaml files, because I cannot create a configmap for some unavoidable reason.

-- Sankar
kubernetes
kubernetes-helm
yaml

1 Answer

4/28/2020

You need to add nindent, like following

        initContainers:
        - name: check-crd
          image: 'bitnami/kubectl:1.12'
          env:
          - name: CRDs
            value: {{ .Values.CRDs.list }}
          command:
          - "/bin/bash"
          - "-c"
          - {{- toYaml .Values.CRDs.script | nindent 12 }}
-- hoque
Source: StackOverflow