Home > Software engineering >  How to describe PV from kubernetes python API
How to describe PV from kubernetes python API

Time:08-30

From a certain PVC, I'm trying to get the volume id from the metadata of the PV associated with the PVC using Kubernetes Python Api.

I'm able to describe PVC with read_namespaced_persistent_volume_claim function and obtain the PV name spec.volume_name. Now I need to go deeper and get the Source.VolumeHandle attribute from the PV metadata to get de EBS Volume Id and obtain the volume status from aws, but I can't find a method to describe pv from the python api.

Any help?

Thanks

CodePudding user response:

While PersistentVolumeClaims are namedspaced, PersistentVolumes are not. Looking at the available methods in the V1 API...

>>> v1 = client.CoreV1Api()
>>> print('\n'.join([x for x in dir(v1) if x.startswith('read') and 'volume' in x]))
read_namespaced_persistent_volume_claim
read_namespaced_persistent_volume_claim_status
read_namespaced_persistent_volume_claim_status_with_http_info
read_namespaced_persistent_volume_claim_with_http_info
read_persistent_volume
read_persistent_volume_status
read_persistent_volume_status_with_http_info
read_persistent_volume_with_http_info

...it looks like read_persistent_volume is probably what we want. Running help(v1.read_persistent_volume) gives us:

read_persistent_volume(name, **kwargs) method of kubernetes.client.api.core_v1_api.CoreV1Api instance
    read_persistent_volume

    read the specified PersistentVolume
    This method makes a synchronous HTTP request by default. To make an
    asynchronous HTTP request, please pass async_req=True
    >>> thread = api.read_persistent_volume(name, async_req=True)
    >>> result = thread.get()

    :param async_req bool: execute request asynchronously
    :param str name: name of the PersistentVolume (required)
    :param str pretty: If 'true', then the output is pretty printed.
    :param _preload_content: if False, the urllib3.HTTPResponse object will
                             be returned without reading/decoding response
                             data. Default is True.
    :param _request_timeout: timeout setting for this request. If one
                             number provided, it will be total request
                             timeout. It can also be a pair (tuple) of
                             (connection, read) timeouts.
    :return: V1PersistentVolume
             If the method is called asynchronously,
             returns the request thread.

  • Related