繁体   English   中英

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

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

使用下面的客户端调用来列出特定命名空间中的 PVC。

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

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

看来我们需要使用循环和过滤 - GitHub 上的类似问题

不,循环和过滤是使用特定 PVC 定位 pod 的唯一方法

将 go 通过特定命名空间中的 pod 的简单代码,将带有 PVC 的 pod 保存到新列表并打印:

// 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)
                }
        }
}

整个代码(基于此代码):

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)
                        }
                }
        }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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