<p>I need to create template for secret value.</p>
<p>This is the secret file:</p>
<pre><code>apiVersion: v1
kind: Secret
metadata:
  name: secret
type: Opaque
stringData:
  &quot;user&quot;:  &quot;user&quot;
  &quot;password&quot;:  &quot;password&quot;
</code></pre>
<p>And this is what I have created in the _helpers.tpl</p>
<pre><code>{{/*
  User
*/}}
{{- define &quot;@@chartName@@.user&quot; -}}
{{- $secret := lookup &quot;v1&quot; &quot;Secret&quot; .Release.Namespace &quot;secret&quot; -}}
  {{- if $secret -}}
     {{- print $secret.stringData.user}}
  {{- else -}}
     {{- fail &quot;The secret is absent&quot; -}}
  {{- end -}}
{{- end -}}
</code></pre>
<p>But it doesn't work and I get the error :  &lt;$secret.stringData.user&gt;: nil pointer evaluating interface {}.user</p>
<p>I have created the secret before installation helm chart and I don't know what is the reason of this error.  Thanks for any help!</p>

Java
<p>You will get that error if <code>$secret</code> exists, but doesn't contain <code>stringData</code>.</p>
<p>At a Helm level, you can work around this by using the <code>default</code> function to force it to exist (&quot;doesn't have <code>stringData</code>&quot; and &quot;doesn't have <code>user</code>&quot; are approximately the same error):</p>
<pre class="lang-none prettyprint-override"><code>{{- $secret := lookup &quot;v1&quot; &quot;Secret&quot; .Release.Namespace &quot;secret&quot; | default dict -}}
{{- $stringData := $secret.stringData | default dict -}}
{{- $stringData.user | required &quot;The secret is absent&quot; -}}
</code></pre>
<p>At a higher-level, if it's possible to read back the Secret, it probably only has the base64-encoded <code>data</code> field (even if it was created with <code>stringData</code>).  Helm provides a <a href="https://docs.helm.sh/docs/chart_template_guide/function_list/#encoding-functions" rel="nofollow noreferrer"><code>b64dec</code> function</a> that could decode it.  It'd be better to <a href="https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets-as-environment-variables" rel="nofollow noreferrer">refer to the secret as an environment variable</a> in your pod spec, or pass the value directly into Helm.</p>
<pre class="lang-yaml prettyprint-override"><code># in a Pod spec; without using Helm `lookup`
env:
  - name: USER
    valueFrom:
      secretKeyRef:
        name: secret
        key: user
</code></pre>
<pre class="lang-none prettyprint-override"><code># or passed directly via Helm
{{- $credentials := .Values.credentials | default dict -}}
{{- $credentials.user | required &quot;missing user.credentials&quot; -}}
</code></pre>
