forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelm_template.go
581 lines (514 loc) · 15.3 KB
/
helm_template.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
package helm
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
const (
// LabelReleaseName stores the chart release name
LabelReleaseName = "jenkins.io/chart-release"
// LabelReleaseChartVersion stores the version of a chart installation in a label
LabelReleaseChartVersion = "jenkins.io/version"
)
// HelmTemplate implements common helm actions but purely as client side operations
// delegating a separate Helmer such as HelmCLI for the client side operations
type HelmTemplate struct {
Client *HelmCLI
OutputDir string
CWD string
Binary string
Runner *util.Command
KubectlValidate bool
KubeClient kubernetes.Interface
}
// NewHelmTemplate creates a new HelmTemplate instance configured to the given client side Helmer
func NewHelmTemplate(client *HelmCLI, workDir string, kubeClient kubernetes.Interface) *HelmTemplate {
cli := &HelmTemplate{
Client: client,
OutputDir: workDir,
Runner: client.Runner,
Binary: "kubectl",
CWD: client.CWD,
KubectlValidate: false,
KubeClient: kubeClient,
}
return cli
}
// SetHost is used to point at a locally running tiller
func (h *HelmTemplate) SetHost(tillerAddress string) {
// NOOP
}
// SetCWD configures the common working directory of helm CLI
func (h *HelmTemplate) SetCWD(dir string) {
h.Client.SetCWD(dir)
}
// HelmBinary return the configured helm CLI
func (h *HelmTemplate) HelmBinary() string {
return h.Client.HelmBinary()
}
// SetHelmBinary configure a new helm CLI
func (h *HelmTemplate) SetHelmBinary(binary string) {
h.Client.SetHelmBinary(binary)
}
// Init executes the helm init command according with the given flags
func (h *HelmTemplate) Init(clientOnly bool, serviceAccount string, tillerNamespace string, upgrade bool) error {
return h.Client.Init(true, serviceAccount, tillerNamespace, upgrade)
}
// AddRepo adds a new helm repo with the given name and URL
func (h *HelmTemplate) AddRepo(repo string, URL string) error {
return h.Client.AddRepo(repo, URL)
}
// RemoveRepo removes the given repo from helm
func (h *HelmTemplate) RemoveRepo(repo string) error {
return h.Client.RemoveRepo(repo)
}
// ListRepos list the installed helm repos together with their URL
func (h *HelmTemplate) ListRepos() (map[string]string, error) {
return h.Client.ListRepos()
}
// SearchCharts searches for all the charts matching the given filter
func (h *HelmTemplate) SearchCharts(filter string) ([]ChartSummary, error) {
return h.Client.SearchCharts(filter)
}
// IsRepoMissing checks if the repository with the given URL is missing from helm
func (h *HelmTemplate) IsRepoMissing(URL string) (bool, error) {
return h.Client.IsRepoMissing(URL)
}
// UpdateRepo updates the helm repositories
func (h *HelmTemplate) UpdateRepo() error {
return h.Client.UpdateRepo()
}
// RemoveRequirementsLock removes the requirements.lock file from the current working directory
func (h *HelmTemplate) RemoveRequirementsLock() error {
return h.Client.RemoveRequirementsLock()
}
// BuildDependency builds the helm dependencies of the helm chart from the current working directory
func (h *HelmTemplate) BuildDependency() error {
return h.Client.BuildDependency()
}
// ListCharts execute the helm list command and returns its output
func (h *HelmTemplate) ListCharts() (string, error) {
return h.Client.ListCharts()
}
// SearchChartVersions search all version of the given chart
func (h *HelmTemplate) SearchChartVersions(chart string) ([]string, error) {
return h.Client.SearchChartVersions(chart)
}
// FindChart find a chart in the current working directory, if no chart file is found an error is returned
func (h *HelmTemplate) FindChart() (string, error) {
return h.Client.FindChart()
}
// Lint lints the helm chart from the current working directory and returns the warnings in the output
func (h *HelmTemplate) Lint() (string, error) {
return h.Client.Lint()
}
// Env returns the environment variables for the helmer
func (h *HelmTemplate) Env() map[string]string {
return h.Client.Env()
}
// PackageChart packages the chart from the current working directory
func (h *HelmTemplate) PackageChart() error {
return h.Client.PackageChart()
}
// Version executes the helm version command and returns its output
func (h *HelmTemplate) Version(tls bool) (string, error) {
return h.Client.VersionWithArgs(tls, "--client")
}
// Mutation API
// InstallChart installs a helm chart according with the given flags
func (h *HelmTemplate) InstallChart(chart string, releaseName string, ns string, version *string, timeout *int,
values []string, valueFiles []string) error {
err := h.clearOutputDir()
if err != nil {
return err
}
chartDir, err := h.chartNameToFolder(chart)
if err != nil {
return err
}
err = h.Client.Template(chartDir, releaseName, ns, h.OutputDir, false, values, valueFiles)
if err != nil {
return err
}
_, versionText, err := h.getChartNameAndVersion(chartDir, version)
if err != nil {
return err
}
err = h.addLabelsToFiles(releaseName, versionText)
if err != nil {
return err
}
wait := true
err = h.kubectlApply(ns, chart, releaseName, false, true)
if err != nil {
return err
}
return h.deleteOldResources(ns, releaseName, versionText, wait)
}
// UpgradeChart upgrades a helm chart according with given helm flags
func (h *HelmTemplate) UpgradeChart(chart string, releaseName string, ns string, version *string, install bool,
timeout *int, force bool, wait bool, values []string, valueFiles []string) error {
err := h.clearOutputDir()
if err != nil {
return err
}
chartDir, err := h.chartNameToFolder(chart)
if err != nil {
return err
}
err = h.Client.Template(chartDir, releaseName, ns, h.OutputDir, false, values, valueFiles)
if err != nil {
return err
}
_, versionText, err := h.getChartNameAndVersion(chartDir, version)
if err != nil {
return err
}
err = h.addLabelsToFiles(releaseName, versionText)
if err != nil {
return err
}
err = h.kubectlApply(ns, chart, releaseName, wait, false)
if err != nil {
return err
}
return h.deleteOldResources(ns, releaseName, versionText, wait)
}
func (h *HelmTemplate) kubectlApply(ns string, chart string, releaseName string, wait bool, create bool) error {
log.Infof("Applying generated chart %s YAML via kubectl in dir: %s\n", chart, h.OutputDir)
command := "apply"
if create {
command = "create"
}
args := []string{command, "--recursive", "-f", h.OutputDir, "-l", LabelReleaseName + "=" + releaseName}
if ns != "" {
args = append(args, "--namespace", ns)
}
if wait && !create {
args = append(args, "--wait")
}
if !h.KubectlValidate {
args = append(args, "--validate=false")
}
return h.runKubectl(args...)
}
func (h *HelmTemplate) deleteOldResources(ns string, releaseName string, versionText string, wait bool) error {
selector := LabelReleaseName + "=" + releaseName + "," + LabelReleaseChartVersion + "!=" + versionText
log.Infof("Removing Kubernetes resources from older releases using selector: %s\n", util.ColorInfo(selector))
return h.deleteResourcesBySelector(ns, selector, wait)
}
func (h *HelmTemplate) deleteResourcesBySelector(ns string, selector string, wait bool) error {
args := []string{"delete", "all", "--ignore-not-found", "--namespace", ns, "-l", selector}
if wait {
args = append(args, "--wait")
}
err := h.runKubectl(args...)
if err != nil {
return err
}
// now lets delete resource CRDs
args = []string{"delete", "release", "--ignore-not-found", "--namespace", ns, "-l", selector}
if wait {
args = append(args, "--wait")
}
// lets ignore failures - probably due to CRD not yet existing
h.runKubectl(args...)
return nil
}
// DeleteRelease removes the given release
func (h *HelmTemplate) DeleteRelease(ns string, releaseName string, purge bool) error {
selector := LabelReleaseName + "=" + releaseName
log.Infof("Removing release %s using selector: %s\n", util.ColorInfo(releaseName), util.ColorInfo(selector))
return h.deleteResourcesBySelector(ns, selector, true)
}
// StatusRelease returns the output of the helm status command for a given release
func (h *HelmTemplate) StatusRelease(ns string, releaseName string) error {
// TODO
return nil
}
// StatusReleases returns the status of all installed releases
func (h *HelmTemplate) StatusReleases(ns string) (map[string]string, error) {
statusMap := map[string]string{}
if h.KubeClient == nil {
return statusMap, fmt.Errorf("No KubeClient configured!")
}
deployList, err := h.KubeClient.AppsV1beta1().Deployments(ns).List(metav1.ListOptions{})
if err != nil {
return statusMap, errors.Wrapf(err, "Failed to list Deployments in namespace %s", ns)
}
for _, deploy := range deployList.Items {
labels := deploy.Labels
if labels != nil {
release := labels[LabelReleaseName]
if release != "" {
statusMap[release] = "DEPLOYED"
}
}
}
return statusMap, nil
}
func (h *HelmTemplate) getOutputDir() (string, error) {
if h.OutputDir == "" {
d, err := ioutil.TempDir("", "helm-template-output-")
if err != nil {
return "", errors.Wrap(err, "Failed to create temporary directory for helm template output")
}
h.OutputDir = d
}
dir := h.OutputDir
if dir == "" {
return dir, fmt.Errorf("No OutputDir specifeid for HelmTemplate")
}
return dir, nil
}
// clearOutputDir removes all files in the helm output dir
func (h *HelmTemplate) clearOutputDir() error {
dir, err := h.getOutputDir()
files, err := filepath.Glob(filepath.Join(dir, "*"))
if err != nil {
return err
}
for _, file := range files {
err = os.RemoveAll(file)
if err != nil {
return err
}
}
return nil
}
func (h *HelmTemplate) chartNameToFolder(chart string) (string, error) {
exists, err := util.FileExists(chart)
if err != nil {
return "", err
}
if exists {
return chart, nil
}
safeChartName := strings.Replace(chart, string(os.PathSeparator), "-", -1)
dir, err := ioutil.TempDir("", safeChartName)
if err != nil {
return "", err
}
err = h.Client.runHelm("fetch", "-d", dir, "--untar", chart)
if err != nil {
return "", err
}
answer := dir
files, err := ioutil.ReadDir(dir)
if err != nil {
return "", err
}
for _, f := range files {
if f.IsDir() {
answer = filepath.Join(dir, f.Name())
break
}
}
log.Infof("Fetched chart %s to dir %s\n", chart, answer)
return answer, nil
}
func (h *HelmTemplate) addLabelsToFiles(chart string, version string) error {
dir, err := h.getOutputDir()
if err != nil {
return err
}
return addLabelsToChartYaml(dir, chart, version)
}
func addLabelsToChartYaml(dir string, chart string, version string) error {
return filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
ext := filepath.Ext(path)
if ext == ".yaml" {
file := path
data, err := ioutil.ReadFile(file)
if err != nil {
return errors.Wrapf(err, "Failed to load file %s", file)
}
m := yaml.MapSlice{}
err = yaml.Unmarshal(data, &m)
if err != nil {
return errors.Wrapf(err, "Failed to parse YAML of file %s", file)
}
helmHook := getYamlValueString(&m, "metadata", "annotations", "helm.sh/hook")
if strings.HasSuffix(helmHook, "-delete") {
// lets remove any pre/post delete hooks...
err = os.Remove(path)
if err != nil {
log.Warnf("Failed to remove helm hook template %s: %s", path, err)
} else {
log.Infof("Ignored helm delete hook file %s\n", path)
}
return nil
}
err = setYamlValue(&m, chart, "metadata", "labels", LabelReleaseName)
if err != nil {
return errors.Wrapf(err, "Failed to modify YAML of file %s", file)
}
err = setYamlValue(&m, version, "metadata", "labels", LabelReleaseChartVersion)
if err != nil {
return errors.Wrapf(err, "Failed to modify YAML of file %s", file)
}
data, err = yaml.Marshal(&m)
if err != nil {
return errors.Wrapf(err, "Failed to marshal YAML of file %s", file)
}
err = ioutil.WriteFile(file, data, util.DefaultWritePermissions)
if err != nil {
return errors.Wrapf(err, "Failed to write YAML file %s", file)
}
}
return nil
})
}
func getYamlValueString(mapSlice *yaml.MapSlice, keys ...string) string {
value := getYamlValue(mapSlice, keys...)
answer, ok := value.(string)
if ok {
return answer
}
return ""
}
func getYamlValue(mapSlice *yaml.MapSlice, keys ...string) interface{} {
if mapSlice == nil {
return nil
}
if mapSlice == nil {
return fmt.Errorf("No map input!")
}
m := mapSlice
lastIdx := len(keys) - 1
for idx, k := range keys {
last := idx >= lastIdx
found := false
for _, mi := range *m {
if mi.Key == k {
found = true
if last {
return mi.Value
} else {
value := mi.Value
if value == nil {
return nil
} else {
v, ok := value.(yaml.MapSlice)
if ok {
m = &v
} else {
v2, ok := value.(*yaml.MapSlice)
if ok {
m = v2
} else {
return nil
}
}
}
}
}
}
if !found {
return nil
}
}
return nil
}
// setYamlValue navigates through the YAML object structure lazily creating or inserting new values
func setYamlValue(mapSlice *yaml.MapSlice, value string, keys ...string) error {
if mapSlice == nil {
return fmt.Errorf("No map input!")
}
m := mapSlice
lastIdx := len(keys) - 1
for idx, k := range keys {
last := idx >= lastIdx
found := false
for i, mi := range *m {
if mi.Key == k {
found = true
if last {
(*m)[i].Value = value
} else {
value := (*m)[i].Value
if value == nil {
v := &yaml.MapSlice{}
(*m)[i].Value = v
m = v
} else {
v, ok := value.(yaml.MapSlice)
if ok {
m2 := &yaml.MapSlice{}
*m2 = append(*m2, v...)
(*m)[i].Value = m2
m = m2
} else {
v2, ok := value.(*yaml.MapSlice)
if ok {
m2 := &yaml.MapSlice{}
*m2 = append(*m2, *v2...)
(*m)[i].Value = m2
m = m2
} else {
return fmt.Errorf("Could not convert key %s value %#v to a yaml.MapSlice", k, value)
}
}
}
}
}
}
if !found {
if last {
*m = append(*m, yaml.MapItem{
Key: k,
Value: value,
})
} else {
m2 := &yaml.MapSlice{}
*m = append(*m, yaml.MapItem{
Key: k,
Value: m2,
})
m = m2
}
}
}
return nil
}
func (h *HelmTemplate) runKubectl(args ...string) error {
h.Runner.Name = h.Binary
h.Runner.Dir = h.CWD
h.Runner.Args = args
_, err := h.Runner.RunWithoutRetry()
return err
}
func (h *HelmTemplate) runKubectlWithOutput(args ...string) (string, error) {
h.Runner.Dir = h.CWD
h.Runner.Name = h.Binary
h.Runner.Args = args
return h.Runner.RunWithoutRetry()
}
// getChartNameAndVersion returns the chart name and version for the current chart folder
func (h *HelmTemplate) getChartNameAndVersion(chartDir string, version *string) (string, string, error) {
versionText := ""
file := filepath.Join(chartDir, "Chart.yaml")
if !filepath.IsAbs(chartDir) {
file = filepath.Join(h.Runner.Dir, file)
}
exists, err := util.FileExists(file)
if err != nil {
return "", versionText, err
}
if !exists {
return "", versionText, fmt.Errorf("No file %s found!", file)
}
chartName, versionText, err := LoadChartNameAndVersion(file)
if version != nil && *version != "" {
versionText = *version
}
return chartName, versionText, err
}