using k8s go-client locally to connect to cluster

4/29/2019

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...

-- Jenny M
go
kubernetes

1 Answer

6/26/2019

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

-- ouven
Source: StackOverflow