Is there a way to mount volume in an init container?

Hi,

I would like to mount a volume on an init container but I get that error : “Spawn failed: ‘V1Container’ did not have an attribute matching ‘volumes’”. This seems logical since the V1Container specification does not specify this attribute. How to mount a volume in an init container ?

Here is an example :

spawner.init_containers = [
{
    "name": "test-mount-01",
    "image": "<<image>>",
    "command": ["sh", "-c", "env; set"],
    "securityContext": {
        "capabilities": {
            "add": ["NET_ADMIN"]
        }
    },
    "env": [
      {
        "name": "JUPYTERHUB_USER",
        "value": user.name
      },
      {
        "name": "ID_TOKEN",
        "value": auth_state['id_token']
      },
    ],
    "volumes": [
                  {
                      "configMap": {
                          "defaultMode": 420,
                          "items": [
                              {
                                  "key": "sync.sh",
                                  "path": "sync.sh"
                              }
                          ],
                          "name": "shared-sync"
                      },
                      "name": "script"
                  },
                  {
                      "configMap": {
                          "defaultMode": 420,
                          "items": [
                              {
                                  "key": "credentials",
                                  "path": "credentials"
                              }
                          ],
                          "name": "shared-sync"
                      },
                      "name": "credentials"
                  },
                  {
                      "name": "jupyterhub-shared",
                      "persistentVolumeClaim": {
                          "claimName": "jupyterhub-shared-volume"
                      }
                  }
              ]
}]

https://jupyterhub-kubespawner.readthedocs.io/en/latest/spawner.html#kubespawner.KubeSpawner.init_containers

The content is passed through to Kubernetes unchanged, so you should be able to follow the standard Kubernetes documentation on Containers

I was wondering if c.KubeSpawner.init_containers accesses the user instance of spawner or it’s the class itself? or can I set specific init_containers in the pre_spawn_hook based on the user?

Yes, you can populate spawner.init_containers during pre_spawn_hook.

1 Like