Helm chart upgrade throws "error calling index: index of untyped nil"

8/13/2021

I'm having the following secret definition:

apiVersion: v1
kind: Secret
metadata:
    name: "{{ .Release.Name }}-psql"
type: Opaque
stringData:
  {{ if not .Release.IsUpgrade }}
  dbReplPassword: {{ (randAlphaNum 23) | quote }}
  {{ else  }}
  dbReplPassword: {{ index (lookup "v1" "Secret" .Release.Namespace "{{ .Release.Name }}-psql").data "dbReplPassword" | b64dec | quote }}
  {{ end }}

On initial installation of my helm chart, everything works smoothly. But when I upgrade my helm chart, it shows me the following error:

pgsql.yaml:11:21: executing "helm-chart/templates/pgsql.yaml" at <index (lookup "v1" "Secret" .Release.Namespace "{{ .Release.Name }}-psql").data "dbReplPassword">: error calling index: index of untyped nil

I guess the problem is the "{{ .Release.Name }}-psql" part of the lookup function, but I've no idea how I can fix this issue. Is anyone having any idea how I can fix it?

-- Nrgyzer
kubernetes
kubernetes-helm

1 Answer

8/13/2021

You can't nest Helm templates like this. The standard Go text/template language includes a printf function that you can use to construct a string from parameters, so use this instead:

... (lookup "v1" "Secret" .Release.Namespace (printf "%s-psql" .Release.Name)) ...

It could be clearer to break up this complex expression into multiple variables:

{{- $secretName := printf "%s-psql" .Release.Name }}
{{- $secret := lookup "v1" "Secret" .Release.Namespace $secretName }}
dbReplPassword: {{ $secret.data.dbReplPassword | b64dec | quote }}
-- David Maze
Source: StackOverflow