Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

go

kubernetes

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.

like image 269
Chris Snell Avatar asked Sep 13 '15 21:09

Chris Snell


3 Answers

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)
    }
}
like image 56
Chris Snell Avatar answered Nov 19 '22 23:11

Chris Snell


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.

like image 5
Rush Avatar answered Nov 19 '22 23:11

Rush


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)
        }
    }
}
like image 3
Ramin Mir. Avatar answered Nov 19 '22 23:11

Ramin Mir.