-
Notifications
You must be signed in to change notification settings - Fork 12
/
rke.go
305 lines (283 loc) · 7.18 KB
/
rke.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package ranchhand
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"io/ioutil"
"os"
"os/exec"
"text/template"
"time"
"github.com/pkg/errors"
)
const (
RKEKubeConfig = "kube_config_rancher-cluster.yml"
RKEConfigFile = "rancher-cluster.yml"
RKETemplate = `# DO NOT EDIT THIS FILE - GENERATED BY RANCHHAND
ssh_key_path: {{ .SSH.KeyPath }}
ignore_docker_version: false
nodes:
{{- range .Nodes }}
- address: {{ .PublicIP }}
{{- with .PrivateIP }}
internal_address: {{ . }}
{{- end }}
user: {{ $.SSH.User }}
port: {{ $.SSH.Port }}
role: [controlplane,worker,etcd]
{{- end }}
services:
etcd:
snapshot: true
creation: 6h
retention: 24h
kube-api:
pod_security_policy: true
extra_args:
anonymous-auth: "false"
profiling: "false"
service-account-lookup: "true"
enable-admission-plugins: "ServiceAccount,NamespaceLifecycle,LimitRanger,PersistentVolumeLabel,DefaultStorageClass,ResourceQuota,DefaultTolerationSeconds,AlwaysPullImages,DenyEscalatingExec,NodeRestriction,EventRateLimit,PodSecurityPolicy"
admission-control-config-file: "{{ .AdmissionControlConfigFile }}"
audit-log-path: "/var/log/kube-audit/audit-log.json"
audit-log-maxage: "30"
audit-log-maxbackup: "10"
audit-log-maxsize: "100"
audit-log-format: "json"
audit-policy-file: "{{ .AuditPolicyFile }}"
extra_binds:
- "/var/log/kube-audit:/var/log/kube-audit"
kube-controller:
extra_args:
profiling: "false"
address: "127.0.0.1"
terminated-pod-gc-threshold: "1000"
kubelet:
extra_args:
streaming-connection-idle-timeout: "30m"
protect-kernel-defaults: "false"
make-iptables-util-chains: "true"
event-qps: "0"
scheduler:
extra_args:
profiling: "false"
address: "127.0.0.1"
ingress:
provider: nginx
extra_args:
default-ssl-certificate: ingress-nginx/ingress-default-cert
addons: |
apiVersion: v1
kind: Secret
metadata:
name: ingress-default-cert
namespace: ingress-nginx
type: kubernetes.io/tls
data:
tls.crt: {{ .CertPEM | base64Encode }}
tls.key: {{ .KeyPEM | base64Encode }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: default-psp-role
namespace: ingress-nginx
rules:
- apiGroups:
- extensions
resourceNames:
- default-psp
resources:
- podsecuritypolicies
verbs:
- use
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: default-psp-rolebinding
namespace: ingress-nginx
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: default-psp-role
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: Group
name: system:serviceaccounts
- apiGroup: rbac.authorization.k8s.io
kind: Group
name: system:authenticated
---
apiVersion: v1
kind: Namespace
metadata:
name: cattle-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: default-psp-role
namespace: cattle-system
rules:
- apiGroups:
- extensions
resourceNames:
- default-psp
resources:
- podsecuritypolicies
verbs:
- use
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: default-psp-rolebinding
namespace: cattle-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: default-psp-role
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: Group
name: system:serviceaccounts
- apiGroup: rbac.authorization.k8s.io
kind: Group
name: system:authenticated
---
apiVersion: extensions/v1beta1
kind: PodSecurityPolicy
metadata:
name: restricted
spec:
requiredDropCapabilities:
- NET_RAW
privileged: false
allowPrivilegeEscalation: false
defaultAllowPrivilegeEscalation: false
fsGroup:
rule: RunAsAny
runAsUser:
rule: MustRunAsNonRoot
seLinux:
rule: RunAsAny
supplementalGroups:
rule: RunAsAny
volumes:
- emptyDir
- secret
- persistentVolumeClaim
- downwardAPI
- configMap
- projected
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: psp:restricted
rules:
- apiGroups:
- extensions
resourceNames:
- restricted
resources:
- podsecuritypolicies
verbs:
- use
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: psp:restricted
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: psp:restricted
subjects:
- apiGroup: rbac.authorization.k8s.io
kind: Group
name: system:serviceaccounts
- apiGroup: rbac.authorization.k8s.io
kind: Group
name: system:authenticated
`
)
var tpl *template.Template
type tmplData struct {
*Config
CertPEM, KeyPEM []byte
AdmissionControlConfigFile, AuditPolicyFile string
}
func launchRKE(cfg *Config, certPEM, keyPEM []byte) error {
// render config template
var buf bytes.Buffer
tplData := tmplData{
Config: cfg,
CertPEM: certPEM,
KeyPEM: keyPEM,
AdmissionControlConfigFile: k8sConfigs["admission"].filename,
AuditPolicyFile: k8sConfigs["audit"].filename,
}
if err := tpl.Execute(&buf, tplData); err != nil {
return errors.Wrap(err, "rke template render failed")
}
tplContents := buf.Bytes()
// decided if file needs to be [over]written
var writeConfig, saveState bool
_, err := os.Stat(RKEConfigFile)
switch {
case os.IsNotExist(err):
writeConfig = true
case cfg.UpgradeKubernetes:
configContents, err := ioutil.ReadFile(RKEConfigFile)
if err != nil {
return errors.Wrap(err, "rke config read failed")
}
writeConfig = !bytes.Equal(configContents, tplContents)
saveState = writeConfig
}
// write config to file
if writeConfig {
err := ioutil.WriteFile(RKEConfigFile, tplContents, 0644)
if err != nil {
return errors.Wrap(err, "rke config write failed")
}
}
// take a snapshot of etcd state before applying upgrade
if saveState {
snapshotName := fmt.Sprintf("snapshot-%s", time.Now().UTC().Format(time.RFC3339))
cmd := exec.Command("rke", "etcd", "snapshot-save", "--name", snapshotName, "--config", RKEConfigFile)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
log.Info("taking snapshot of etcd state prior to upgrade")
if err := cmd.Run(); err != nil {
return errors.Wrap(cmd.Run(), "cannot snapshot etcd")
}
}
// run `rke up` if file was just written or cluster has not converged
if writeConfig || hasNotConverged() {
cmd := exec.Command("rke", "up", "--config", RKEConfigFile)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
log.Info("launching rke up")
return errors.Wrap(cmd.Run(), "cannot install kubernetes")
}
return nil
}
func hasNotConverged() bool {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "rke", "version", "--config", RKEConfigFile)
return cmd.Run() != nil
}
func init() {
tpl = template.New("rke-tmpl")
tpl.Funcs(template.FuncMap{
"base64Encode": func(bs []byte) string {
return base64.StdEncoding.EncodeToString(bs)
},
})
template.Must(tpl.Parse(RKETemplate))
}