How to list Pods which are associated with persistent volume claim using client-go library?

1/19/2022

Using below client-go call to list PVC in a particular namespace.

x, err := clientset.CoreV1().PersistentVolumeClaims("namespace_name").List(context.TODO(), metav1.ListOptions{})

How we can get a list of Pods associated with PVC?

-- Akash
client-go
go
kubernetes

1 Answer

1/20/2022

It seems that we need to use loop and filtering - similar question on the GitHub:

No, looping and filtering is the only way to locate pods using a specific PVC

Simple code that will go through pods in specific namespace, save pods with PVC to new list and print:

// Set namespace
var namespace = "default"

// Get pods list
podList, _ := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})

// Create new pod list
podsWithPVC := &corev1.PodList{}

// Filter pods to check if PVC exists, if yes append to the list
for _, pod := range podList.Items {
        for _, volume := range pod.Spec.Volumes {
                if volume.PersistentVolumeClaim != nil {
                        podsWithPVC.Items = append(podsWithPVC.Items, pod)
                        fmt.Println("Pod Name: " + pod.GetName())
                        fmt.Println("PVC Name: " + volume.PersistentVolumeClaim.ClaimName)
                }
        }
}

Whole code (based on this code):

package main

import (
        "context"
        "flag"
        "fmt"
        "path/filepath"

        corev1 "k8s.io/api/core/v1"
        metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
        "k8s.io/client-go/kubernetes"
        "k8s.io/client-go/tools/clientcmd"
        "k8s.io/client-go/util/homedir"
)

func main() {

        var kubeconfig *string
        if home := homedir.HomeDir(); home != "" {
                kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
        } else {
                kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
        }
        flag.Parse()

        config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
        if err != nil {
                panic(err)
        }
        clientset, err := kubernetes.NewForConfig(config)
        if err != nil {
                panic(err)
        }

        // Set namespace
        var namespace = "default"

        // Get pods list
        podList, _ := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})

        // Create new pod list
        podsWithPVC := &corev1.PodList{}

        // Filter pods to check if PVC exists, if yes append to the list
        for _, pod := range podList.Items {
                for _, volume := range pod.Spec.Volumes {
                        if volume.PersistentVolumeClaim != nil {
                                podsWithPVC.Items = append(podsWithPVC.Items, pod)
                                fmt.Println("Pod Name: " + pod.GetName())
                                fmt.Println("PVC Name: " + volume.PersistentVolumeClaim.ClaimName)
                        }
                }
        }
}
-- Mikolaj S.
Source: StackOverflow