forked from Praqma/helmsman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kube_helpers.go
500 lines (412 loc) · 15.3 KB
/
kube_helpers.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
package main
import (
"io/ioutil"
"log"
"strings"
"gopkg.in/yaml.v2"
)
// validateServiceAccount checks if k8s service account exists in a given namespace
// if the provided namespace is empty, it checks in the "default" namespace
func validateServiceAccount(sa string, namespace string) (bool, string) {
if namespace == "" {
namespace = "default"
}
ns := " -n " + namespace
cmd := command{
Cmd: "bash",
Args: []string{"-c", "kubectl get serviceaccount " + sa + ns},
Description: "validating if serviceaccount [ " + sa + " ] exists in namespace [ " + namespace + " ].",
}
if exitCode, err := cmd.exec(debug, verbose); exitCode != 0 {
return false, err
}
return true, ""
}
// createRBAC creates a k8s service account and bind it to a (Cluster)Role
// role can be "cluster-admin" or any other custom name
// It binds it to a new role called "helmsman-tiller"
func createRBAC(sa string, namespace string, role string) (bool, string) {
var ok bool
var err string
if role == "" {
if namespace == "kube-system" {
role = "cluster-admin"
} else {
role = "helmsman-tiller"
}
}
if ok, err = createServiceAccount(sa, namespace); ok {
if role == "cluster-admin" || (role == "" && namespace == "kube-system") {
if ok, err = createRoleBinding(role, sa, namespace); ok {
return true, ""
}
return false, err
}
if ok, err = createRole(namespace, role); ok {
if ok, err = createRoleBinding(role, sa, namespace); ok {
return true, ""
}
return false, err
}
return false, err
}
return false, err
}
// addNamespaces creates a set of namespaces in your k8s cluster.
// If a namespace with the same name exists, it will skip it.
// If --ns-override flag is used, it only creates the provided namespace in that flag
func addNamespaces(namespaces map[string]namespace) {
if nsOverride == "" {
for nsName, ns := range namespaces {
createNamespace(nsName)
labelNamespace(nsName, ns.Labels)
annotateNamespace(nsName, ns.Annotations)
setLimits(nsName, ns.Limits)
}
} else {
createNamespace(nsOverride)
overrideAppsNamespace(nsOverride)
}
}
// overrideAppsNamespace replaces all apps namespaces with one specific namespace
func overrideAppsNamespace(newNs string) {
log.Println("INFO: overriding apps namespaces with [ " + newNs + " ] ...")
for _, r := range s.Apps {
overrideNamespace(r, newNs)
}
}
// createNamespace creates a namespace in the k8s cluster
func createNamespace(ns string) {
cmd := command{
Cmd: "bash",
Args: []string{"-c", "kubectl create namespace " + ns},
Description: "creating namespace " + ns,
}
if exitCode, _ := cmd.exec(debug, verbose); exitCode != 0 {
log.Println("WARN: I could not create namespace [ " +
ns + " ]. It already exists. I am skipping this.")
}
}
// labelNamespace labels a namespace with provided labels
func labelNamespace(ns string, labels map[string]string) {
for k, v := range labels {
cmd := command{
Cmd: "bash",
Args: []string{"-c", "kubectl label --overwrite namespace/" + ns + " " + k + "=" + v},
Description: "labeling namespace " + ns,
}
if exitCode, _ := cmd.exec(debug, verbose); exitCode != 0 {
log.Println("WARN: I could not label namespace [ " + ns + " with " + k + "=" + v +
" ]. It already exists. I am skipping this.")
}
}
}
// annotateNamespace annotates a namespace with provided annotations
func annotateNamespace(ns string, labels map[string]string) {
for k, v := range labels {
cmd := command{
Cmd: "bash",
Args: []string{"-c", "kubectl annotate --overwrite namespace/" + ns + " " + k + "=" + v},
Description: "annotating namespace " + ns,
}
if exitCode, _ := cmd.exec(debug, verbose); exitCode != 0 {
log.Println("WARN: I could not annotate namespace [ " + ns + " with " + k + "=" + v +
" ]. It already exists. I am skipping this.")
}
}
}
// setLimits creates a LimitRange resource in the provided Namespace
func setLimits(ns string, lims limits) {
if len(lims) == 0 {
return
}
definition := `
---
apiVersion: v1
kind: LimitRange
metadata:
name: limit-range
spec:
limits:
`
d, err := yaml.Marshal(&lims)
if err != nil {
logError(err.Error())
}
definition = definition + Indent(string(d), strings.Repeat(" ", 4))
if err := ioutil.WriteFile("temp-LimitRange.yaml", []byte(definition), 0666); err != nil {
logError(err.Error())
}
cmd := command{
Cmd: "bash",
Args: []string{"-c", "kubectl apply -f temp-LimitRange.yaml -n " + ns},
Description: "creating LimitRange in namespace [ " + ns + " ]",
}
exitCode, e := cmd.exec(debug, verbose)
if exitCode != 0 {
logError("ERROR: failed to create LimitRange in namespace [ " + ns + " ]: " + e)
}
deleteFile("temp-LimitRange.yaml")
}
// createContext creates a context -connecting to a k8s cluster- in kubectl config.
// It returns true if successful, false otherwise
func createContext() (bool, string) {
if s.Settings.BearerToken && s.Settings.BearerTokenPath == "" {
log.Println("INFO: creating kube context with bearer token from K8S service account.")
s.Settings.BearerTokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token"
} else if s.Settings.BearerToken && s.Settings.BearerTokenPath != "" {
log.Println("INFO: creating kube context with bearer token from " + s.Settings.BearerTokenPath)
} else if s.Settings.Password == "" || s.Settings.Username == "" || s.Settings.ClusterURI == "" {
return false, "ERROR: missing information to create context [ " + s.Settings.KubeContext + " ] " +
"you are either missing PASSWORD, USERNAME or CLUSTERURI in the Settings section of your desired state file."
} else if !s.Settings.BearerToken && (s.Certificates == nil || s.Certificates["caCrt"] == "" || s.Certificates["caKey"] == "") {
return false, "ERROR: missing information to create context [ " + s.Settings.KubeContext + " ] " +
"you are either missing caCrt or caKey or both in the Certifications section of your desired state file."
} else if s.Settings.BearerToken && (s.Certificates == nil || s.Certificates["caCrt"] == "") {
return false, "ERROR: missing information to create context [ " + s.Settings.KubeContext + " ] " +
"caCrt is missing in the Certifications section of your desired state file."
}
// set certs locations (relative filepath, GCS bucket, AWS bucket)
caCrt := s.Certificates["caCrt"]
caKey := s.Certificates["caKey"]
caClient := s.Certificates["caClient"]
// download certs and keys
// GCS bucket+file format should be: gs://bucket-name/dir.../filename.ext
// S3 bucket+file format should be: s3://bucket-name/dir.../filename.ext
// CA cert
if caCrt != "" {
caCrt = downloadFile(caCrt, "ca.crt")
}
// CA key
if caKey != "" {
caKey = downloadFile(caKey, "ca.key")
}
// client certificate
if caClient != "" {
caClient = downloadFile(caClient, "client.crt")
}
// bearer token
tokenPath := "bearer.token"
if s.Settings.BearerToken && s.Settings.BearerTokenPath != "" {
downloadFile(s.Settings.BearerTokenPath, tokenPath)
}
// connecting to the cluster
setCredentialsCmd := ""
if s.Settings.BearerToken {
token := readFile(tokenPath)
if s.Settings.Username == "" {
s.Settings.Username = "helmsman"
}
setCredentialsCmd = "kubectl config set-credentials " + s.Settings.Username + " --token=" + token
} else {
setCredentialsCmd = "kubectl config set-credentials " + s.Settings.Username + " --username=" + s.Settings.Username +
" --password=" + s.Settings.Password + " --client-key=" + caKey
if caClient != "" {
setCredentialsCmd = setCredentialsCmd + " --client-certificate=" + caClient
}
}
cmd := command{
Cmd: "bash",
Args: []string{"-c", setCredentialsCmd},
Description: "creating kubectl context - setting credentials.",
}
if exitCode, err := cmd.exec(debug, verbose); exitCode != 0 {
return false, "ERROR: failed to create context [ " + s.Settings.KubeContext + " ]: " + err
}
cmd = command{
Cmd: "bash",
Args: []string{"-c", "kubectl config set-cluster " + s.Settings.KubeContext + " --server=" + s.Settings.ClusterURI +
" --certificate-authority=" + caCrt},
Description: "creating kubectl context - setting cluster.",
}
if exitCode, err := cmd.exec(debug, verbose); exitCode != 0 {
return false, "ERROR: failed to create context [ " + s.Settings.KubeContext + " ]: " + err
}
cmd = command{
Cmd: "bash",
Args: []string{"-c", "kubectl config set-context " + s.Settings.KubeContext + " --cluster=" + s.Settings.KubeContext +
" --user=" + s.Settings.Username},
Description: "creating kubectl context - setting context.",
}
if exitCode, err := cmd.exec(debug, verbose); exitCode != 0 {
return false, "ERROR: failed to create context [ " + s.Settings.KubeContext + " ]: " + err
}
if setKubeContext(s.Settings.KubeContext) {
return true, ""
}
return false, "ERROR: something went wrong while setting the kube context to the newly created one."
}
// setKubeContext sets your kubectl context to the one specified in the desired state file.
// It returns false if it fails to set the context. This means the context does not exist.
func setKubeContext(context string) bool {
if context == "" {
return getKubeContext()
}
cmd := command{
Cmd: "bash",
Args: []string{"-c", "kubectl config use-context " + context},
Description: "setting kubectl context to [ " + context + " ]",
}
exitCode, _ := cmd.exec(debug, verbose)
if exitCode != 0 {
log.Println("INFO: KubeContext: " + context + " does not exist. I will try to create it.")
return false
}
return true
}
// getKubeContext gets your kubectl context.
// It returns false if no context is set.
func getKubeContext() bool {
cmd := command{
Cmd: "bash",
Args: []string{"-c", "kubectl config current-context"},
Description: "getting kubectl context",
}
exitCode, result := cmd.exec(debug, verbose)
if exitCode != 0 || result == "" {
log.Println("INFO: Kubectl context is not set")
return false
}
return true
}
// createServiceAccount creates a service account in a given namespace and associates it with a cluster-admin role
func createServiceAccount(saName string, namespace string) (bool, string) {
cmd := command{
Cmd: "bash",
Args: []string{"-c", "kubectl create serviceaccount -n " + namespace + " " + saName},
Description: "creating service account [ " + saName + " ] in namespace [ " + namespace + " ]",
}
exitCode, err := cmd.exec(debug, verbose)
if exitCode != 0 {
//logError("ERROR: failed to create service account " + saName + " in namespace [ " + namespace + " ]: " + err)
return false, err
}
return true, ""
}
// createRoleBinding creates a role binding in a given namespace for a service account with a cluster-role/role in the cluster.
func createRoleBinding(role string, saName string, namespace string) (bool, string) {
clusterRole := false
resource := "rolebinding"
if role == "cluster-admin" {
clusterRole = true
resource = "clusterrolebinding"
}
bindingName := saName + "-binding"
bindingOption := "--role=" + role
if clusterRole {
bindingOption = "--clusterrole=" + role
bindingName = namespace + ":" + saName + "-binding"
}
log.Println("INFO: creating " + resource + " for service account [ " + saName + " ] in namespace [ " + namespace + " ] with role: " + role + ".")
cmd := command{
Cmd: "bash",
Args: []string{"-c", "kubectl create " + resource + " " + bindingName + " " + bindingOption + " --serviceaccount " + namespace + ":" + saName + " -n " + namespace},
Description: "creating " + resource + " for service account [ " + saName + " ] in namespace [ " + namespace + " ] with role: " + role,
}
exitCode, err := cmd.exec(debug, verbose)
if exitCode != 0 {
return false, err
}
return true, ""
}
// createRole creates a k8s Role in a given namespace from a template
func createRole(namespace string, role string) (bool, string) {
// load static resource
resource, e := Asset("data/role.yaml")
if e != nil {
logError(e.Error())
}
replaceStringInFile(resource, "temp-modified-role.yaml", map[string]string{"<<namespace>>": namespace, "<<role-name>>": role})
cmd := command{
Cmd: "bash",
Args: []string{"-c", "kubectl apply -f temp-modified-role.yaml "},
Description: "creating role [" + role + "] in namespace [ " + namespace + " ]",
}
exitCode, err := cmd.exec(debug, verbose)
if exitCode != 0 {
return false, err
}
deleteFile("temp-modified-role.yaml")
return true, ""
}
// labelResource applies Helmsman specific labels to Helm's state resources (secrets/configmaps)
func labelResource(r *release) {
if r.Enabled {
log.Println("INFO: applying Helmsman labels to [ " + r.Name + " ] in namespace [ " + r.Namespace + " ] ")
storageBackend := "configmap"
if s.Settings.StorageBackend == "secret" {
storageBackend = "secret"
}
cmd := command{
Cmd: "bash",
Args: []string{"-c", "kubectl label " + storageBackend + " -n " + getDesiredTillerNamespace(r) + " -l NAME=" + r.Name + " MANAGED-BY=HELMSMAN NAMESPACE=" + r.Namespace + " TILLER_NAMESPACE=" + getDesiredTillerNamespace(r) + " --overwrite"},
Description: "applying labels to Helm state in [ " + getDesiredTillerNamespace(r) + " ] for " + r.Name,
}
exitCode, err := cmd.exec(debug, verbose)
if exitCode != 0 {
logError(err)
}
}
}
// getHelmsmanReleases returns a map of all releases that are labeled with "MANAGED-BY=HELMSMAN"
// The releases are categorized by the namespaces in which their Tiller is running
// The returned map format is: map[<Tiller namespace>:map[<releases managed by Helmsman and deployed using this Tiller>:true]]
func getHelmsmanReleases() map[string]map[string]bool {
var lines []string
releases := make(map[string]map[string]bool)
storageBackend := "configmap"
if s.Settings.StorageBackend == "secret" {
storageBackend = "secret"
}
namespaces := make([]string, len(s.Namespaces))
i := 0
for s, v := range s.Namespaces {
if v.InstallTiller || v.UseTiller {
namespaces[i] = s
i++
}
}
namespaces = namespaces[0:i]
if v, ok := s.Namespaces["kube-system"]; !ok || (ok && (v.UseTiller || v.InstallTiller)) {
namespaces = append(namespaces, "kube-system")
}
for _, ns := range namespaces {
cmd := command{
Cmd: "bash",
Args: []string{"-c", "kubectl get " + storageBackend + " -n " + ns + " -l MANAGED-BY=HELMSMAN"},
Description: "getting helm releases which are managed by Helmsman in namespace [[ " + ns + " ]].",
}
exitCode, output := cmd.exec(debug, verbose)
if exitCode != 0 {
logError(output)
}
if strings.ToUpper("No resources found.") != strings.ToUpper(strings.TrimSpace(output)) {
lines = strings.Split(output, "\n")
}
for i := 0; i < len(lines); i++ {
if lines[i] == "" || strings.HasSuffix(strings.TrimSpace(lines[i]), "AGE") {
continue
} else {
fields := strings.Fields(lines[i])
if _, ok := releases[ns]; !ok {
releases[ns] = make(map[string]bool)
}
releases[ns][fields[0][0:strings.LastIndex(fields[0], ".v")]] = true
}
}
}
return releases
}
// getKubectlClientVersion returns kubectl client version
func getKubectlClientVersion() string {
cmd := command{
Cmd: "bash",
Args: []string{"-c", "kubectl version --client --short"},
Description: "checking kubectl version ",
}
exitCode, result := cmd.exec(debug, false)
if exitCode != 0 {
logError("ERROR: while checking kubectl version: " + result)
}
return result
}