英文:
Golang code to get all the pod details on my setup
问题
我有一个正在运行的 k8s 设置。当我运行 "curl http://
我看到了一些类似的代码 "https://stackoverflow.com/questions/32554893/how-can-i-create-a-simple-client-app-with-the-kubernetes-go-library",但这对我帮助不大。
英文:
I have a setup where k8s is running. When i run the "curl http://< host-ip>/api/v1/pods" i get all the pod details running on my setup. I need to do something similar using the k8s client instead, using the token/certificate authentication.
I did see some code which is similar "https://stackoverflow.com/questions/32554893/how-can-i-create-a-simple-client-app-with-the-kubernetes-go-library" but this is not helping me much.
答案1
得分: 1
你可以使用以下代码创建你的客户端:
rest, err := clientcmd.BuildConfigFromFlags("", "kube-config-file")
if err != nil {
log.Fatal("Failed to load KubeConfig", err)
}
client := clientset.NewForConfigOrDie(rest)
pods, err := client.Core().Pods("").List(api.ListOptions{})
如果你在k8s集群内运行此代码,可以使用以下方式:
rest, err := clientcmd.BuildConfigFromFlags("", "")
英文:
you can create your client using
rest, err := clientcmd.BuildConfigFromFlags("", "kube-config-file")
if err != nil {
log.Fatal("Failed to load KubeConfig", err)
}
client := clientset.NewForConfigOrDie(rest)
pods, err := client.Core().Pods("").List(api.ListOptions{})
if you running this inside the k8s cluster you can do it by
rest, err := clientcmd.BuildConfigFromFlags("", "")
答案2
得分: 0
这是要翻译的内容:
众所周知,官方的k8s Go客户端有点混乱。我建议你看看这个库,看起来好得多:
https://amdatu.org/infra/goclient/gettingstarted/
godocs文档:
https://godoc.org/bitbucket.org/amdatulabs/amdatu-kubernetes-go/client
英文:
It's well know that the official Go client for k8s is a little messy.
I recommend you take a look to this library looks much better:
https://amdatu.org/infra/goclient/gettingstarted/
godocs:
https://godoc.org/bitbucket.org/amdatulabs/amdatu-kubernetes-go/client
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论