K8s/Helm - how to mount volume based on value attribute?

7/27/2020

I have the values file with some attributes that will be used on mount volumes. As example:

secrets:
  my-secret:
    as: volume
    key: file.xml
    mountPath: /etc/xml

I'm trying to mount volumes only if the value of as is volume:

          volumeMounts:
          {{- range $name, $value := .Values.secrets }}
            {{- if eq $value.as "volume" }}
            - name: {{ $name }}-volume
              mountPath: {{ quote $value.mountPath }}
            {{- end }}
          {{- end }}

For some reason, it's failing:

Error: Deployment.apps "webanalytics" is invalid: spec.template.spec.containers[0].volumeMounts[0].name: Not found: "my-secret-volume"

Any idea on what's wrong?

-- placplacboom
kubernetes
kubernetes-helm

2 Answers

7/27/2020

From what I understood,

secrets:
  my-secret:             // this is the name of your secret
    as: volume           // when this value is "volume", you mount it; otherwise, it's ignored
    key: file.xml        // contents of the secret named "my-secret"
    mountPath: /etc/xml

So ultimately, the secret will be named my-secret. I think you should try by removing -volume in deployment template like below:

      volumeMounts:
          {{- range $name, $value := .Values.secrets }}
            {{- if eq $value.as "volume" }}
            - name: {{ $name }}
              mountPath: {{ quote $value.mountPath }}
            {{- end }}
          {{- end }}

Above logic only works if : 1. The secret/my-secret is created in helm template. 2. The template of secret doesn't have -volume hardcoded and name is taken dynamically from Values.yaml

-- Aditya Jalkhare
Source: StackOverflow

7/27/2020

The logs show that you are accessing "my-secret-volume". But that has to exist in your Kubernetes cluster in the same namespace. The error shows that it doesn't exist so you have to create it ⚒️.

Because you didn't share the whole template it could be that the secrets is actually created by the Helm template but it has a different name from "my-secret-volume"

☮️✌️

-- Rico
Source: StackOverflow