--Define in metav1.ListOptions --There are many other than Label Selector
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() {
//Specify the file path of Kubeconfig
kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config")
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
log.Fatal(err)
}
//Load Kubeconfig
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatal(err)
}
//Call the pod list
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