How to get pod volume list using python?

5/31/2021

My pod has a volume as:

        "volumes": [
            {
                "name": "configs",
                "secret": {
                    "defaultMode": 420,
                    "secretName": "some_secret"
                }
            },
            ....]

I want to be able to read it using Python as V1Volume.

Tried to do:

    from kubernetes import config
    config.load_incluster_config()
    spec = client.V1PodSpec()

But I'm stuck as it gives me

    raise ValueError("Invalid value for `containers`, must not be `None`")

and I'm not sure how to continue. How can I get the volumes from the V1PodSpec?

-- sony
kubernetes
python

1 Answer

5/31/2021

It gives you the error because you initialise V1PodSpec without any arguments. V1PodSpec used to create pods, not to read them.

To read pod spec from Kubernetes:

from  kubernetes import client,config
config.load_kube_config()
# or 
# config.load_incluster_config()
core_api = client.CoreV1Api()
response = core_api.read_namespaced_pod(name="debug-pod", namespace='dev')

# access volumes in the returned response
type(response.spec.volumes[0])
# returns:
# <class 'kubernetes.client.models.v1_volume.V1Volume'>
-- anemyte
Source: StackOverflow