-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
75 lines (64 loc) · 1.59 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"log"
"os"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
// This program lists the pods in a cluster equivalent to
//
// kubectl get pods
//
func main() {
rNs := os.Args[1]
if rNs == "" {
fmt.Println("no namespace requested")
}
// Bootstrap k8s configuration from local Kubernetes config file
//kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config")
kubeconfig := os.Getenv("KUBECONFIG")
log.Println("Using kubeconfig file: ", kubeconfig)
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig)
if err != nil {
log.Fatal(err)
}
// Create an rest client not targeting specific API version
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatal(err)
}
//create namespace
nsNew, err := clientset.CoreV1().Namespaces().Create(requestNamepace(rNs))
if err != nil {
if errors.IsAlreadyExists(err) {
fmt.Println("namespace exist", rNs)
} else {
log.Fatalln("error creating namespace: ", err)
}
} else {
fmt.Println("namespace created", nsNew.Name)
}
//get all namespaces
allns, err := clientset.CoreV1().Namespaces().List(metav1.ListOptions{})
if err != nil {
log.Fatalln("failed to get namespace:", err)
}
//show namespaces
for n, nns := range allns.Items {
fmt.Printf("[%d] %s\n", n, nns.Name)
}
}
func requestNamepace(namepace string) *v1.Namespace {
if namepace == "" {
return nil
}
return &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namepace,
},
}
}