在本地使用 K8s Go-Client 连接到集群



我想运行 Go K8S 客户端库并使用 kubeconfig 连接到集群,该集群在我的本地定义Mac 在 /Users/i0334456/.kube/config

错误是:

# k8s-go-client/vendor/k8s.io/client-go/rest
vendor/k8s.io/client-go/rest/request.go:598:31: not enough arguments in call to watch.NewStreamWatcher
have (*versioned.Decoder)
want (watch.Decoder, watch.Reporter)

这是我尝试使用的程序

package main
import (
   "flag"
   "fmt"
   "os"
   "path/filepath"
   "time"
   "k8s.io/apimachinery/pkg/api/errors"
   metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
   "k8s.io/client-go/kubernetes"
   "k8s.io/client-go/tools/clientcmd"
)
func main() {
   var kubeconfig *string
   if home := 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(metav1.ListOptions{})
      if err != nil {
         panic(err.Error())
      }
      fmt.Printf("There are %d pods in the clustern", len(pods.Items))
      // Examples for error handling:
      // - Use helper functions like e.g. errors.IsNotFound()
      // - And/or cast to StatusError and use its properties like e.g. ErrStatus.Message
      namespace := "default"
      pod := "example-xxxxx"
      _, err = clientset.CoreV1().Pods(namespace).Get(pod, metav1.GetOptions{})
      if errors.IsNotFound(err) {
         fmt.Printf("Pod %s in namespace %s not foundn", pod, namespace)
      } else if statusError, isStatus := err.(*errors.StatusError); isStatus {
         fmt.Printf("Error getting pod %s in namespace %s: %vn",
            pod, namespace, statusError.ErrStatus.Message)
      } else if err != nil {
         panic(err.Error())
      } else {
         fmt.Printf("Found pod %s in namespace %sn", pod, namespace)
      }
      time.Sleep(10 * time.Second)
   }
}
func homeDir() string {
   if h := os.Getenv("HOME"); h != "" {
      return h
   }
   return os.Getenv("USERPROFILE") // windows
}

我试图在戈兰 IDE 上提供我作为arguments的道路就像/Users/i0334456/.kube/config没有成功,我在这里错过了什么?

我想通过 K8S 的 Go 客户端通过 API 访问集群

如果我可以使用代码中显式的路径,那也很好......

请确保按照如下所述运行依赖项安装步骤:https://github.com/kubernetes/client-go/blob/master/INSTALL.md#go-modules这为我解决了这个问题。

在 https://github.com/kubernetes/client-go/issues/584 中也提到

相关内容

  • 没有找到相关文章