<p>I am trying to dynamic lookup available namespaces and able to create secrets in the namespaces using below  helm chart.</p>
<blockquote>
<p>templates/secrets.yaml</p>
</blockquote>
<pre><code>{{ range $index, $namespace := (lookup &quot;v1&quot; &quot;Namespace&quot; &quot;&quot; &quot;&quot;).items }}
apiVersion: v1
kind: Secret
metadata:
  name: myregcred
  namespace: {{ $namespace.metadata.name }}
type: kubernetes.io/dockerconfigjson
data:
  .dockerconfigjson: {{ template &quot;imagePullSecret&quot; . }}
{{- end}}
</code></pre>
<blockquote>
<p>values.yaml</p>
</blockquote>
<pre><code>imageCredentials:
  registry: quay.io
  username: someone
  password: sillyness
  email: someone@host.com
</code></pre>
<blockquote>
<p>_helpers.tpl</p>
</blockquote>
<pre><code>{{- define &quot;imagePullSecret&quot; }}
{{- with .Values.imageCredentials }}
{{- printf &quot;{\&quot;auths\&quot;:{\&quot;%s\&quot;:{\&quot;username\&quot;:\&quot;%s\&quot;,\&quot;password\&quot;:\&quot;%s\&quot;,\&quot;email\&quot;:\&quot;%s\&quot;,\&quot;auth\&quot;:\&quot;%s\&quot;}}}&quot; .registry .username .password .email (printf &quot;%s:%s&quot; .username .password | b64enc) | b64enc }}
{{- end }}
{{- end }}
</code></pre>
<p>When  i run this helm chart, i get below error</p>
<blockquote>
<p>Error: INSTALLATION FAILED: template: secrets/templates/_helpers.tpl:2:16: executing &quot;imagePullSecret&quot; at &lt;.Values.imageCredentials&gt;: nil pointer evaluating interface {}.imageCredentials</p>
</blockquote>
<p>I dont know what I am doing wrong here.</p>

Ade
<p>When you reference the named template &quot;imagePullSecret&quot; inside the range, the context &quot;.&quot; you are providing refers to the body of the loop, which does not have the &quot;Values&quot; attribute.</p>
<p>Try providing the root context instead:</p>
<pre class="lang-yaml prettyprint-override"><code>{{ range $index, $namespace := (lookup &quot;v1&quot; &quot;Namespace&quot; &quot;&quot; &quot;&quot;).items }}
apiVersion: v1
kind: Secret
metadata:
  name: myregcred
  namespace: {{ $namespace.metadata.name }}
type: kubernetes.io/dockerconfigjson
data:
  .dockerconfigjson: {{ template &quot;imagePullSecret&quot; $ }}
---
{{- end}}
</code></pre>
