How to create a simple client application with the Kubernetes Go library?

I am struggling with the Kubernetes Go library. Documents - at least the ones I found - become outdated with the library itself. The above example is not built due to import problems. I'm just trying to make something simple: get the service object by name and print some attributes (e.g. nodePort). I just need a simple example of using the library to make me move.

I could easily do this using the RESTful API, but it seems like reinventing the wheel.

+8
go kubernetes
source share
3 answers

So, after a little experiment and a hint of the k8s Slack channel, I have this example. Perhaps someone can update the example using the correct 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) } } 
+5
source share

The client comes from the kubernet, this can be done as follows:

 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) } } } 
+3
source share

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

If you are in a 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 are outside 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 spoke in detail about this in the message.

+2
source share

All Articles