Skip to content

Instantly share code, notes, and snippets.

@vladimirvivien
Last active August 18, 2017 15:56
Show Gist options
  • Save vladimirvivien/9c2e26c1f300e0e31b029d6d34e4fc2e to your computer and use it in GitHub Desktop.
Save vladimirvivien/9c2e26c1f300e0e31b029d6d34e4fc2e to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
// simple k8s client that lists all available pods
// it gets config from $HOME/.kube/config
func main() {
kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config")
fmt.Println("Using kubeconfig: ", kubeconfig)
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
panic(err.Error())
}
// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
pods, err := clientset.CoreV1().Pods("").List(metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
if len(pods.Items) > 0 {
fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
for _, pod := range pods.Items {
fmt.Printf("pod %s\n", pod.GetName())
}
} else {
fmt.Println("No pods found!")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment