Search code examples
kuberneteskubernetes-helm

nil pointer evaluating interface when installing a helm chart


I'm trying to install a chart to my cluster but I'm getting a error

Error: template: go-api/templates/deployment.yaml:18:24: executing "go-api/templates/deployment.yaml" 
at <.Values.deployment.container.name>: nil pointer evaluating interface {}.name

However I executed the same commands for another 2 charts and it worked fine.

The template file I'm using is this:

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: {{ .Values.namespace}}
  labels: {{- include "chart.labels" . | nindent 4 }}
  name: {{ .Values.deployment.name}}
spec:
  replicas: {{ .Values.deployment.replicas}}
  selector:
    matchLabels: {{ include "chart.matchLabels" . | nindent 6 }}
  template:
    metadata:
      labels: 
        app.kubernetes.io/name: {{ template "chart.name" . }}
        app.kubernetes.io/instance: {{ .Release.Name }}
    spec:
      containers:
      - name: {{ .Values.deployment.container.name }}
        image: {{ .Values.deployment.container.image }}
        imagePullPolicy: Never
        ports:
          - containerPort: {{ .Values.deployment.container.port }}


Solution

  • This can happen if the Helm values you're using to install don't have that particular block:

    namespace: default
    deployment:
      name: a-name
      replicas: 1
      # but no container:
    

    To avoid this specific error in the template code, it's useful to pick out the parent dictionary into a variable; then if the parent is totally absent, you can decide what to do about it. This technique is a little more useful if there are optional fields or sensible defaults:

    {{- $deployment := .Values.deployment | default dict }}
    metadata:
      name: {{ $deployment.name | default (include "chart.fullname" .) }}
    spec:
    {{- if $deployment.replicas }}
      replicas: {{ $deployment.replicas }}
    {{- end }}
    

    If you really can't work without the value, Helm has an undocumented required function that can print a more specific error message.

    {{- $deployment := .Values.deployment | required "deployment configuration is required" }}
    

    (My experience has been that required values are somewhat frustrating as an end user, particularly if you're trying to run someone else's chart, and I would try to avoid this if possible.)

    Given what you show, it's also possible you're making the chart too configurable. The container name, for example, is mostly a detail that only appears if you have a multi-container pod (or are using Istio); the container port is a fixed attribute of the image you're running. You can safely fix these values in the Helm template file, and then it's reasonable to provide defaults for things like the replica count or image name (consider setting the repository name, image name, and tag as separate variables).

    {{- $deployment := .Values.deployment | default dict }}
    {{- $registry := $deployment.registry | default "docker.io" }}
    {{- $image := $deployment.image | default "my/image" }}
    {{- $tag := $deployment.tag | default "latest" }}
    containers:
      - name: app # fixed
        image: {{ printf "%s/%s:%s" $registry $image $tag }}
    {{- with .Values.imagePullPolicy }}
        imagePullPolicy: {{ . }}
    {{- end }}
        ports:
          - name: http
            containerPort: 3000 # fixed