How to list storage classes using Kubernetes client

1/9/2019

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.

-- ambikanair
api
java
kubernetes
node.js

2 Answers

1/9/2019

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.

-- Prafull Ladha
Source: StackOverflow

1/14/2019

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

-- Tajamul Fazili
Source: StackOverflow