-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprovision.go
100 lines (86 loc) · 2.6 KB
/
provision.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package main
import (
"fmt"
"os"
"path/filepath"
flags "github.com/jessevdk/go-flags"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)
var options struct {
Verbose []bool `short:"v" long:"verbose" description:"Show verbose debug information"`
Namespace string `short:"n" long:"namespace" description:"Namespace to create" required:"true"`
Kubeconfig string `short:"k" long:"kubeconfig" description:"absolute path to the kubeconfig file (default: ~/.kube/config)"`
Username string `short:"u" long:"username" description:"User to add to namespace" required:"true"`
}
func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}
func createNamespace(clientset *kubernetes.Clientset) {
nsSpec := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: options.Namespace}}
_, err := clientset.CoreV1().Namespaces().Create(nsSpec)
if err != nil {
panic(err.Error())
}
fmt.Printf("Created namespace %s\n", options.Namespace)
}
func createRoleBinding(clientset *kubernetes.Clientset) {
rbSpec := &rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: options.Username + "-rolebinding",
Namespace: options.Namespace,
},
Subjects: []rbacv1.Subject{
rbacv1.Subject{
Name: options.Username,
Kind: "User",
},
},
RoleRef: rbacv1.RoleRef{
Kind: "ClusterRole",
Name: "admin",
},
}
_, err := clientset.RbacV1().RoleBindings(options.Namespace).Create(rbSpec)
if err != nil {
panic(err.Error())
}
fmt.Printf("Created rolebinding %s-rolebinding in namespace %s\n", options.Username, options.Namespace)
}
func main() {
parser := flags.NewParser(&options, flags.Default)
_, err := parser.Parse()
if err != nil {
parser.WriteHelp(os.Stderr)
os.Exit(1)
}
if options.Kubeconfig == "" {
home := homeDir()
options.Kubeconfig = filepath.Join(home, ".kube", "config")
}
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", options.Kubeconfig)
if err != nil {
panic(err.Error())
}
// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
_, err = clientset.CoreV1().Namespaces().Get(options.Namespace, metav1.GetOptions{})
if err != nil {
fmt.Printf("Namespace %s not found, creating...\n", options.Namespace)
createNamespace(clientset)
} else {
fmt.Printf("Namespace already exists, creating rolebinding...\n")
}
createRoleBinding(clientset)
}