登录
首页 >  Golang >  Go问答

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

来源:stackoverflow

时间:2024-02-18 16:39:24 150浏览 收藏

小伙伴们对Golang编程感兴趣吗?是否正在学习相关知识点?如果是,那么本文《如何使用 client-go 库列出与持久卷声明关联的 Pod?》,就很适合你,本篇文章讲解的知识点主要包括。在之后的文章中也会多多分享相关知识点,希望对大家的知识积累有所帮助!

问题内容

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

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

我们如何获取与 pvc 关联的 pod 列表?


正确答案


看来我们需要use loop and filtering - similar question on the GitHub

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

简单的代码将遍历特定命名空间中的 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)
                }
        }
}

整个代码(基于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)
                        }
                }
        }
}

到这里,我们也就讲完了《如何使用 client-go 库列出与持久卷声明关联的 Pod?》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>