内容目录
package main
import (
"context"
"fmt"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"os"
)
func main() {
kubeconfig := "config"
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
panic(err.Error())
}
clientset, _ := kubernetes.NewForConfig(config)
pods, err := clientset.CoreV1().Pods("default").List(context.TODO(), v1.ListOptions{})
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for _, v := range pods.Items {
fmt.Println(v.ObjectMeta.Labels["app.kubernetes.io/name"])
}
}
文章评论