在本地使用k8s go-client连接集群

时间:2019-04-29 14:52:51

标签: go kubernetes

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

错误是:

# 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 cluster\n", 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 found\n", pod, namespace)
      } else if statusError, isStatus := err.(*errors.StatusError); isStatus {
         fmt.Printf("Error getting pod %s in namespace %s: %v\n",
            pod, namespace, statusError.ErrStatus.Message)
      } else if err != nil {
         panic(err.Error())
      } else {
         fmt.Printf("Found pod %s in namespace %s\n", pod, namespace)
      }

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

func homeDir() string {
   if h := os.Getenv("HOME"); h != "" {
      return h
   }
   return os.Getenv("USERPROFILE") // windows
}

我试图在Goland IDE上以arguments的形式提供路径 像/Users/i0334456/.kube/config一样没有成功,我在这里想念什么?

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

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

2 个答案:

答案 0 :(得分:1)

确保按照以下说明完成依赖项安装步骤:https://github.com/kubernetes/client-go/blob/master/INSTALL.md#go-modules 那为我解决了这个问题。

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

中提到

答案 1 :(得分:0)

这对我来说就像是一种魅力:

→  ./myk8sclient
There are 12 pods in the cluster
Found pod mynewjobname-lvxgn in namespace default

您可以检查的内容:

  1. 您的HOME目录实际上是:/Users/i0334456/

  2. 您的~/.kube/config文件的正确性。尝试运行:

    kubectl config view --minify
    
  3. 您的配置文件实际上是/Users/i0334456/.kube/config吗?

  4. 您是否有一个KUBECONFIG环境变量覆盖了~/.kube/config文件?