<p>You have couple options two that come to my mind:</p>
<ul>
<li>use variable</li>
<li>use helper function</li>
</ul>
<h3>Variable</h3>
<p>On top of the file declare a variable and then just use it:</p>
<pre><code>{{- $prefix := (or (eq .Release.Namespace &quot;prod&quot;) (eq .Release.Namespace &quot;prod-phi&quot;) | ternary &quot;prod&quot; &quot;dev&quot;) -}}
...
spec:
  data:
    - secretKey: RdsDataUsername
      remoteRef:
        key: {{ $prefix }}/rds/data/primary
</code></pre>
<h3>Helper function</h3>
<p>make a file <code>_helpers.tpl</code></p>
<pre><code>{{/*
Environment prefix.
*/}}
{{- define &quot;CHART-NAME.prefix&quot; -}}
{{- (or (eq .Release.Namespace &quot;prod&quot;) (eq .Release.Namespace &quot;prod-phi&quot;) | ternary &quot;prod&quot; &quot;dev&quot;) -}}
{{- end -}}
</code></pre>
<p>and use it:</p>
<pre><code>spec:
  data:
    - secretKey: RdsDataUsername
      remoteRef:
        key: {{ include &quot;CHART-NAME.prefix&quot; . }}/rds/data/primary
</code></pre>
<hr />
<blockquote>
<p><strong>Note</strong>: If you are using the prefix in multiple files, you can use both &quot;methods&quot; in combination.</p>
<pre><code>{{- $prefix := include &quot;CHART-NAME.prefix&quot; . -}}
... 
spec:
  data:
    - secretKey: RdsDataUsername
      remoteRef:
        key: {{ $prefix }}/rds/data/primary
</code></pre>
</blockquote>

CC BY-SA 4.0
2023-06-24T14:51:51.083Z
2023-06-24T14:51:51.083Z
Kyslik
<p>I am writing a helm template for external secrets.</p>
<p>The secrets are in AWS secrets manager, and they're prefixed with &quot;dev&quot; or &quot;prod&quot;.</p>
<p>Namespaces are used to differentiate environments, but there are many more than &quot;dev&quot; or &quot;prod&quot; -- &quot;prod&quot; and &quot;prod-phi&quot; should use &quot;prod&quot; secrets, while everything else should use &quot;dev&quot; secrets.</p>
<p>To accomplish that, I have a pretty long expression, which you can see prefixing a secret name here:</p>
<pre class="lang-yaml prettyprint-override"><code>    - secretKey: RdsDataUsername
      remoteRef:
        key: {{ or (eq .Release.Namespace &quot;prod&quot;) (eq .Release.Namespace &quot;prod-phi&quot;) | ternary &quot;prod&quot; &quot;dev&quot; }}/rds/data/primary
        property: username
</code></pre>
<p>I am repeating that expression &quot;<code>{{ or (eq .Release.Namespace &quot;prod&quot;) (eq .Release.Namespace &quot;prod-phi&quot;) | ternary &quot;prod&quot; &quot;dev&quot; }}</code>&quot; many times throughout this file, and it's affecting readability.</p>
<p>Is there a way to set a variable one time in the file using this expression, then reference that instead?</p>
<p>Something like:</p>
<pre class="lang-yaml prettyprint-override"><code>variables:
  prefix: {{ or (eq .Release.Namespace &quot;prod&quot;) (eq .Release.Namespace &quot;prod-phi&quot;) | ternary &quot;prod&quot; &quot;dev&quot; }}

spec:
  data:
    - secretKey: RdsDataUsername
      remoteRef:
        key: {{ $prefix }}/rds/data/primary
        property: username
</code></pre>

<kubernetes><helm><kubernetes-helm>
Shorten a reused expression in a helm template
amacrobert
# Shorten a reused expression in a helm template

(Asked by: username_0 on 2023-05-16)

I am writing a helm template for external secrets.
The secrets are in AWS secrets manager, and they're prefixed with "dev" or "prod".
Namespaces are used to differentiate environments, but there are many more than "dev" or "prod" -- "prod" and "prod-phi" should use "prod" secrets, while everything else should use "dev" secrets.
To accomplish that, I have a pretty long expression, which you can see prefixing a secret name here:
    - secretKey: RdsDataUsername
      remoteRef:
        key: {{ or (eq .Release.Namespace "prod") (eq .Release.Namespace "prod-phi") | ternary "prod" "dev" }}/rds/data/primary
        property: username

I am repeating that expression "{{ or (eq .Release.Namespace "prod") (eq .Release.Namespace "prod-phi") | ternary "prod" "dev" }}" many times throughout this file, and it's affecting readability.
Is there a way to set a variable one time in the file using this expression, then reference that instead?
Something like:
variables:
  prefix: {{ or (eq .Release.Namespace "prod") (eq .Release.Namespace "prod-phi") | ternary "prod" "dev" }}

spec:
  data:
    - secretKey: RdsDataUsername
      remoteRef:
        key: {{ $prefix }}/rds/data/primary
        property: username

**Tags:** kubernetes, helm, kubernetes-helm


---

## 1 answers

### Answer by username_1 on 2023-06-24. Score: 1 | Accepted answer

You have couple options two that come to my mind:

use variable
use helper function

Variable
On top of the file declare a variable and then just use it:
{{- $prefix := (or (eq .Release.Namespace "prod") (eq .Release.Namespace "prod-phi") | ternary "prod" "dev") -}}
...
spec:
  data:
    - secretKey: RdsDataUsername
      remoteRef:
        key: {{ $prefix }}/rds/data/primary

Helper function
make a file _helpers.tpl
{{/*
Environment prefix.
*/}}
{{- define "CHART-NAME.prefix" -}}
{{- (or (eq .Release.Namespace "prod") (eq .Release.Namespace "prod-phi") | ternary "prod" "dev") -}}
{{- end -}}

and use it:
spec:
  data:
    - secretKey: RdsDataUsername
      remoteRef:
        key: {{ include "CHART-NAME.prefix" . }}/rds/data/primary



Note: If you are using the prefix in multiple files, you can use both "methods" in combination.
{{- $prefix := include "CHART-NAME.prefix" . -}}
... 
spec:
  data:
    - secretKey: RdsDataUsername
      remoteRef:
        key: {{ $prefix }}/rds/data/primary


---

