<p><em>We use helm charts to deploy to kubernetes and helm to generate those charts. Unfortunately I'm not familiar with helm or helm templates (and only moderately familiar with kubernetes) so in asking the question below I may use incorrect terminology (in fact I may have done already in this paragraph) so please bear with me as I get up to speed.</em></p>
<p>I have a helm template, foo.yaml, that resembles the following:</p>
<pre class="lang-yaml prettyprint-override"><code>{{- define &quot;env.variables&quot; }}
  echo &quot;--- Setting env variables ---&quot;

  export foo={{ .Values.global_vars.foo }}
{{- end }}
apiVersion: v1
kind: Secret
type: Opaque
metadata:
  name: &quot;{{ .Values.global_vars.bar }}-set-env-vars&quot;
data:
  set-env-vars.sh: {{ include &quot;env.variables&quot; . | b64enc }}
</code></pre>
<p>As you can see it defines a script that creates some environment variables. I am working on a requirement to only create those variables where some condition is true (<code>.Values.global_vars.baz == 1</code>)</p>
<p>I suppose I could achieve this by doing something like:</p>
<pre class="lang-yaml prettyprint-override"><code>{{- define &quot;env.variables&quot; }}
  if [ {{ .Values.global_vars.baz }} = 1 ]
  then
    echo &quot;--- Setting env variables ---&quot;

    export foo={{ .Values.global_vars.foo }}
  fi
{{- end }}
apiVersion: v1
kind: Secret
type: Opaque
metadata:
  name: &quot;{{ .Values.global_vars.bar }}-set-env-vars&quot;
data:
  set-env-vars.sh: {{ include &quot;env.variables&quot; . | b64enc }}
</code></pre>
<p>but that doesn't feel like a very elegant way of doing it. Can I put a conditional expression into <code>data</code> that only includes the script where the condition is met. Something like this:</p>
<pre class="lang-yaml prettyprint-override"><code>{{- define &quot;env.variables&quot; }}
  echo &quot;--- Setting env variables ---&quot;

  export foo={{ .Values.global_vars.foo }}
{{- end }}
{{- define &quot;no.env.variables&quot; }}
  echo &quot;--- No env variables to set ---&quot;
{{- end }}
apiVersion: v1
kind: Secret
type: Opaque
metadata:
  name: &quot;{{ .Values.global_vars.bar }}-set-env-vars&quot;
data:
  set-env-vars.sh: {{ if {{ .Values.global_vars.bar }} = 1 then include &quot;env.variables&quot; else include &quot;no.env.variables&quot; . | b64enc }}
</code></pre>
<p>Hope that makes sense. This is literally my first ever excursion into helm so am looking for some noob guidance.</p>

jamiet
<p>The fancy <code>{{ }}</code> things in the helm files are <a href="https://golang.org/pkg/text/template/" rel="nofollow noreferrer">GO templates</a>. They have several built-in features, one of them are control structures, like if-else. They allow you to render part of the template conditionally.</p>
<p>You can add a condition to the <code>env.variables</code> <a href="https://helm.sh/docs/chart_template_guide/named_templates/" rel="nofollow noreferrer">named template</a>:</p>
<pre class="lang-yaml prettyprint-override"><code>{{ define &quot;env.variables&quot; }}
  {{ if eq .Values.global_vars.baz &quot;1&quot; }}
  echo &quot;--- Setting env variables ---&quot;

  export foo={{ .Values.global_vars.foo }}
  {{ else }}
  echo &quot;--- No env variables to set ---&quot;
  {{ end }}
{{ end }}
data:
  set-env-vars.sh: {{ include &quot;env.variables&quot; . | b64enc }}
</code></pre>
<p>or to the <code>set-env-vars.sh</code> value:</p>
<pre class="lang-yaml prettyprint-override"><code>{{- define &quot;env.variables&quot; }}
  echo &quot;--- Setting env variables ---&quot;

  export foo={{ .Values.global_vars.foo }}
{{- end }}
{{- define &quot;no.env.variables&quot; }}
  echo &quot;--- No env variables to set ---&quot;
{{- end }}
data:
  set-env-vars.sh: {{ if eq .Values.global_vars.baz &quot;1&quot; }} 
    {{- include &quot;env.variables&quot; . | b64enc }}
  {{- else }}
    {{- include &quot;env.variables&quot; . | b64enc }}
  {{- end }}
</code></pre>
<p>The effect would be the same.</p>
<p>One confusing thing may be the <code>if eq .Values.global_vars.baz &quot;1&quot;</code> syntax. The <code>eq</code> is a function, and the next values are its arguments (GO templates do not have equals operator).</p>
