Optional volume/secret volume in kubernetes?

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


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

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
        optional: true

Tags:

Kubernetes