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

问题描述 投票:0回答:1

我想运行Go K8S客户端库并使用kubeconfig连接到集群,kubeconfig在我的mac上本地定义,位于qazxsw poi下

错误是:

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

我试图像Qazxswpoi一样在Goland IDE上提供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 } 的路径但没有成功,我在这里缺少什么?

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

如果我可以在代码中使用显式路径这么好...

go kubernetes
1个回答
0
投票

确保你按照这里描述的顺序执行依赖项安装步骤:arguments这为我解决了这个问题。

/Users/i0334456/.kube/config中也提到过

© www.soinside.com 2019 - 2024. All rights reserved.