如何使用客户端go-in golang程序列出k8s集群中的所有POD?

如何使用客户端go-in golang程序列出k8s集群中的所有POD?,go,kubernetes,client-go,Go,Kubernetes,Client Go,我想使用客户机go-in-go程序列出k8s集群中的所有POD。 使用客户端go列出k8s集群中所有POD的go程序???假设您正在集群中运行该程序,请使用InClusterConfig如下所示,并调用clientset.CoreV1().pods(“”.list(context.TODO(),metav1.ListOptions{})。因为我们没有为名称空间传递任何值,所以它将列出所有名称空间中的所有pod func main() { // creates the in-cluster

我想使用客户机go-in-go程序列出k8s集群中的所有POD。
使用客户端go列出k8s集群中所有POD的go程序???

假设您正在集群中运行该程序,请使用
InClusterConfig
如下所示,并调用
clientset.CoreV1().pods(“”.list(context.TODO(),metav1.ListOptions{})
。因为我们没有为名称空间传递任何值,所以它将列出所有名称空间中的所有pod

func main() {
    // creates the in-cluster config
    config, err := rest.InClusterConfig()
    if err != nil {
        panic(err.Error())
    }
    // creates the clientset
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        panic(err.Error())
    }
    for {
        // get pods in all the namespaces by omitting namespace
        // Or specify namespace to get pods in particular namespace
        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))

        // Examples for error handling:
        // - Use helper functions e.g. errors.IsNotFound()
        // - And/or cast to StatusError and use its properties like e.g. ErrStatus.Message
        _, err = clientset.CoreV1().Pods("default").Get(context.TODO(), "example-xxxxx", metav1.GetOptions{})
        if errors.IsNotFound(err) {
            fmt.Printf("Pod example-xxxxx not found in default namespace\n")
        } else if statusError, isStatus := err.(*errors.StatusError); isStatus {
            fmt.Printf("Error getting pod %v\n", statusError.ErrStatus.Message)
        } else if err != nil {
            panic(err.Error())
        } else {
            fmt.Printf("Found example-xxxxx pod in default namespace\n")
        }

        time.Sleep(10 * time.Second)
    }
}

假设您正在集群内运行该程序,请使用下面的
InClusterConfig
,并调用
clientset.CoreV1().Pods(“”.List(context.TODO(),metav1.ListOptions{})
。因为我们没有为名称空间传递任何值,所以它将列出所有名称空间中的所有pod

func main() {
    // creates the in-cluster config
    config, err := rest.InClusterConfig()
    if err != nil {
        panic(err.Error())
    }
    // creates the clientset
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        panic(err.Error())
    }
    for {
        // get pods in all the namespaces by omitting namespace
        // Or specify namespace to get pods in particular namespace
        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))

        // Examples for error handling:
        // - Use helper functions e.g. errors.IsNotFound()
        // - And/or cast to StatusError and use its properties like e.g. ErrStatus.Message
        _, err = clientset.CoreV1().Pods("default").Get(context.TODO(), "example-xxxxx", metav1.GetOptions{})
        if errors.IsNotFound(err) {
            fmt.Printf("Pod example-xxxxx not found in default namespace\n")
        } else if statusError, isStatus := err.(*errors.StatusError); isStatus {
            fmt.Printf("Error getting pod %v\n", statusError.ErrStatus.Message)
        } else if err != nil {
            panic(err.Error())
        } else {
            fmt.Printf("Found example-xxxxx pod in default namespace\n")
        }

        time.Sleep(10 * time.Second)
    }
}

如何退出ClusterConfig?并在go程序中列出pod部署。如何获取OutClusterConfig?并在go程序中列出pod部署。