How can I create a simple client app with the Kubernetes Go library?

前端 未结 3 830
梦毁少年i
梦毁少年i 2021-02-05 21:23

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

3条回答
  •  面向向阳花
    2021-02-05 21:57

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

提交回复
热议问题