简体   繁体   中英

using k8s go-client locally to connect to cluster

I want to run the Go K8S client library and conect to the cluster with kubeconfig which is locally defined on my mac under /Users/i0334456/.kube/config

The error is:

# 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)

This is the program which I try to use

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
}

I was tried to provide my path as arguments on Goland IDE like /Users/i0334456/.kube/config without success, what am I missing here ?

I want to access via the Go client for K8S to the cluster via API's

if I can use the path explicit in the code this is good either...

make sure you ran through the dependency installation steps as described here: https://github.com/kubernetes/client-go/blob/master/INSTALL.md#go-modules That solved this problem for me.

Also mentioned in https://github.com/kubernetes/client-go/issues/584

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM