我如何与客户端交互?

问题描述

我的集群有一个基本接口,现在想学习如何执行功能。我从哪里开始阅读?该文档是简约的。例如,如何获取每个 Pod 的名称

这是我如何获得客户端:

package main

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

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

  for {
    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))
    time.Sleep(10 * time.Second)
  }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)