简体   繁体   中英

How to list storage classes using Kubernetes client

I am using below line of code to get details about a particular PVC

response = await `serverModule.kubeclient.api.v1.namespaces(ns).persistentvolumeclaims(pvc).get();`

The corresponding API for above call is readNamespacedPersistentVolumeClaim with below format

GET /api/v1/namespaces/{namespace}/persistentvolumeclaims/{name}

Now, I am trying to call readStorageClass using same convention as above

response = await serverModule.kubeclient.apis.storage.k8s.io.v1.storageclasses(sc).get();

As you can see in the link, GET /apis/storage.k8s.io/v1/storageclasses/{name} is the format, I have used above syntax. But for some reason the code fails with error

Exported kubeclient, ready to process requests
TypeError: Cannot read property 'k8s' of undefined

What is the syntax error that I have made. I tried various combinations but none worked.

This issue is listing PersistentVolumeClaim is a part of coreV1Api of kubernetes and listing StorageClass is the part of StorageV1beta1Api . Following it the simplest code for listing storage class using JAVA client:

ApiClient defaultClient = Configuration.getDefaultApiClient();

// Configure API key authorization: BearerToken
ApiKeyAuth BearerToken = (ApiKeyAuth) defaultClient.getAuthentication("BearerToken");
BearerToken.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//BearerToken.setApiKeyPrefix("Token");

StorageV1beta1Api apiInstance = new StorageV1beta1Api();
try {
    V1beta1StorageClassList result = apiInstance.listStorageClass();
    System.out.println(result);
} catch (ApiException e) {
    System.err.println("Exception when calling StorageV1beta1Api#listStorageClass");
    e.printStackTrace();
}

Following is the official documentation link for your reference:

https://github.com/kubernetes-client/java/blob/master/kubernetes/docs/StorageV1beta1Api.md#listStorageClass

Hope this helps.

Use client.apis["storage.k8s.io"].v1.storageclasses.get() , Applicable to any api containing dots. Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM