Search code examples
kuberneteskubernetes-helm

Helm chart: reference a secret gives name: %!s(<nil>)-%!s(<nil>)


I am creating a Helm chart. When doing a dry run I get a error:

Error: YAML parse error on vstsagent/templates/vsts-buildrelease-agent.yaml: error converting YAML to JSON: yaml: line 28: found character that cannot start any token

The dry run also outputs the secret and deployment YAML file which I created. The part where it goes wrong in the deployment shows:

      - name: ACCOUNT
        valueFrom:
          secretKeyRef:
            name: %!s(<nil>)-%!s(<nil>)
            key: ACCOUNT
      - name: TOKEN
        valueFrom:
          secretKeyRef:
            name:  %!s(<nil>)-%!s(<nil>)
            key: TOKEN

The output from the dry run for the secret looks fine.

The templates I created:

apiVersion: v1
kind: Secret
metadata:
  name: {{ template "chart.fullname" . }}
type: Opaque
data:
 ACCOUNT: {{ .Values.chart.secret.account }}
 TOKEN: {{ .Values.chart.secret.token }}


apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "chart.fullname" . }}
  labels:
    app: {{ template "chart.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
spec:
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        release: {{ .Release.Name }}
        app: {{ template "chart.name" . }}
      annotations:
        agentVersion: {{ .Values.chart.image.tag }}
      spec:
        containers:
          - name: {{ template "chart.name" . }}
            image: {{ .Values.chart.image.name }}
            imagePullPolicy: {{ .Values.chart.image.pullPolicy }}
            env:
              - name: ACCOUNT
                valueFrom:
                  secretKeyRef:
                    name: {{ template "chart.fullname" }}
                    key: ACCOUNT
              - name: TOKEN
                valueFrom:
                  secretKeyRef:
                    name:  {{ template "chart.fullname" }}
                    key: TOKEN

The _helper.tpl looks like this:

{{/* vim: set filetype=mustache: */}}
{{/*
Expand the name of the chart.
*/}}
{{- define "chart.name" -}}
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
{{- end -}}

{{/*
Create a default fully qualified app name.
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
*/}}
{{- define "chart.fullname" -}}
{{- $name := default .Chart.Name .Values.nameOverride -}}
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
{{- end -}}

Where am I going wrong in this?


Solution

  • I missed 2 dots....

          - name: ACCOUNT
            valueFrom:
              secretKeyRef:
                name: {{ template "chart.fullname" . }}
                key: ACCOUNT
          - name: TOKEN
            valueFrom:
              secretKeyRef:
                name:  {{ template "chart.fullname" . }}
                key: TOKEN