QuirkyBit
QuirkyBit

Reputation: 772

Kubernetes Helm define multiline init container command

I’m trying to organize my charts and migrate all the initContainers into a helper named chart _scripts.tlp.

I have an initContainers step in proxy-deployment.yaml that is waiting until my broker service initializes like so:

      # 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:
          - >-
            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;

I am trying to rewrite it like so:

      # 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:
          - >-
            {{ template  "service.waitBrokerReady"}}

I have the following names template _scripts.tlp:

{{/*
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 }}

Unfortunately, the fact that this is a multiline command and there are spaces demarcations, I see error converting YAML to JSON: yaml.

I have tried several variations with printf, but not sure how to pass nested templates and often see Error: parse error at (service/templates/_functions.tpl:6): "-p" in command.

Upvotes: 1

Views: 1675

Answers (1)

QuirkyBit
QuirkyBit

Reputation: 772

The following solution seems to be working for me:

_scripts.tlp:

{{/*
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 -}}

proxy-deployment.yaml:

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

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

` {{- include "pulsar.waitBrokerReady" . | squote | indent 10 | replace "'" "" }}

Once, it does template, you can remove the squote and replace.

Upvotes: 2

Related Questions