-
Notifications
You must be signed in to change notification settings - Fork 0
/
k8s.go
61 lines (52 loc) · 1.31 KB
/
k8s.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
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"flag"
"os"
"path/filepath"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
type Client struct {
c *kubernetes.Clientset
}
func (client *Client) namespaces() *v1.NamespaceList {
nsList, err := client.c.CoreV1().Namespaces().List(metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
return nsList
}
func (client *Client) delete(ns string) {
err := client.c.CoreV1().Namespaces().Delete(ns, metav1.NewDeleteOptions(0))
if err != nil {
panic(err.Error())
}
}
func NewClient() Client {
var kubeconfig *string
if home := homeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
// use the current context in 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())
}
return Client{clientset}
}
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}