Home > database >  Creating kubernetes multiple docker-registry secrets in each lookup namespaces using helm chart?
Creating kubernetes multiple docker-registry secrets in each lookup namespaces using helm chart?

Time:01-04

I am trying to create multiple secrets with different names in the same namespaces using below helm chart.

{{ range $index, $namespace := (lookup "v1" "Namespace" "" "").items }}
apiVersion: v1
kind: Secret
metadata:
  name: myregcred
  namespace: {{ $namespace.metadata.name }}
type: kubernetes.io/dockerconfigjson
data:
  .dockerconfigjson: {{ template "imagePullSecret" . }}
{{- end}}

I have tried the below

values.yaml

imageCredentials:
 - name: cred1
   registry: quay.io
   username: someone
   password: sillyness
   email: [email protected]
 - name: cred2
   registry: quay.io
   username: someone
   password: sillyness
   email: [email protected]

_helpers.tpl

{{- define "imagePullSecret" }}
{{- with .Values.imageCredentials }}
{{- printf "{\"auths\":{\"%s\":{\"username\":\"%s\",\"password\":\"%s\",\"email\":\"%s\",\"auth\":\"%s\"}}}" .registry .username .password .email (printf "%s:%s" .username .password | b64enc) | b64enc }}
{{- end }}
{{- end }}

When i run this helm chart, i get the below error

Error: INSTALLATION FAILED: template: secrets/templates/_helpers.tpl:2:16: executing "imagePullSecret" at <.Values.imageCredentials>: nil pointer evaluating interface {}.imageCredentials

how can i create two secrets in each namespaces ?

CodePudding user response:

The culprint is this:

{{ range ... }}
{{ template "imagePullSecret" . }}
{{ end }}

Since you are inside the loop, the context (dot .) is set to the current item of the iteration.

You want to reference the root context using $ instead of the dot ..

Its mentioned in the offical docs:

When execution begins, $ is set to the data argument passed to Execute, that is, to the starting value of dot.

It is also mentioned in the helm docs:

However, there is one variable that is always global - $ - this variable will always point to the root context. This can be very useful when you are looping in a range and you need to know the chart's release name.

Additionally, you want to have a second loop creating all secret for each namespace:

{{ range $index, $namespace := (lookup "v1" "Namespace" "" "").items }}
{{ range $.Values.imageCredentials }}
---
apiVersion: v1
kind: Secret
metadata:
  name: {{ .name }}
  namespace: {{ $namespace.metadata.name }}
type: kubernetes.io/dockerconfigjson
data:
  .dockerconfigjson: {{ template "imagePullSecret" . }}
{{ end}}
{{ end}}

And adjust your helper to work with the item directly by removing the with clause. This is because you are passing the item directly into the template.

{{- define "imagePullSecret" }}
{{- printf "{\"auths\":{\"%s\":{\"username\":\"%s\",\"password\":\"%s\",\"email\":\"%s\",\"auth\":\"%s\"}}}" .registry .username .password .email (printf "%s:%s" .username .password | b64enc) | b64enc }}
{{- end }}

CodePudding user response:

This works for me by replacing . with $

{{ range $index, $namespace := (lookup "v1" "Namespace" "" "").items }}
{{ range $.Values.imageCredentials }}
---
apiVersion: v1
kind: Secret
metadata:  
  name: {{ .name }}
  namespace: {{ $namespace.metadata.name }}
type: kubernetes.io/dockerconfigjson
data:
  .dockerconfigjson: {{ template "imagePullSecret" $ }}
{{ end }}
{{ end }}
  • Related