title: Kubernetes Helm define multiline init container command
body: <p>I’m trying to organize my charts and migrate all the <code>initContainers</code> into a helper named chart <strong>_scripts.tlp</strong>.</p>
  
  <p>I have an <code>initContainers</code> step in <strong>proxy-deployment.yaml</strong> that is waiting until my broker service initializes like so:</p>
  
  <pre class="lang-sh prettyprint-override"><code>      # This init container will wait for at least one broker to be ready before proceeding
        # with deploying the rest of the proxy service
        - name: wait-broker-ready
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: ["bash", "-c"]
          args:
            - &gt;-
              for i in {0..10}; do
                broker_pods="$(nslookup -timeout=10 {{ template "service.fullname" . }}-{{ .Values.broker.component }} | grep Name | wc -l)"
                if [[ ${broker_pods} -ge 1 ]]; then
                  break
                fi
                sleep 30;
              done;
  </code></pre>
  
  <p>I am trying to rewrite it like so:</p>
  
  <pre class="lang-sh prettyprint-override"><code>      # This init container will wait for at least one broker to be ready before proceeding
        # with deploying the rest of the proxy service
        - name: wait-broker-ready
          image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
          imagePullPolicy: {{ .Values.image.pullPolicy }}
          command: ["bash", "-c"]
          args:
            - &gt;-
              {{ template  "service.waitBrokerReady"}}
  </code></pre>
  
  <p>I have the following names template <strong>_scripts.tlp</strong>:</p>
  
  <pre class="lang-sh prettyprint-override"><code>{{/*
  Wait until at least one broker instances is initialized
  */}}
  {{ define "service.waitBrokerReady" }}
  for i in {0..10}; do
    broker_pods="$(nslookup -timeout=10 {{ template "service.fullname" . }}-{{ .Values.broker.component }} | grep Name | wc -l)"
    if [[ ${broker_pods} -ge 1 ]]; then
      break
    fi
    sleep 30;
  done;
  {{- end }}
  </code></pre>
  
  <p>Unfortunately, the fact that this is a multiline command and there are spaces demarcations, I see <code>error converting YAML to JSON: yaml</code>. </p>
  
  <p>I have tried several variations with <code>printf</code>, but not sure how to pass nested templates and often see <code>Error: parse error at (service/templates/_functions.tpl:6): "-p" in command</code>.</p>