pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: "app=vamdemic111aaa-app",
})
package main
import (
"context"
"encoding/json"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"log"
"os"
"path/filepath"
)
func main() {
//Geben Sie den Dateipfad von Kubeconfig an
kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config")
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
log.Fatal(err)
}
//Laden Sie Kubeconfig
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatal(err)
}
//Rufen Sie die Pod-Liste auf
namespace := "default"
pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: "app=vamdemic111aaa-app",
})
if err != nil {
log.Fatalln("failed to get pods:", err)
}
m := map[string]string{}
for _, pod := range pods.Items {
m[pod.Name] = string(pod.Status.Phase)
}
data, _ := json.Marshal(m)
fmt.Printf(string(data))
}
https://blog.nnn.dev/entry/2019/12/21/144550
Recommended Posts