-
Notifications
You must be signed in to change notification settings - Fork 9
/
podlist.go
37 lines (30 loc) · 934 Bytes
/
podlist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package main
import (
"flag"
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"path/filepath"
)
func main() {
var defaultKubeConfigPath string
if home := homedir.HomeDir(); home != "" {
// build kubeconfig path from $HOME dir
defaultKubeConfigPath = filepath.Join(home, ".kube", "config")
}
// set kubeconfig flag
kubeconfig := flag.String("kubeconfig", defaultKubeConfigPath, "kubeconfig config file")
flag.Parse()
// retrieve kubeconfig
config, _ := clientcmd.BuildConfigFromFlags("", *kubeconfig)
// get clientset for kubernetes resources
clientset, _ := kubernetes.NewForConfig(config)
// Get list of pod objects
pods, _ := clientset.CoreV1().Pods("").List(metav1.ListOptions{})
// show pod object to stdout
for i, pod := range pods.Items {
fmt.Printf("[Pod Name %d]%s\n", i, pod.GetName())
}
}