-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkube.go
86 lines (75 loc) · 1.84 KB
/
kube.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
package main
import (
"fmt"
"gopkg.in/yaml.v2"
)
type KubeConfig struct {
Kind string `yaml:"kind"`
Preferences Preferences `yaml:"preferences"`
CurrentContext string `yaml:"current-context"`
Contexts []ContextElement `yaml:"contexts"`
Clusters []ClusterElement `yaml:"clusters"`
APIVersion string `yaml:"apiVersion"`
Users []UserElement `yaml:"users"`
}
type ClusterElement struct {
Cluster ClusterCluster `yaml:"cluster"`
Name string `yaml:"name"`
}
type ClusterCluster struct {
CertificateAuthorityData string `yaml:"certificate-authority-data"`
Server string `yaml:"server"`
}
type ContextElement struct {
Name string `yaml:"name"`
Context ContextContext `yaml:"context"`
}
type ContextContext struct {
Cluster string `yaml:"cluster"`
User string `yaml:"user"`
}
type Preferences struct {
}
type UserElement struct {
Name string `yaml:"name"`
User UserUser `yaml:"user"`
}
type UserUser struct {
Token string `yaml:"token"`
}
func genKubeConfig(token string, clusterResponse *ClusterResponse) ([]byte, error) {
name := fmt.Sprintf("gke_%s_%s_%s", *projectID, *locationID, *clusterID)
kc := &KubeConfig{
Kind: "Config",
Preferences: Preferences{},
CurrentContext: name,
Contexts: []ContextElement{
{
Name: name,
Context: ContextContext{
Cluster: name,
User: name,
},
},
},
Clusters: []ClusterElement{
{
Cluster: ClusterCluster{
CertificateAuthorityData: clusterResponse.MasterAuth.ClusterCACertificate,
Server: "https://" + clusterResponse.Endpoint,
},
Name: name,
},
},
APIVersion: "v1",
Users: []UserElement{
{
Name: name,
User: UserUser{
Token: token,
},
},
},
}
return yaml.Marshal(kc)
}