亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 client-go 庫列出與持久卷聲明關聯的 Pod?

如何使用 client-go 庫列出與持久卷聲明關聯的 Pod?

Go
慕容3067478 2022-10-24 17:05:19
使用下面的客戶端調用來列出特定命名空間中的 PVC。x, err := clientset.CoreV1().PersistentVolumeClaims("namespace_name").List(context.TODO(), metav1.ListOptions{})我們如何獲取與 PVC 關聯的 Pod 列表?
查看完整描述

1 回答

?
DIEA

TA貢獻1820條經驗 獲得超3個贊

看來我們需要使用循環和過濾 - GitHub上的類似問題

不,循環和過濾是使用特定 PVC 定位 pod 的唯一方法

將通過特定命名空間中的 pod 的簡單代碼,將帶有 PVC 的 pod 保存到新列表并打?。?/p>

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

                        }

                }

        }

}


查看完整回答
反對 回復 2022-10-24
  • 1 回答
  • 0 關注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號