Search code examples
kubernetes-helm

How to set Secret name in Helm templates/deployment.yaml?


How to set Secret name in Helm templates/deployment.yaml? I have this code in templates/deployment.yaml:

containers:
  - name: {{ .Chart.Name }}
    envFrom:
    - secretRef:
        name: {{ .Values.secretName }}

in values.yaml file i have this:

envFrom:
- secretRef:
    name: secret1

But helm gives error when creating:

Error: INSTALLATION FAILED: 1 error occurred:
        * Deployment.apps "release1-chart1" is invalid: spec.template.spec.containers[0].envFrom[0].secretRef.name: Required value

Solution

  • The values.yaml file has configuration for how to install the chart; it does not usually contain fragments of Kubernetes YAML. In your template code you refer to .Values.secretName, which means there needs to be a top-level key in values.yaml named secretName.

    # templates/deployment.yaml
    envFrom:
      - secretRef:
          name: {{ .Values.secretName }}
    
    # values.yaml
    secretName: secret1
    

    If the Secret is part of your Helm chart, you might not need to configure its name, so long as the Deployment and the Secret use the same name.

    # templates/secret.yaml
    metadata:
      name: {{ include "mychart.fullname" . }}
    
    # templates/deployment.yaml
    envFrom:
      - secretRef:
          name: {{ include "mychart.fullname" . }}
    
    # secret name doesn't appear in values.yaml