如何使用 Kubernetes Go 库创建一个简单的客户端应用程序? [英] How can I create a simple client app with the Kubernetes Go library?
问题描述
我正在努力使用 Kubernetes Go 库.文档——至少是我发现的——出现了与图书馆本身的最新日期.由于导入问题,提供的示例无法构建.我只是想做一些简单的事情:按名称获取 Service 对象并打印一些属性(如 nodePort).我只需要一个简单的库使用示例就可以了.
I'm struggling with the Kubernetes Go library. The docs--at least the ones I found--appear out-of-date with the library itself. The example provided does not build because of issues with the imports. I'm just trying to do something simple: get a Service object by name and print some attributes (like nodePort). I just need a simple example of library usage to get me going.
我可以使用 RESTful API 轻松做到这一点,但这感觉就像是在重新发明轮子.
I could easily do this using the RESTful API but that feels like re-inventing the wheel.
推荐答案
所以经过一些实验和来自 k8s Slack 频道的提示,我有了这个例子.也许有人可以使用正确的导入路径更新示例.
So after a little experimentation and a hint from the k8s Slack channel, I have this example. Perhaps someone can update the example with a proper import path.
package main
import (
"fmt"
"log"
"github.com/kubernetes/kubernetes/pkg/api"
client "github.com/kubernetes/kubernetes/pkg/client/unversioned"
)
func main() {
config := client.Config{
Host: "http://my-kube-api-server.me:8080",
}
c, err := client.New(&config)
if err != nil {
log.Fatalln("Can't connect to Kubernetes API:", err)
}
s, err := c.Services(api.NamespaceDefault).Get("some-service-name")
if err != nil {
log.Fatalln("Can't get service:", err)
}
fmt.Println("Name:", s.Name)
for p, _ := range s.Spec.Ports {
fmt.Println("Port:", s.Spec.Ports[p].Port)
fmt.Println("NodePort:", s.Spec.Ports[p].NodePort)
}
}
这篇关于如何使用 Kubernetes Go 库创建一个简单的客户端应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!