如何使用client-go库列出与持久卷声明相关联的Pod?

huangapple go评论121阅读模式
英文:

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

问题

使用以下的client-go调用来列出特定命名空间中的PVC。

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

我们如何获取与PVC相关联的Pod列表?

英文:

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

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

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

答案1

得分: 0

似乎我们需要使用循环和过滤来定位使用特定 PVC 的 Pod。以下是一个简单的代码示例,它将遍历特定命名空间中的 Pod,将具有 PVC 的 Pod 保存到新列表中并打印出来:

  1. // 设置命名空间
  2. var namespace = "default"
  3. // 获取 Pod 列表
  4. podList, _ := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
  5. // 创建新的 Pod 列表
  6. podsWithPVC := &corev1.PodList{}
  7. // 过滤 Pod,检查是否存在 PVC,如果存在则添加到列表中
  8. for _, pod := range podList.Items {
  9. for _, volume := range pod.Spec.Volumes {
  10. if volume.PersistentVolumeClaim != nil {
  11. podsWithPVC.Items = append(podsWithPVC.Items, pod)
  12. fmt.Println("Pod Name: " + pod.GetName())
  13. fmt.Println("PVC Name: " + volume.PersistentVolumeClaim.ClaimName)
  14. }
  15. }
  16. }

完整的代码示例(基于此代码):

  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "path/filepath"
  7. corev1 "k8s.io/api/core/v1"
  8. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  9. "k8s.io/client-go/kubernetes"
  10. "k8s.io/client-go/tools/clientcmd"
  11. "k8s.io/client-go/util/homedir"
  12. )
  13. func main() {
  14. var kubeconfig *string
  15. if home := homedir.HomeDir(); home != "" {
  16. kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
  17. } else {
  18. kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
  19. }
  20. flag.Parse()
  21. config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
  22. if err != nil {
  23. panic(err)
  24. }
  25. clientset, err := kubernetes.NewForConfig(config)
  26. if err != nil {
  27. panic(err)
  28. }
  29. // 设置命名空间
  30. var namespace = "default"
  31. // 获取 Pod 列表
  32. podList, _ := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
  33. // 创建新的 Pod 列表
  34. podsWithPVC := &corev1.PodList{}
  35. // 过滤 Pod,检查是否存在 PVC,如果存在则添加到列表中
  36. for _, pod := range podList.Items {
  37. for _, volume := range pod.Spec.Volumes {
  38. if volume.PersistentVolumeClaim != nil {
  39. podsWithPVC.Items = append(podsWithPVC.Items, pod)
  40. fmt.Println("Pod Name: " + pod.GetName())
  41. fmt.Println("PVC Name: " + volume.PersistentVolumeClaim.ClaimName)
  42. }
  43. }
  44. }
  45. }
英文:

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:

  1. // Set namespace
  2. var namespace = "default"
  3. // Get pods list
  4. podList, _ := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
  5. // Create new pod list
  6. podsWithPVC := &corev1.PodList{}
  7. // Filter pods to check if PVC exists, if yes append to the list
  8. for _, pod := range podList.Items {
  9. for _, volume := range pod.Spec.Volumes {
  10. if volume.PersistentVolumeClaim != nil {
  11. podsWithPVC.Items = append(podsWithPVC.Items, pod)
  12. fmt.Println("Pod Name: " + pod.GetName())
  13. fmt.Println("PVC Name: " + volume.PersistentVolumeClaim.ClaimName)
  14. }
  15. }
  16. }

Whole code (based on this code):

  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "path/filepath"
  7. corev1 "k8s.io/api/core/v1"
  8. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  9. "k8s.io/client-go/kubernetes"
  10. "k8s.io/client-go/tools/clientcmd"
  11. "k8s.io/client-go/util/homedir"
  12. )
  13. func main() {
  14. var kubeconfig *string
  15. if home := homedir.HomeDir(); home != "" {
  16. kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
  17. } else {
  18. kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
  19. }
  20. flag.Parse()
  21. config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
  22. if err != nil {
  23. panic(err)
  24. }
  25. clientset, err := kubernetes.NewForConfig(config)
  26. if err != nil {
  27. panic(err)
  28. }
  29. // Set namespace
  30. var namespace = "default"
  31. // Get pods list
  32. podList, _ := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
  33. // Create new pod list
  34. podsWithPVC := &corev1.PodList{}
  35. // Filter pods to check if PVC exists, if yes append to the list
  36. for _, pod := range podList.Items {
  37. for _, volume := range pod.Spec.Volumes {
  38. if volume.PersistentVolumeClaim != nil {
  39. podsWithPVC.Items = append(podsWithPVC.Items, pod)
  40. fmt.Println("Pod Name: " + pod.GetName())
  41. fmt.Println("PVC Name: " + volume.PersistentVolumeClaim.ClaimName)
  42. }
  43. }
  44. }
  45. }

huangapple
  • 本文由 发表于 2022年1月19日 19:13:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/70769569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定