Created
April 21, 2017 09:42
-
-
Save mhausenblas/30e8dc5f7682888c10906cb0f8c471cb to your computer and use it in GitHub Desktop.
A simple Go program for interacting with a Kubernetes cluster
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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