Golang code to get all the pod details on my setup

huangapple go评论89阅读模式
英文:

Golang code to get all the pod details on my setup

问题

我有一个正在运行的 k8s 设置。当我运行 "curl http:///api/v1/pods" 时,我可以获取到在我的设置上运行的所有 pod 的详细信息。我需要使用 k8s 客户端来完成类似的操作,使用令牌/证书进行身份验证。

我看到了一些类似的代码 "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(&quot;&quot;, &quot;kube-config-file&quot;)
if err != nil {
	log.Fatal(&quot;Failed to load KubeConfig&quot;, err)
}


client := clientset.NewForConfigOrDie(rest)

pods, err := client.Core().Pods(&quot;&quot;).List(api.ListOptions{})

if you running this inside the k8s cluster you can do it by

rest, err := clientcmd.BuildConfigFromFlags(&quot;&quot;, &quot;&quot;)

答案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

huangapple
  • 本文由 发表于 2017年3月30日 13:33:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/43109337.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定