64 lines
1.8 KiB
Go
64 lines
1.8 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"flag"
|
||
|
"fmt"
|
||
|
"k8s.io/client-go/kubernetes"
|
||
|
"k8s.io/client-go/tools/clientcmd"
|
||
|
"k8s.io/client-go/util/homedir"
|
||
|
|
||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||
|
"path/filepath"
|
||
|
)
|
||
|
|
||
|
// 集群外初始化 client
|
||
|
// 需要集群的 kubeconfig 配置文件
|
||
|
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()
|
||
|
|
||
|
// use the current context in kubeconfig
|
||
|
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
|
||
|
if err != nil {
|
||
|
panic(err.Error())
|
||
|
}
|
||
|
|
||
|
// create the clientset
|
||
|
clientSet, err := kubernetes.NewForConfig(config)
|
||
|
if err != nil {
|
||
|
panic(err.Error())
|
||
|
}
|
||
|
|
||
|
nodes, err := clientSet.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
|
||
|
if err != nil {
|
||
|
panic(err.Error())
|
||
|
}
|
||
|
pods, err := clientSet.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
|
||
|
if err != nil {
|
||
|
panic(err.Error())
|
||
|
}
|
||
|
fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
|
||
|
for _, v := range pods.Items {
|
||
|
fmt.Println(v.Name)
|
||
|
}
|
||
|
fmt.Println("===================================================================================================")
|
||
|
for _, v := range nodes.Items {
|
||
|
fmt.Println("========================================")
|
||
|
fmt.Printf("node:%v ip:%v\n", v.Name, v.Status.Addresses[0].Address)
|
||
|
for _, c := range v.Status.Conditions {
|
||
|
fmt.Println("-----------------------------------------")
|
||
|
fmt.Printf("message:%v\n", c.Message)
|
||
|
fmt.Printf("status:%v\n", c.Status)
|
||
|
fmt.Printf("type:%v\n", c.Type)
|
||
|
fmt.Printf("reason:%v\n", c.Reason)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|