如何使用Kubernetes Go库创建简单的客户端应用程序? [英] How can I create a simple client app with the Kubernetes Go library?
问题描述
我可以使用RESTful API轻松完成此操作,但感觉就像重新发明了轮子。 p>
所以经过一些实验和k8s Slack频道的提示后,我就有了这个例子。也许有人可以用适当的导入路径更新示例。
包主
导入(
fmt
log
github.com/kubernetes/kubernetes/pkg/api
客户端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(无法连接到Kubernetes API:,err)
}
$ bs,err:= c.Services(api.NamespaceDefault).Get(some-service-name)
if err!= nil {
log.Fatalln(Can not 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)
}
}
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.
I could easily do this using the RESTful API but that feels like re-inventing the wheel.
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屋!