获取所有以前缀开头的Kubernetes pods



我有一个Kubernetes集群,其中有几个(未知数量,因为可以在运行时创建更多)pod,它们的标签前缀为x=y。基于此前缀(x=),我如何以编程方式获得所有y值的列表?

您需要做如下操作:

# kClient ---> Kubernetes Client
# List all the pods from all namespaces
podList, err := es.kClient.CoreV1().Pods(core.NamespaceAll).List(context.TODO(), metav1.ListOptions{})
if err != nil {
log.Fatal(err)
}
# Store answer in Ys
var Ys []string
# Loop over all the pods, and check for the label key "x"
# If "x" exists, store the value of "y" in Ys.
for _, pod := range podList.Items {
if y, exists := pod.Labels["x"]; exists {
Ys = append(Ys, y)
}
}

fmt.Println(Ys)

使用kubernetes客户端coreV1Api列出与标签匹配的pod

coreV1Api.list_namespaced_pod(namespace=<namespace>, label_selector="X=Y")

最新更新