This repository has been archived by the owner on Nov 7, 2020. It is now read-only.
forked from Praqma/helmsman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm_helpers.go
438 lines (379 loc) · 15.9 KB
/
helm_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
package main
import (
"log"
"strconv"
"strings"
"time"
"errors"
"github.com/gofunky/copy"
"github.com/gofunky/helmsman/gcs"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"path/filepath"
)
var currentState map[string]releaseState
// releaseState represents the current state of a release
type releaseState struct {
Revision int
Updated time.Time
Status string
Chart string
Namespace string
TillerNamespace string
}
// getAllReleases fetches a list of all releases in a k8s cluster
func getAllReleases() string {
result := getTillerReleases("kube-system")
for ns, v := range s.Namespaces {
if v.InstallTiller && ns != "kube-system" {
result = result + getTillerReleases(ns)
}
}
return result
}
// getTillerReleases gets releases deployed with a given Tiller (in a given namespace)
func getTillerReleases(tillerNS string) string {
cmd := command{
Cmd: "bash",
Args: []string{"-c", "helm list --all --tiller-namespace " + tillerNS + getNSTLSFlags(tillerNS)},
Description: "listing all existing releases in namespace [ " + tillerNS + " ]...",
}
exitCode, result := cmd.exec(debug, verbose)
if exitCode != 0 {
log.Fatal("ERROR: failed to list all releases in namespace [ " + tillerNS + " ]: " + result)
}
// appending tiller-namespace to each release found
lines := strings.Split(result, "\n")
for i, l := range lines {
if l != "" && !strings.HasPrefix(strings.TrimSpace(l), "NAME") && !strings.HasSuffix(strings.TrimSpace(l), "NAMESPACE") {
lines[i] = strings.TrimSuffix(l, "\n") + " " + tillerNS
}
}
return strings.Join(lines, "\n")
}
// buildState builds the currentState map containing information about all releases existing in a k8s cluster
func buildState() {
log.Println("INFO: mapping the current helm state ...")
currentState = make(map[string]releaseState)
lines := strings.Split(getAllReleases(), "\n")
for i := 0; i < len(lines); i++ {
// skipping the header from helm output
if lines[i] == "" || (strings.HasPrefix(strings.TrimSpace(lines[i]), "NAME") && strings.HasSuffix(strings.TrimSpace(lines[i]), "NAMESPACE")) {
continue
}
r, _ := strconv.Atoi(strings.Fields(lines[i])[1])
t := strings.Fields(lines[i])[2] + " " + strings.Fields(lines[i])[3] + " " + strings.Fields(lines[i])[4] + " " +
strings.Fields(lines[i])[5] + " " + strings.Fields(lines[i])[6]
parsedTime, err := time.Parse("Mon Jan _2 15:04:05 2006", t)
if err != nil {
log.Fatal("ERROR: while converting release time: " + err.Error())
}
currentState[strings.Fields(lines[i])[0]+"-"+strings.Fields(lines[i])[10]] = releaseState{
Revision: r,
Updated: parsedTime,
Status: strings.Fields(lines[i])[7],
Chart: strings.Fields(lines[i])[8],
Namespace: strings.Fields(lines[i])[9],
TillerNamespace: strings.Fields(lines[i])[10],
}
}
}
// helmRealseExists checks if a Helm release is/was deployed in a k8s cluster.
// It searches the Current State for releases.
// The key format for releases uniqueness is: <release name - the Tiller namespace where it should be deployed >
// If status is provided as an input [deployed, deleted, failed], then the search will verify the release status matches the search status.
func helmReleaseExists(r *release, status string) (bool, releaseState) {
compositeReleaseName := r.Name + "-" + getDesiredTillerNamespace(r)
v, ok := currentState[compositeReleaseName]
if !ok {
return false, v
}
if status != "" {
if v.Status == strings.ToUpper(status) {
return true, v
}
return false, v
}
return true, v
}
// getReleaseRevision returns the revision number for a release
func getReleaseRevision(rs releaseState) string {
return strconv.Itoa(rs.Revision)
}
// getReleaseChartName extracts and returns the Helm chart name from the chart info in a release state.
// example: chart in release state is "jenkins-0.9.0" and this function will extract "jenkins" from it.
func getReleaseChartName(rs releaseState) string {
chart := rs.Chart
runes := []rune(chart)
return string(runes[0:strings.LastIndexByte(chart[0:strings.IndexByte(chart, '.')], '-')])
}
// getReleaseChartVersion extracts and returns the Helm chart version from the chart info in a release state.
// example: chart in release state is returns "jenkins-0.9.0" and this functions will extract "0.9.0" from it.
func getReleaseChartVersion(rs releaseState) string {
chart := rs.Chart
runes := []rune(chart)
return string(runes[strings.LastIndexByte(chart, '-')+1 : len(chart)])
}
// getNSTLSFlags returns TLS flags for a given namespace if it's deployed with TLS
func getNSTLSFlags(ns string) string {
tls := ""
if tillerTLSEnabled(ns) {
tls = " --tls --tls-ca-cert " + ns + "-ca.cert --tls-cert " + ns + "-client.cert --tls-key " + ns + "-client.key "
}
return tls
}
// validateReleaseCharts validates if the charts defined in a release are valid.
// Valid charts are the ones that can be found in the defined repos.
// This function uses Helm search to verify if the chart can be found or not.
// For valid local paths, Helm creates a temporary package to check its validity.
func validateReleaseCharts(apps map[string]*release) (bool, string) {
for app, r := range apps {
if stat, err := os.Stat(r.Chart); err != nil || !stat.IsDir() {
cmd := command{
Cmd: "bash",
Args: []string{"-c", "helm search " + r.Chart + " --version " + r.Version + " -l"},
Description: "validating if chart " + r.Chart + "-" + r.Version + " is available in the defined repos.",
}
if exitCode, result := cmd.exec(debug, verbose); exitCode != 0 || strings.Contains(result, "No results found") {
return false, "ERROR: chart " + r.Chart + "-" + r.Version + " is specified for " +
"app [" + app + "] but is not found in the defined repos or the local file system."
}
} else {
destDir, err := forkChart(r)
if err != nil {
return false, err.Error()
}
cmd := command{
Cmd: "bash",
Args: []string{"-c", "helm package --destination " + destDir + " " + r.Chart},
Description: "validating if chart " + r.Chart + " is available at the defined path.",
}
if exitCode, result := cmd.exec(debug, verbose); exitCode != 0 || !strings.Contains(result, "Successfully packaged chart") {
return false, "ERROR: chart " + r.Chart + " is specified for " +
"app [" + app + "] but the chart data is not valid. " + result
}
}
}
return true, ""
}
// forkChart makes a copy of the given release chart, updates the release's chart path and version,
// and returns the containing temporary folder.
func forkChart(r *release) (string, error) {
// Read chart meta
chart, err := getChartDef(r.Chart)
if err != nil {
return "", errors.New("ERROR: the specified local helm chart is not valid.")
}
// Check chart version
if r.Version != "" && r.Version != chart.Version {
return "", errors.New("ERROR: the helm chart version does not match the specified helmsman app version.")
}
r.Version = chart.Version
// Create temporary directory for chart package
tmpDir, err := ioutil.TempDir("", "helmsman")
if err != nil {
return "", errors.New("ERROR: the temp path for helm packaging could not be accessed.")
}
// Use absolute path to a copy of the chart to circumvent https://github.com/helm/helm/issues/1979
newChartPath := filepath.Join(tmpDir, chart.Name)
err = copy.Copy(r.Chart, newChartPath)
if r.Chart, err = filepath.Abs(newChartPath); err != nil {
return "", errors.New("ERROR: the absolute path to the specified helm chart could not be determined.")
}
return tmpDir, nil
}
// chart type represents the fields of a Chart.yaml
type chartDef struct {
Name string `yaml:"name"`
Version string `yaml:"version"`
}
// getChartDef reads and parses the helm chart in the given path
func getChartDef(path string) (*chartDef, error) {
yamlFile, err := ioutil.ReadFile(filepath.Join(path, "Chart.yaml"))
if err != nil {
return nil, err
}
var chart chartDef
err = yaml.Unmarshal(yamlFile, &chart)
if err != nil {
return nil, err
}
return &chart, nil
}
// waitForTiller keeps checking if the helm Tiller is ready or not by executing helm list and checking its error (if any)
// waits for 5 seconds before each new attempt and eventually terminates after 10 failed attempts.
func waitForTiller(namespace string) {
attempt := 0
cmd := command{
Cmd: "bash",
Args: []string{"-c", "helm list --tiller-namespace " + namespace + getNSTLSFlags(namespace)},
Description: "checking if helm Tiller is ready in namespace [ " + namespace + " ].",
}
exitCode, err := cmd.exec(debug, verbose)
for attempt < 10 {
if exitCode == 0 {
return
} else if strings.Contains(err, "could not find a ready tiller pod") || strings.Contains(err, "could not find tiller") {
log.Println("INFO: waiting for helm Tiller to be ready in namespace [" + namespace + "] ...")
time.Sleep(5 * time.Second)
exitCode, err = cmd.exec(debug, verbose)
} else {
log.Fatal("ERROR: while waiting for helm Tiller to be ready in namespace [ " + namespace + " ] : " + err)
}
attempt = attempt + 1
}
logError("ERROR: timeout reached while waiting for helm Tiller to be ready in namespace [ " + namespace + " ]. Aborting!")
}
// addHelmRepos adds repositories to Helm if they don't exist already.
// Helm does not mind if a repo with the same name exists. It treats it as an update.
func addHelmRepos(repos map[string]string) (bool, string) {
for repoName, url := range repos {
// check if repo is in GCS, then perform GCS auth -- needed for private GCS helm repos
// failed auth would not throw an error here, as it is possible that the repo is public and does not need authentication
if strings.HasPrefix(url, "gs://") {
gcs.Auth()
}
cmd := command{
Cmd: "bash",
Args: []string{"-c", "helm repo add " + repoName + " " + url},
Description: "adding repo " + repoName,
}
if exitCode, err := cmd.exec(debug, verbose); exitCode != 0 {
return false, "ERROR: while adding repo [" + repoName + "]: " + err
}
}
cmd := command{
Cmd: "bash",
Args: []string{"-c", "helm repo update "},
Description: "updating helm repos",
}
if exitCode, err := cmd.exec(debug, verbose); exitCode != 0 {
return false, "ERROR: while updating helm repos : " + err
}
return true, ""
}
// deployTiller deploys Helm's Tiller in a specific namespace with a serviceAccount
// If serviceAccount is not provided (empty string), the defaultServiceAccount is used.
// If no defaultServiceAccount is provided, Tiller is deployed with the namespace default service account
// If no namespace is provided, Tiller is deployed to kube-system
func deployTiller(namespace string, serviceAccount string, defaultServiceAccount string) (bool, string) {
log.Println("INFO: deploying Tiller in namespace [ " + namespace + " ].")
sa := ""
sharedTiller := false
if namespace == "kube-system" && serviceAccount == "" {
sharedTiller = true
}
if serviceAccount != "" {
if ok, err := validateServiceAccount(serviceAccount, namespace); !ok {
if strings.Contains(err, "NotFound") || strings.Contains(err, "not found") {
log.Println("INFO: service account [ " + serviceAccount + " ] does not exist in namespace [ " + namespace + " ] .. attempting to create it ... ")
if _, rbacErr := createRBAC(serviceAccount, namespace, sharedTiller); rbacErr != "" {
return false, rbacErr
}
} else {
return false, "ERROR: while validating/creating service account [ " + serviceAccount + " ] in namespace [" + namespace + "]: " + err
}
}
sa = "--service-account " + serviceAccount
} else if defaultServiceAccount != "" {
if ok, err := validateServiceAccount(defaultServiceAccount, namespace); !ok {
if strings.Contains(err, "NotFound") || strings.Contains(err, "not found") {
log.Println("INFO: service account [ " + defaultServiceAccount + " ] does not exist in namespace [ " + namespace + " ] .. attempting to create it ... ")
if _, rbacErr := createRBAC(defaultServiceAccount, namespace, sharedTiller); rbacErr != "" {
return false, rbacErr
}
} else {
return false, "ERROR: while validating/creating service account [ " + defaultServiceAccount + " ] in namespace [ " + namespace + "]: " + err
}
}
sa = "--service-account " + defaultServiceAccount
}
if namespace == "" {
namespace = "kube-system"
}
tillerNameSpace := " --tiller-namespace " + namespace
tls := ""
if tillerTLSEnabled(namespace) {
tillerCert := downloadFile(s.Namespaces[namespace].TillerCert, namespace+"-tiller.cert")
tillerKey := downloadFile(s.Namespaces[namespace].TillerKey, namespace+"-tiller.key")
caCert := downloadFile(s.Namespaces[namespace].CaCert, namespace+"-ca.cert")
// client cert and key
downloadFile(s.Namespaces[namespace].ClientCert, namespace+"-client.cert")
downloadFile(s.Namespaces[namespace].ClientKey, namespace+"-client.key")
tls = " --tiller-tls --tiller-tls-cert " + tillerCert + " --tiller-tls-key " + tillerKey + " --tiller-tls-verify --tls-ca-cert " + caCert
}
storageBackend := ""
if v, ok := s.Settings["storageBackend"]; ok && v == "secret" {
storageBackend = " --override 'spec.template.spec.containers[0].command'='{/tiller,--storage=secret}'"
}
cmd := command{
Cmd: "bash",
Args: []string{"-c", "helm init --upgrade " + sa + tillerNameSpace + tls + storageBackend},
Description: "initializing helm on the current context and upgrading Tiller on namespace [ " + namespace + " ].",
}
if exitCode, err := cmd.exec(debug, verbose); exitCode != 0 {
return false, "ERROR: while deploying Helm Tiller in namespace [" + namespace + "]: " + err
}
return true, ""
}
// initHelm initializes helm on a k8s cluster and deploys Tiller in one or more namespaces
func initHelm() (bool, string) {
defaultSA := s.Settings["serviceAccount"]
for k, ns := range s.Namespaces {
if ns.InstallTiller && k != "kube-system" {
if ok, err := deployTiller(k, ns.TillerServiceAccount, defaultSA); !ok {
return false, err
}
}
}
return true, ""
}
// cleanUntrackedReleases checks for any releases that are managed by Helmsman and is no longer tracked by the desired state
// It compares the currently deployed releases with "MANAGED-BY=HELMSMAN" labels with Apps defined in the desired state
// For all untracked releases found, a decision is made to delete them and is added to the Helmsman plan
// NOTE: Untracked releases don't benefit from either namespace or application protection.
// NOTE: Removing/Commenting out an app from the desired state makes it untracked.
func cleanUntrackedReleases() {
toDelete := make(map[string]map[string]bool)
log.Println("INFO: checking if any Helmsman managed releases are no longer tracked by your desired state ...")
for ns, releases := range getHelmsmanReleases() {
for r := range releases {
tracked := false
for _, app := range s.Apps {
if app.Name == r && getDesiredTillerNamespace(app) == ns {
tracked = true
}
}
if !tracked {
if _, ok := toDelete[ns]; !ok {
toDelete[ns] = make(map[string]bool)
}
toDelete[ns][r] = true
}
}
}
if len(toDelete) == 0 {
log.Println("INFO: no untracked releases found.")
} else {
for ns, releases := range toDelete {
for r := range releases {
logDecision("DECISION: untracked release found: release [ "+r+" ] from Tiller in namespace [ "+ns+" ]. It will be deleted.", -800)
deleteUntrackedRelease(r, ns)
}
}
}
}
// deleteUntrackedRelease creates the helm command to purge delete an untracked release
func deleteUntrackedRelease(release string, tillerNamespace string) {
tls := ""
if tillerTLSEnabled(tillerNamespace) {
tls = " --tls --tls-ca-cert " + tillerNamespace + "-ca.cert --tls-cert " + tillerNamespace + "-client.cert --tls-key " + tillerNamespace + "-client.key "
}
cmd := command{
Cmd: "bash",
Args: []string{"-c", "helm delete --purge " + release + " --tiller-namespace " + tillerNamespace + tls},
Description: "deleting untracked release [ " + release + " ] from Tiller in namespace [[ " + tillerNamespace + " ]]",
}
outcome.addCommand(cmd, -800, nil)
}