Skip to content

Instantly share code, notes, and snippets.

@mhausenblas
Created April 21, 2017 09:42
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhausenblas/30e8dc5f7682888c10906cb0f8c471cb to your computer and use it in GitHub Desktop.
Save mhausenblas/30e8dc5f7682888c10906cb0f8c471cb to your computer and use it in GitHub Desktop.
A simple Go program for interacting with a Kubernetes cluster
package main
import (
"flag"
"fmt"
"github.com/chzyer/readline"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
kubeconfig := flag.String("kubeconfig", "/Users/mhausenblas/.kube/config", "absolute path to the kubeconfig file")
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err.Error())
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
rl, err := readline.New("k8s> ")
if err != nil {
panic(err)
}
defer rl.Close()
for {
line, err := rl.Readline()
if err != nil || line == "exit" {
break
}
if line == "ps" {
pods, err := clientset.CoreV1().Pods("").List(v1.ListOptions{})
if err != nil {
panic(err.Error())
}
for _, pod := range pods.Items {
fmt.Printf("%s %s\n", pod.GetName(), pod.GetCreationTimestamp())
}
} else {
fmt.Printf("unknown command\n")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment