<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>

QuirkyBit
<p>The following solution seems to be working for me:</p>

<p><strong>_scripts.tlp</strong>:</p>

<pre class="lang-sh prettyprint-override"><code>{{/*
Wait until at least one broker instances is initialized
*/}}
{{- define "service.waitBrokerReady" }}
- &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;
{{- end -}}
</code></pre>

<p><strong>proxy-deployment.yaml</strong>:</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:
            {{- include "pulsar.waitBrokerReady" . | indent 10 }}
</code></pre>

<p>At first, I wasn't able to figure out why the include didn't work, so I used the combination of <code>squote</code> command to figure out the alignment and then you can add the <code>replace</code> to check if the template actually would work:</p>

<p>`            {{- include "pulsar.waitBrokerReady" . | squote | indent 10 | replace "'" "" }}</p>

<p>Once, it does template, you can remove the <code>squote</code> and <code>replace</code>.</p>
