-
Notifications
You must be signed in to change notification settings - Fork 0
/
kube_api.go
49 lines (40 loc) · 956 Bytes
/
kube_api.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
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"os"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
func getPods(kubeconfig string, namespace string) []string {
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())
}
// fetch current namespace
if len(namespace) == 0 {
namespace, err = extractCurrentNamespaceFromFile(kubeconfig)
if err != nil {
panic(err.Error())
}
}
pods, err := clientset.CoreV1().Pods(namespace).List(metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
var names []string
for _, pod := range pods.Items {
names = append(names, pod.ObjectMeta.Name)
}
return names
}
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}