-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
95 lines (75 loc) · 1.79 KB
/
config.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
package utils
import (
"gopkg.in/yaml.v2"
)
type Config struct {
ApiVersion string `yaml:"apiVersion"`
Kind string `yaml:"kind"`
Clusters []Cluster `yaml:"clusters"`
Users []User `yaml:"users"`
Contexts []Context `yaml: "contexts"`
}
// Cluster configs
type Cluster struct {
Name string
ClusterConfig ClusterConfig `yaml:"cluster"`
}
type ClusterConfig struct {
CertificateAuthority string `yaml:"certificate-authority"`
Server string
}
// User configs
type User struct {
Name string `yaml:"name"`
UserCred UserCred `yaml:"user"`
}
type UserCred struct {
ClientCertificate string `yaml:"client-certificate"`
ClientKey string `yaml:"client-key"`
}
// Context configs
type Context struct {
Name string
ContextConfig ContextConfig `yaml:"context,omitempty"`
}
type ContextConfig struct {
ClusterName string `yaml:cluster`
NameSpace string
User string
}
func (c *Config) Create(users []string, clusters []string, contexts []string) {
c.ApiVersion = "v1"
c.Kind = "Config"
// Populating cluster, context and user objects
userConfigs := make([]User, len(users))
for i, user := range users {
config := User{
Name: user,
}
userConfigs[i] = config
}
clusterConfigs := make([]Cluster, len(clusters))
for i, cluster := range clusters {
config := Cluster{
Name: cluster,
}
clusterConfigs[i] = config
}
contextConfigs := make([]Context, len(contexts))
for i, context := range contexts {
config := Context{
Name: context,
}
contextConfigs[i] = config
}
c.Users = userConfigs
c.Contexts = contextConfigs
c.Clusters = clusterConfigs
}
func (c *Config) Parse() ([]byte, error) {
configContents, err := yaml.Marshal(c)
if err != nil {
return make([]byte, 0), err
}
return configContents, nil
}