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

13,009

Solution 1

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

Solution 2

Here's how to do it with the latest Go client.

If you're inside the k8s cluster:

package main

import (
  "fmt"

  "k8s.io/client-go/1.5/kubernetes"
  "k8s.io/client-go/1.5/pkg/api/v1"
  "k8s.io/client-go/1.5/rest"
)

func main()  {
    config, err = rest.InClusterConfig()
    if err != nil {
      return nil, err
    }

    c, err := kubernetes.NewForConfig(config)
    if err != nil {
      return nil, err
    }

    // Get Pod by name
    pod, err := c.Pods(v1.NamespaceDefault).Get("my-pod")
    if err != nil {
        fmt.Println(err)
        return
    }

    // Print its creation time
    fmt.Println(pod.GetCreationTimestamp())
}

And if you're outside of the cluster:

package main

import (
  "fmt"

  "k8s.io/client-go/1.5/kubernetes"
  "k8s.io/client-go/1.5/pkg/api/v1"
  "k8s.io/client-go/1.5/tools/clientcmd"
)

func main()  {
    config, err := clientcmd.BuildConfigFromFlags("", <kube-config-path>)
    if err != nil {
      return nil, err
    }

    c, err := kubernetes.NewForConfig(config)
    if err != nil {
      return nil, err
    }

    // Get Pod by name
    pod, err := c.Pods(v1.NamespaceDefault).Get("my-pod")
    if err != nil {
        fmt.Println(err)
        return
    }

    // Print its creation time
    fmt.Println(pod.GetCreationTimestamp())
}

I have gone into more detail on this in a blog post.

Solution 3

With kubernetes go client, it could be done this way:

package main

import (
    "flag"
    "fmt"

    "k8s.io/client-go/kubernetes"
    "k8s.io/client-go/pkg/api/v1"
    "k8s.io/client-go/tools/clientcmd"
)

var (
    kubeconfig = flag.String("kubeconfig", "./config", "absolute path to the kubeconfig file")
)

func main() {
    flag.Parse()
    // uses the current context in kubeconfig
    config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
    if err != nil {
        panic(err.Error())
    }
    // creates the clientset
    clientset, err := kubernetes.NewForConfig(config)
    if err != nil {
        panic(err.Error())
    }
    services, err := clientset.Core().Services("").List(v1.ListOptions{})
    if err != nil {
        panic(err.Error())
    }
    fmt.Printf("There are %d pods in the cluster\n", len(services.Items))

    for _, s := range services.Items {
        for p, _ := range s.Spec.Ports {
            fmt.Println("Port:", s.Spec.Ports[p].Port)
            fmt.Println("NodePort:", s.Spec.Ports[p].NodePort)
        }
    }
}
Share:
13,009

Related videos on Youtube

Chris Snell
Author by

Chris Snell

Updated on September 14, 2022

Comments

  • Chris Snell
    Chris Snell about 1 year

    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.

  • Chetan
    Chetan over 6 years
    My code is not able to resolve "c.Pods" in the line pod, err := c.Pods(v1.NamespaceDefault).Get("my-pod") Any idea why?
  • itorres
    itorres over 6 years
    @Chetan this code explicitly uses the 1.5 version of the go-client library with support up to kubernetes-1.4. If you're connecting to a GKE cluster you probably need the 2.0 version to connect to kubernetes 1.5.x. Check the compatibility matrix: github.com/kubernetes/client-go#compatibility-matrix
  • Chetan
    Chetan over 6 years
    My kubernetes is 1.5 and the go-client library is master/HEAD. I also tried the same with go-client 2.0. Also a catch which might have affected is, I was unable to do a "go get k8s.io/client-go/..." So i manually downloaded the client and placed it in the workspace. So i think the dependencies have not resolved. If so, any idea how to resolve them?