导语:使用client-go 调用测试环境

将测试环境的kubelet.kubeconfig 或者/root/.kube/config 拷贝到电脑的$HOME/.kube/下

go.mod

module demo

go 1.13

require (
	github.com/evanphx/json-patch v4.9.0+incompatible // indirect
	github.com/fsnotify/fsnotify v1.4.9 // indirect
	github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7 // indirect
	github.com/golang/protobuf v1.4.2 // indirect
	github.com/googleapis/gnostic v0.4.0 // indirect
	github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
	github.com/imdario/mergo v0.3.11 // indirect
	github.com/json-iterator/go v1.1.10 // indirect
	github.com/pkg/errors v0.9.1 // indirect
	golang.org/x/net v0.0.0-20200707034311-ab3426394381 // indirect
	golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4 // indirect
	golang.org/x/text v0.3.3 // indirect
	golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e // indirect
	google.golang.org/protobuf v1.24.0 // indirect
	k8s.io/apimachinery v0.17.0
	k8s.io/client-go v0.17.0
	k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac // indirect
	k8s.io/klog/v2 v2.2.0 // indirect
	k8s.io/utils v0.0.0-20201110183641-67b214c5f920 // indirect
	sigs.k8s.io/structured-merge-diff/v4 v4.0.1 // indirect
)

main.go

package main

import (
	"flag"
	//    "context"
	"fmt"
	"path/filepath"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/clientcmd"
	"k8s.io/client-go/util/homedir"
)

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())
	}
	// creates the clientset
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		panic(err.Error())
	}

	pods, err1 := clientset.CoreV1().Pods("").List(metav1.ListOptions{})
	if err1 != nil {
		panic(err1.Error())
	}
	pvs, err2 := clientset.CoreV1().PersistentVolumes().List(metav1.ListOptions{})
	if err2 != nil {
		panic(err2.Error())
	}
	pvcs, err3 := clientset.CoreV1().PersistentVolumeClaims("").List(metav1.ListOptions{})
	if err3 != nil {
		panic(err3.Error())
	}
	namespaces, err4 := clientset.CoreV1().Namespaces().List(metav1.ListOptions{})
	if err4 != nil {
		panic(err4.Error())
	}
	fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
	fmt.Printf("There are %d pvs in the cluster\n", len(pvs.Items))
	fmt.Printf("There are %d pvcs in the cluster\n", len(pvcs.Items))
	fmt.Printf("There are %d namespaces in the cluster\n", len(namespaces.Items))

	fmt.Println("---------pods----------")
	for _, pod := range pods.Items {
		fmt.Printf("Name: %s, Status: %s, CreateTime: %s,NameSpace: %s\n", pod.ObjectMeta.Name, pod.Status.Phase, pod.ObjectMeta.CreationTimestamp, pod.Namespace)
	}
	fmt.Println("---------pvs----------")
	for _, pv := range pvs.Items {
		fmt.Printf("Name: %s, Status: %s, CreateTime: %s\n", pv.ObjectMeta.Name, pv.Status.Phase, pv.ObjectMeta.CreationTimestamp)
	}
	fmt.Println("---------pvcs----------")
	for _, pvc := range pvcs.Items {
		fmt.Printf("Name: %s, Status: %s, CreateTime: %s\n", pvc.ObjectMeta.Name, pvc.Status.Phase, pvc.ObjectMeta.CreationTimestamp)
	}
	fmt.Println("---------namespaces----------")
	for _, namespace := range namespaces.Items {
		fmt.Printf("Name: %s, Status: %s, CreateTime: %s\n", namespace.ObjectMeta.Name, namespace.Status.Phase, namespace.ObjectMeta.CreationTimestamp)
	}
}

运行测试

go run main.go

参考

https://blog.csdn.net/xixihahalelehehe/article/details/110358813

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐