Optional volume/secret volume in kubernetes?

1/12/2018

I'd like to mount volume if it exists. For example:

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: mypod
    image: redis
    volumeMounts:
    - name: foo
      mountPath: "/etc/foo"
  volumes:
  - name: foo
    secret:
      secretName: mysecret

is the example from the documentation. However if the secret mysecret doesn't exist I'd like to skip mounting. That is optimistic/optional mount point.

Now it stalls until the secret is created.

-- nmiculinic
kubernetes

2 Answers

1/12/2018

secret and configmap volumes can be marked optional, and result in empty directories if the associated secret or configmap doesn't exist, rather than blocking pod startup

See the SecretVolumeSource reference.

-- Jordan Liggitt
Source: StackOverflow

1/12/2018

While this optional logic exists for env variables, it's not available for volumes as far as I am aware. It also seems a bit problematic as your infrastructure stops being immutable, depending on sequence for creation in kube you get a different application state. Rather then looking for this I woud suggest utilising a higher level templating features like the ones available in Helm so that you can do :

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: mypod
    image: redis
{{- if .Values.mysecret.enabled }}
    volumeMounts:
    - name: foo
      mountPath: "/etc/foo"
  volumes:
  - name: foo
    secret:
      secretName: mysecret
{{- end }}

And then if you provision with --set mysecret.enabled=true you will get the volume declared and with --set mysecret.enabled=false it will not be declared so it will not attempt to mount it at all

-- Radek 'Goblin' Pieczonka
Source: StackOverflow