Search code examples
kuberneteskubernetes-helmpodspec

Include a helm template in yaml file, removing quotes


This is a snippet from helpers.tpl of my helm chart:

{{/*
Pod-specific labels - added to pod template only
Adding a revision label to the pod will cause it to restart every time the chart is deployed.
*/}}
{{- define "app.podLabels" -}}
helm-revision: {{ .Release.Revision | quote }}
{{- end }}

Including it in pod labels like this:

  labels:
    {{- include "app.podLabels" . | nindent 8 }}

The result would be as shown below. The quotes around 1 is required because Kubernetes accepts string labels only.

  labels:
    helm-revision: "1"

I need to use the same template for an init container, replacing the : with = like this:

  args:
    - "pod"
    - "-l {{ include "app.podLabels" . | replace ": " "=" }}"

But the output would be an incorrect yaml:

  args:
    - "pod"
    - "-l helm-revision="1""

with error:

error converting YAML to JSON: yaml: line 34: did not find expected '-' indicator

What I actually want is something like this, that doesn't contain quotes around 1:

  args:
    - "pod"
    - "-l helm-revision=1"

How can I achieve this?


Solution

  • This can be achieved by replacing " with nothing ( In effect, removes "). Escaping the quotes using \ character would be needed.

    Also adding a replace function to remove newlines and add , as a separator between labels. This would be helpful if more than one pod-specific label is present. But make sure this will work in your case when rendering the template.

          args:
            - "pod"
            - "-l {{ include "app.podLabels" . | replace ": " "=" | replace "\n" ", " | replace "\"" "" }}"