forked from kubernetes-retired/bootkube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
k8s.go
168 lines (147 loc) · 5.41 KB
/
k8s.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
158
159
160
161
162
163
164
165
166
167
168
package asset
import (
"bytes"
"encoding/base64"
"path/filepath"
"text/template"
"github.com/ghodss/yaml"
"github.com/kubernetes-incubator/bootkube/pkg/asset/internal"
)
const (
secretNamespace = "kube-system"
secretAPIServerName = "kube-apiserver"
secretCMName = "kube-controller-manager"
)
func newStaticAssets() Assets {
var noData interface{}
assets := Assets{
mustCreateAssetFromTemplate(AssetPathScheduler, internal.SchedulerTemplate, noData),
mustCreateAssetFromTemplate(AssetPathSchedulerDisruption, internal.SchedulerDisruptionTemplate, noData),
mustCreateAssetFromTemplate(AssetPathControllerManagerDisruption, internal.ControllerManagerDisruptionTemplate, noData),
mustCreateAssetFromTemplate(AssetPathKubeDNSDeployment, internal.DNSDeploymentTemplate, noData),
mustCreateAssetFromTemplate(AssetPathCheckpointer, internal.CheckpointerTemplate, noData),
mustCreateAssetFromTemplate(AssetPathKubeFlannel, internal.KubeFlannelTemplate, noData),
mustCreateAssetFromTemplate(AssetPathKubeSystemSARoleBinding, internal.KubeSystemSARoleBindingTemplate, noData),
}
return assets
}
func newDynamicAssets(conf Config) Assets {
assets := Assets{
mustCreateAssetFromTemplate(AssetPathControllerManager, internal.ControllerManagerTemplate, conf),
mustCreateAssetFromTemplate(AssetPathAPIServer, internal.APIServerTemplate, conf),
mustCreateAssetFromTemplate(AssetPathProxy, internal.ProxyTemplate, conf),
mustCreateAssetFromTemplate(AssetPathKubeFlannelCfg, internal.KubeFlannelCfgTemplate, conf),
mustCreateAssetFromTemplate(AssetPathKubeDNSSvc, internal.DNSSvcTemplate, conf),
mustCreateAssetFromTemplate(AssetPathBootstrapAPIServer, internal.BootstrapAPIServerTemplate, conf),
mustCreateAssetFromTemplate(AssetPathBootstrapControllerManager, internal.BootstrapControllerManagerTemplate, conf),
mustCreateAssetFromTemplate(AssetPathBootstrapScheduler, internal.BootstrapSchedulerTemplate, conf),
}
if conf.SelfHostKubelet {
assets = append(assets, mustCreateAssetFromTemplate(AssetPathKubelet, internal.KubeletTemplate, conf))
}
if conf.SelfHostedEtcd {
assets = append(assets,
mustCreateAssetFromTemplate(AssetPathEtcdOperator, internal.EtcdOperatorTemplate, conf),
mustCreateAssetFromTemplate(AssetPathEtcdSvc, internal.EtcdSvcTemplate, conf),
mustCreateAssetFromTemplate(AssetPathKenc, internal.KencTemplate, conf),
mustCreateAssetFromTemplate(AssetPathBootstrapEtcd, internal.BootstrapEtcdTemplate, conf),
)
}
return assets
}
func newKubeConfigAsset(assets Assets, conf Config) (Asset, error) {
caCert, err := assets.Get(AssetPathCACert)
if err != nil {
return Asset{}, err
}
kubeletCert, err := assets.Get(AssetPathKubeletCert)
if err != nil {
return Asset{}, err
}
kubeletKey, err := assets.Get(AssetPathKubeletKey)
if err != nil {
return Asset{}, err
}
type templateCfg struct {
Server string
CACert string
KubeletCert string
KubeletKey string
}
return assetFromTemplate(AssetPathKubeConfig, internal.KubeConfigTemplate, templateCfg{
Server: conf.APIServers[0].String(),
CACert: base64.StdEncoding.EncodeToString(caCert.Data),
KubeletCert: base64.StdEncoding.EncodeToString(kubeletCert.Data),
KubeletKey: base64.StdEncoding.EncodeToString(kubeletKey.Data),
})
}
func newAPIServerSecretAsset(assets Assets) (Asset, error) {
secretAssets := []string{
AssetPathAPIServerKey,
AssetPathAPIServerCert,
AssetPathServiceAccountPubKey,
AssetPathCACert,
}
secretYAML, err := secretFromAssets(secretAPIServerName, secretNamespace, secretAssets, assets)
if err != nil {
return Asset{}, err
}
return Asset{Name: AssetPathAPIServerSecret, Data: secretYAML}, nil
}
func newControllerManagerSecretAsset(assets Assets) (Asset, error) {
secretAssets := []string{
AssetPathServiceAccountPrivKey,
AssetPathCACert, //TODO(aaron): do we want this also distributed as secret? or expect available on host?
}
secretYAML, err := secretFromAssets(secretCMName, secretNamespace, secretAssets, assets)
if err != nil {
return Asset{}, err
}
return Asset{Name: AssetPathControllerManagerSecret, Data: secretYAML}, nil
}
// TODO(aaron): use actual secret object (need to wrap in apiversion/type)
type secret struct {
ApiVersion string `json:"apiVersion"`
Kind string `json:"kind"`
Metadata map[string]string `json:"metadata"`
Type string `json:"type"`
Data map[string]string `json:"data"`
}
func secretFromAssets(name, namespace string, assetNames []string, assets Assets) ([]byte, error) {
data := make(map[string]string)
for _, an := range assetNames {
a, err := assets.Get(an)
if err != nil {
return []byte{}, err
}
data[filepath.Base(a.Name)] = base64.StdEncoding.EncodeToString(a.Data)
}
return yaml.Marshal(secret{
ApiVersion: "v1",
Kind: "Secret",
Type: "Opaque",
Metadata: map[string]string{
"name": name,
"namespace": namespace,
},
Data: data,
})
}
func mustCreateAssetFromTemplate(name string, template []byte, data interface{}) Asset {
a, err := assetFromTemplate(name, template, data)
if err != nil {
panic(err)
}
return a
}
func assetFromTemplate(name string, tb []byte, data interface{}) (Asset, error) {
tmpl, err := template.New(name).Parse(string(tb))
if err != nil {
return Asset{}, err
}
var buf bytes.Buffer
if err := tmpl.Execute(&buf, data); err != nil {
return Asset{}, err
}
return Asset{Name: name, Data: buf.Bytes()}, nil
}