Shared Folder for users with r/o access for some and r/w access for some in Jupyterhub

For those who are interested, here is a little example on how to use the c.KubeSpawner.pod_modify_hook based on this post to mount additional PVCs into the users home dir.

Here I just use a static user -> volume map in a Python dict. Which allows you to also set the readOnly volumeMount option

erigrid = {
  'name': 'villas4erigrid',
  'pvc': 'group-villas4erigrid',
  'mountPath': '/home/jovyan/villas4erigrid',
  'readOnly': False
}
user_volume_map = {
  'lrsxpv3p': [ erigrid ],
  'vzi3jsam': [ erigrid ]
}

from kubernetes import client
from kubespawner.utils import get_k8s_model
from kubernetes.client.models import ( V1Volume, V1VolumeMount )

def modify_pod_hook(spawner, pod):
    try:
        user = spawner.user.name
        if user in user_volume_map:
            for volume in user_volume_map[user]:
                pod.spec.volumes.append(
                    get_k8s_model(V1Volume, {
                        'name' : volume['name'],
                        'persistentVolumeClaim': {
                            'claimName': volume['pvc']
                        }
                    })
                )

                # Note implicitly only 1 container...
                pod.spec.containers[0].volume_mounts.append(
                    get_k8s_model(V1VolumeMount, {
                        'name' : volume['name'],
                        'mountPath' : volume['mountPath'],
                        'readOnly': volume['readOnly']
                    })
                )
    except Exception as e:
        spawner.log.info("Exception in shared-mounts" + str(e))
        pass
    return pod

c.KubeSpawner.modify_pod_hook = modify_pod_hook