-
Notifications
You must be signed in to change notification settings - Fork 2
/
filegen.go
82 lines (78 loc) · 2.21 KB
/
filegen.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
package main
import (
"fmt"
"io/ioutil"
"log"
"gopkg.in/yaml.v2"
)
func genconfig(clustername string, cadata string, serverurl string, namespace string, saname string, token string) string {
fmt.Println("Creating kubeconfig file...")
// A strucure to hold output kubeconfig yaml data
type Cluster struct {
CertificateAuthority string `yaml:"certificate-authority-data"`
Server string `yaml:"server"`
}
type Clusters struct {
Cluster Cluster `yaml:"cluster"`
Name string `yaml:"name"`
}
type Context struct {
Cluster string `yaml:"cluster"`
Namespace string `yaml:"namespace"`
User string `yaml:"user"`
}
type Contexts struct {
Context Context `yaml:"context"`
Name string `yaml:"name"`
}
type User struct {
Token string `yaml:"token"`
}
type Users struct {
Name string `yaml:"name"`
User User `yaml:"user"`
}
type AutoGenerated struct {
CurrentContext string `yaml:"current-context"`
APIVersion string `yaml:"apiVersion"`
Clusters []Clusters `yaml:"clusters"`
Contexts []Contexts `yaml:"contexts"`
Kind string `yaml:"kind"`
Users []Users `yaml: "users"`
}
ag := new(AutoGenerated)
ag.APIVersion = "v1"
ag.Kind = "Config"
ag.CurrentContext = clustername
ag.Clusters = make([]Clusters, 0)
cs := Cluster{CertificateAuthority: cadata,
Server: serverurl,
}
css := Clusters{Name: clustername, Cluster: cs}
ag.Clusters = append(ag.Clusters, css)
ag.Contexts = make([]Contexts, 0)
ct := Context{Cluster: clustername,
Namespace: namespace,
User: saname}
cts := Contexts{
Context: ct,
Name: clustername}
ag.Contexts = append(ag.Contexts, cts)
ag.Users = make([]Users, 0)
usr := User{Token: token}
usrs := Users{Name: saname, User: usr}
ag.Users = append(ag.Users, usrs)
d, err := yaml.Marshal(&ag)
if err != nil {
log.Fatalf("yaml.Marshal failed with '%s'\n", err)
}
filename := `output.yaml`
var strconfig string
strconfig = string(d)
err = ioutil.WriteFile(filename, []byte(strconfig), 0644)
if err != nil {
log.Fatalf("writing kubeconfig faile failed '%s'\n", err)
}
fmt.Println("created kubeconfig file called output.yaml in present working directory ✓")
return strconfig
}