<p>I have the following snippet in my Helm deployment yaml file:</p>

<pre><code>{{if or .Values.ha.enabled .Values.checkpointing.enable_checkpointing .Values.enable_upgrade_hook}}
{{if eq .Values.pvc.file_prefix "file://"}}
- mountPath: {{ .Values.pvc.shared_storage_path }}/{{ template "fullname" . }}
  name: shared-pvc
{{end}}
{{end}}
</code></pre>

<p>I would like to put all these <em>if</em> checks into a custom function and just call the function here. My new snippet using the function should look like this:</p>

<pre><code>{{if eq enable_mount_volume "true"}}
- mountPath: {{ .Values.pvc.shared_storage_path }}/{{ template "fullname" . }}
  name: shared-pvc
{{end}}
</code></pre>

<p>How would I achieve this? I may have multiple deployment yaml files, each doing this conditional check, and it would be useful to just call a function instead of  putting the logic-heavy if check in each yaml file(Just to make it less error-prone).</p>

<p>Also, I wouldn't want to define this function in every single template file, as that would defeat the purpose.</p>

<p>You can create a <a href="https://helm.sh/docs/howto/charts_tips_and_tricks/#using-partials-and-template-includes" rel="nofollow noreferrer" title="partial template">partial template</a> named <em>conditional-mount</em> in a file starting with underscore, for example, <code>templates/_conditional-mount.tpl</code>:</p>
<pre><code>{{define &quot;conditional-mount&quot;}}
{{if or .Values.ha.enabled .Values.checkpointing.enable_checkpointing .Values.enable_upgrade_hook}}
{{if eq .thisPvc.file_prefix &quot;file://&quot;}}
- mountPath: {{ .thisPvc.shared_storage_path }}/{{ template &quot;fullname&quot; . }}
  name: shared-pvc
{{end}}
{{end}}
{{end}}
</code></pre>
<p>And then use it you anywhere you need via:</p>
<pre><code>{{include &quot;conditional-mount&quot; (dict &quot;Values&quot; .Values &quot;thisPvc&quot; .Values.pvc)}}
</code></pre>
<p>The trick here is that you specify a pvc to be mounted via the scope object <strong>thisPvc</strong> pointing to the <strong>.Values.pvc</strong>. The <a href="https://github.com/Masterminds/sprig/blob/master/docs/dicts.md" rel="nofollow noreferrer" title="Sprig dict function">Sprig dict function</a> is used.
You can then call it for different PVC, for example, <code>.Values.pvcXYZ</code>:</p>
<pre><code>{{include &quot;conditional-mount&quot; (dict &quot;Values&quot; .Values &quot;thisPvc&quot; .Values.pvcXYZ)}}
</code></pre>
