Any equivalent/alternatives for "kubectl get provisioner" in golang

199 views Asked by At

We can list namespaces on the cluster with the help of client-go, something like below:

clientset.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})

Whether any similar option for kubectl get provisioner command. I know like client-go doesn't have this feature, but any other custom ones which can be used?

2

There are 2 answers

0
NoName On BEST ANSWER

Able to to get the required resources with the help of sigs.k8s.io/controller-runtime/pkg/client using unstructured objects to retrieve an object.

Reference: https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/client#example-Client-Get

0
Ömer Sezer On

There is no K8s built-in resource called "provisioner". However, storage classes use "provisioner" to create volume area. Persistent Volume (PV) uses storage classes. You can list provisioner over storage classes.

List All Storage Class:

kubectl get sc

Specific Storage Class:

kubectl get sc <StorageClass> -o json | jq -r '.provisioner'
e.g. kubectl get sc nfs-client -o json | jq -r '.provisioner'

Possible Outputs:

kubernetes.io/aws-ebs
cluster.local/nfs-subdir-external-provisioner
kubernetes.io/gce-pd

Possible Golang Code:

storageClassName := flag.String("storage-class", "my-storageclass", "Name of the storage class")
storageClass, err := clientset.StorageV1().StorageClasses().Get(context.TODO(), *storageClassName, metav1.GetOptions{})
fmt.Printf("Provisioner: %s\n", storageClass.Provisioner)

Refs:

https://www.kubermatic.com/blog/keeping-the-state-of-apps-5-introduction-to-storage-classes/

https://github.com/kubernetes/client-go/blob/master/informers/storage/v1/storageclass.go