kuberneteskubectlkubernetes-apiserverkubernetes-python-client

In kubectl python client how to get storage class by name in yaml format


I am trying to get yaml for the particular storage class using Kubernetes python client, following code does the job:

list_storage_class = v1.list_storage_class(_preload_content=False)
storageclass = list_storage_class.read().decode('utf-8') # <class 'urllib3.response.HTTPResponse'>
print(json.dumps(json.loads(storageclass),indent=2))

Is it a way to specify storage class with the call? On the same hand is it possible to get response directly in yaml conforming to k get sc NAME -o yaml ?


Solution

  • Take a look at the StorageV1API documentation. To get a single StorageClass, you want the read_storage_class method:

    >>> from kubernetes import client, config
    >>> config.load_kube_config()
    >>> v1 = client.StorageV1Api()
    >>> sc = v1.read_storage_class('ocs-external-storagecluster-ceph-rbd')
    >>> sc.metadata.name
    'ocs-external-storagecluster-ceph-rbd'
    

    If you want to dump the result to a YAML document:

    >>> import yaml
    >>> print(yaml.safe_dump(sc.to_dict()))
    allow_volume_expansion: true
    allowed_topologies: null
    api_version: storage.k8s.io/v1
    kind: StorageClass
    metadata:
      annotations:
        description: Provides RWO Filesystem volumes, and RWO and RWX Block volumes
        storageclass.kubernetes.io/is-default-class: 'true'
      labels:
        app.kubernetes.io/instance: cluster-resources-ocp-staging
    ...