-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
157 lines (140 loc) · 4.43 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
b64 "encoding/base64"
"flag"
"fmt"
"path/filepath"
"gopkg.in/AlecAivazis/survey.v1"
//apiv1 "k8s.io/api/core/v1"
//rbacv1 "k8s.io/api/rbac/v1"
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"
)
// Forming questions
// the questions to ask
var qs = []*survey.Question{
{
Name: "clustername",
Prompt: &survey.Input{Message: "Enter name of cluster", Help: "This will be used as current context in kubeconfig file"},
},
}
func main() {
fmt.Println("\n\t " +
`888 d8P .d8888b. .d8888b.
888 d8P d88P Y88b d88P Y88b
888 d8P 888 888 888 888
888d88K 888 888
8888888b 888 888 88888
888 Y88b 888 888 888 888
888 Y88b Y88b d88P Y88b d88P
888 Y88b "Y8888P" "Y8888P88 `)
fmt.Println("\t " +
` _
| |_ _ ._ ._ _ _|_ _ _ _ _ ._ _|_ o _ _ _ ._ _ ._ _. _|_ _ ._
|< |_| |_) (/_ | | | (/_ |_ (/_ _> (_ (_) | | | | (_| (_| (/_ | | (/_ | (_| |_ (_) |
_| _| ` + "\n")
// the answers will be written to this struct
answers := struct {
Clustername string
}{}
// ask questions
err := survey.Ask(qs, &answers)
if err != nil {
fmt.Println(err.Error())
return
}
// user input is stored in these variables
var namespace string
var clustername string
clustername = answers.Clustername
// Read kubeconfig file on this runtime by default look sunder ~/.kube for config file
var kubeconfig *string
if home := homedir.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()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err)
}
clusterurl := config.Host
// Ask for confirmtation to proceed in creating kubeconfig file
var proceed bool = false
prompt := &survey.Confirm{
Message: "You are going to create a kubeconfig file for cluster: " + clusterurl + "\n", Default: true,
}
survey.AskOne(prompt, &proceed, nil)
if proceed {
var nsusrlist []string
api := clientset.CoreV1()
nsinterface := api.Namespaces()
nslist, err := nsinterface.List(metav1.ListOptions{})
if err != nil {
panic(err)
}
for _, nsitem := range nslist.Items {
//fmt.Println(nsitem.ObjectMeta.Name)
nsusrlist = append(nsusrlist, nsitem.ObjectMeta.Name)
}
selectedns := ""
prompt := &survey.Select{
Message: "Select one of the follwing namespaces of this cluster",
Options: nsusrlist,
}
survey.AskOne(prompt, &selectedns, nil)
namespace = selectedns
fmt.Println("selected naemspace is ", namespace)
// Get secret name
fmt.Printf("Getting credentials needed for kubeconfig \n")
var sausrlist []string
sainterface := api.ServiceAccounts(namespace)
salist, err := sainterface.List(metav1.ListOptions{})
if err != nil {
panic(err)
}
for _, saitem := range salist.Items {
sausrlist = append(sausrlist, saitem.ObjectMeta.Name)
}
saname := ""
prompt = &survey.Select{
Message: "Select one of the follwing service accounts of this cluster",
Options: sausrlist,
}
survey.AskOne(prompt, &saname, nil)
sa, err := api.ServiceAccounts(namespace).Get(saname, metav1.GetOptions{})
if err != nil {
panic(err)
}
secret := string(sa.Secrets[0].Name)
if err != nil {
fmt.Println(err)
}
fmt.Println("got secret name")
// Get ca data from secret
fmt.Println("Getting certificate authority data")
ca, err := api.Secrets(namespace).Get(secret, metav1.GetOptions{})
if err != nil {
panic(err)
}
scrt := string(ca.Data["ca.crt"])
sEnc := b64.StdEncoding.EncodeToString([]byte(scrt))
if err != nil {
fmt.Println(err)
}
fmt.Println("got certificate authority data")
// Get token value from secret
fmt.Printf("Getting token \n")
token := string(ca.Data["token"])
fmt.Println("got token")
// calling function that generates kubeconfig yaml file
genconfig(clustername, sEnc, clusterurl, namespace, saname, token)
}
}