-
Notifications
You must be signed in to change notification settings - Fork 785
/
helm_cli.go
703 lines (638 loc) · 19.9 KB
/
helm_cli.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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
package helm
import (
"fmt"
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/blang/semver"
"github.com/jenkins-x/jx/v2/pkg/kube"
"k8s.io/kubernetes/pkg/util/slice"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
)
// HelmCLI implements common helm actions based on helm CLI
type HelmCLI struct {
Binary string
BinVersion Version
CWD string
Runner util.Commander
Debug bool
kuber kube.Kuber
}
// NewHelmCLIWithRunner creates a new HelmCLI interface for the given runner
func NewHelmCLIWithRunner(runner util.Commander, binary string, version Version, cwd string, debug bool, kuber kube.Kuber) *HelmCLI {
cli := &HelmCLI{
Binary: binary,
BinVersion: version,
CWD: cwd,
Runner: runner,
Debug: debug,
kuber: kuber,
}
return cli
}
// NewHelmCLI creates a new HelmCLI instance configured to use the provided helm CLI in
// the given current working directory
func NewHelmCLI(binary string, version Version, cwd string, debug bool, args ...string) *HelmCLI {
a := []string{}
for _, x := range args {
y := strings.Split(x, " ")
a = append(a, y...)
}
runner := &util.Command{
Args: a,
Name: binary,
Dir: cwd,
}
cli := &HelmCLI{
Binary: binary,
BinVersion: version,
CWD: cwd,
Runner: runner,
Debug: debug,
}
return cli
}
// NewHelmCLIWithCompatibilityCheck creates a new HelmCLI and checks the compatibility with
// the currently installed helm CLI. This will exit the program with a fatal log if the helm CLI
// is not compatible.
func NewHelmCLIWithCompatibilityCheck(binary string, version Version, cwd string, debug bool, args ...string) *HelmCLI {
cli := NewHelmCLI(binary, version, cwd, debug, args...)
cli.checkCompatibility()
return cli
}
// checkCompatibility verifies whether the current helm CLI is compatible. The current
// implementation only supports helm CLI v2. This function will exit the program if the
// installed helm cli is not compatible.
func (h *HelmCLI) checkCompatibility() {
version, err := h.VersionWithArgs(false, "--client")
version = strings.TrimPrefix(version, "Client: ")
if err != nil {
log.Logger().Warnf("Unable to detect the current helm version due to: %s", err)
return
}
v, err := semver.ParseTolerant(version)
if err != nil {
log.Logger().Warnf("Unable to parse the current helm version: %s", version)
return
}
if h.BinVersion == V3 {
if v.Major != 3 {
log.Logger().Fatalf("Your current helm version v%d is not supported. Please upgrade to helm v3.", v.Major)
}
} else if os.Getenv("JX_HELM3") == "true" {
if v.Major != 3 {
log.Logger().Fatalf("You have set $JX_HELM3=true but your helm client version is %d", v.Major)
}
} else {
if v.Major > 2 {
log.Logger().Fatalf("Your current helm version v%d is not supported. Please downgrade to helm v2.", v.Major)
}
}
}
// SetHost is used to point at a locally running tiller
func (h *HelmCLI) SetHost(tillerAddress string) {
if h.Debug {
log.Logger().Infof("Setting tiller address to %s", util.ColorInfo(tillerAddress))
}
h.Runner.SetEnvVariable("HELM_HOST", tillerAddress)
}
// SetCWD configures the common working directory of helm CLI
func (h *HelmCLI) SetCWD(dir string) {
h.CWD = dir
}
// HelmBinary return the configured helm CLI
func (h *HelmCLI) HelmBinary() string {
return h.Binary
}
// SetHelmBinary configure a new helm CLI
func (h *HelmCLI) SetHelmBinary(binary string) {
h.Binary = binary
}
func (h *HelmCLI) runHelm(args ...string) error {
h.Runner.SetDir(h.CWD)
h.Runner.SetName(h.Binary)
h.Runner.SetArgs(args)
_, err := h.Runner.RunWithoutRetry()
return err
}
func (h *HelmCLI) runHelmWithOutput(args ...string) (string, error) {
h.Runner.SetDir(h.CWD)
h.Runner.SetName(h.Binary)
h.Runner.SetArgs(args)
return h.Runner.RunWithoutRetry()
}
// Init executes the helm init command according with the given flags
func (h *HelmCLI) Init(clientOnly bool, serviceAccount string, tillerNamespace string, upgrade bool) error {
args := []string{}
args = append(args, "init")
if clientOnly {
args = append(args, "--client-only")
}
if serviceAccount != "" {
args = append(args, "--service-account", serviceAccount)
}
if tillerNamespace != "" {
args = append(args, "--tiller-namespace", tillerNamespace)
}
if upgrade {
args = append(args, "--upgrade", "--wait", "--force-upgrade")
}
if h.Debug {
log.Logger().Debugf("Initialising Helm '%s'", util.ColorInfo(strings.Join(args, " ")))
}
return h.runHelm(args...)
}
// AddRepo adds a new helm repo with the given name and URL
func (h *HelmCLI) AddRepo(repo, URL, username, password string) error {
args := []string{"repo", "add", repo, URL}
if username != "" {
args = append(args, "--username", username)
}
if password != "" {
args = append(args, "--password", password)
}
return h.runHelm(args...)
}
// RemoveRepo removes the given repo from helm
func (h *HelmCLI) RemoveRepo(repo string) error {
return h.runHelm("repo", "remove", repo)
}
// ListRepos list the installed helm repos together with their URL
func (h *HelmCLI) ListRepos() (map[string]string, error) {
output, err := h.runHelmWithOutput("repo", "list")
repos := map[string]string{}
if err != nil {
// helm3 now returns an error if there are no repos
return repos, nil
//return nil, shouldError.Wrap(err, "failed to list repositories")
}
lines := strings.Split(strings.TrimSpace(output), "\n")
for _, line := range lines[1:] {
line = strings.TrimSpace(line)
fields := strings.Fields(line)
if len(fields) > 1 {
repos[strings.TrimSpace(fields[0])] = fields[1]
} else if len(fields) > 0 {
repos[fields[0]] = ""
}
}
return repos, nil
}
// SearchCharts searches for all the charts matching the given filter
func (h *HelmCLI) SearchCharts(filter string, allVersions bool) ([]ChartSummary, error) {
answer := []ChartSummary{}
args := []string{"search", filter}
if allVersions {
args = append(args, "--versions")
}
output, err := h.runHelmWithOutput(args...)
if err != nil {
return nil, errors.Wrap(err, "failed to search charts")
}
lines := strings.Split(output, "\n")
for _, line := range lines {
if strings.HasPrefix(line, "NAME") || line == "" {
continue
}
line = strings.TrimSpace(line)
fields := strings.Split(line, "\t")
chart := ChartSummary{}
l := len(fields)
if l == 0 {
continue
}
chart.Name = strings.TrimSpace(fields[0])
if l > 1 {
chart.ChartVersion = strings.TrimSpace(fields[1])
}
if l > 2 {
chart.AppVersion = strings.TrimSpace(fields[2])
}
if l > 3 {
chart.Description = strings.TrimSpace(fields[3])
}
answer = append(answer, chart)
}
return answer, nil
}
// IsRepoMissing checks if the repository with the given URL is missing from helm.
// If the repo is found, the name of the repo will be returned
func (h *HelmCLI) IsRepoMissing(URL string) (bool, string, error) {
repos, err := h.ListRepos()
if err != nil {
return true, "", errors.Wrap(err, "failed to list the repositories")
}
searchedURL, err := url.Parse(URL)
if err != nil {
return true, "", errors.Wrap(err, "provided repo URL is invalid")
}
for name, repoURL := range repos {
if len(repoURL) > 0 {
url, err := url.Parse(repoURL)
if err != nil {
return true, "", errors.Wrap(err, "failed to parse the repo URL")
}
// match on the whole URL as helm dep build requires on username + passowrd in the URL
if url.Host == searchedURL.Host && url.Path == searchedURL.Path {
return false, name, nil
}
}
}
return true, "", nil
}
// UpdateRepo updates the helm repositories
func (h *HelmCLI) UpdateRepo() error {
return h.runHelm("repo", "update")
}
// RemoveRequirementsLock removes the requirements.lock file from the current working directory
func (h *HelmCLI) RemoveRequirementsLock() error {
dir := h.CWD
path := filepath.Join(dir, "requirements.lock")
exists, err := util.FileExists(path)
if err != nil {
return errors.Wrapf(err, "no requirements.lock file found in directory '%s'", dir)
}
if exists {
err = os.Remove(path)
if err != nil {
return errors.Wrap(err, "failed to remove the requirements.lock file")
}
}
return nil
}
// BuildDependency builds the helm dependencies of the helm chart from the current working directory
func (h *HelmCLI) BuildDependency() error {
if h.Debug {
log.Logger().Infof("Running %s dependency build in %s\n", h.Binary, util.ColorInfo(h.CWD))
out, err := h.runHelmWithOutput("dependency", "build")
log.Logger().Infof(out)
return err
}
return h.runHelm("dependency", "build")
}
// InstallChart installs a helm chart according with the given flags
func (h *HelmCLI) InstallChart(chart string, releaseName string, ns string, version string, timeout int,
values []string, valueStrings []string, valueFiles []string, repo string, username string, password string) error {
var err error
args := []string{}
args = append(args, "install", "--wait", "--name", releaseName, "--namespace", ns, chart)
repo, err = addUsernamePasswordToURL(repo, username, password)
if err != nil {
return err
}
if timeout != -1 {
if h.Binary == "helm3" {
args = append(args, "--timeout", fmt.Sprintf("%ss", strconv.Itoa(timeout)))
} else {
args = append(args, "--timeout", strconv.Itoa(timeout))
}
}
if version != "" {
args = append(args, "--version", version)
}
for _, value := range values {
args = append(args, "--set", value)
}
for _, value := range valueStrings {
args = append(args, "--set-string", value)
}
for _, valueFile := range valueFiles {
args = append(args, "--values", valueFile)
}
if repo != "" {
args = append(args, "--repo", repo)
}
if username != "" {
args = append(args, "--username", username)
}
if password != "" {
args = append(args, "--password", password)
}
logLevel := os.Getenv("JX_HELM_VERBOSE")
if logLevel != "" {
args = append(args, "-v", logLevel)
}
if h.Debug {
log.Logger().Infof("Installing Chart '%s'", util.ColorInfo(strings.Join(args, " ")))
}
err = h.runHelm(args...)
if err != nil {
return err
}
return nil
}
// FetchChart fetches a Helm Chart
func (h *HelmCLI) FetchChart(chart string, version string, untar bool, untardir string, repo string,
username string, password string) error {
args := []string{}
args = append(args, "fetch", chart)
repo, err := addUsernamePasswordToURL(repo, username, password)
if err != nil {
return err
}
if untardir != "" {
args = append(args, "--untardir", untardir)
}
if untar {
args = append(args, "--untar")
}
if username != "" {
args = append(args, "--username", username)
}
if password != "" {
args = append(args, "--password", password)
}
if version != "" {
args = append(args, "--version", version)
}
if repo != "" {
args = append(args, "--repo", repo)
}
if h.Debug {
log.Logger().Infof("Fetching Chart '%s'", util.ColorInfo(strings.Join(args, " ")))
}
return h.runHelm(args...)
}
// Template generates the YAML from the chart template to the given directory
func (h *HelmCLI) Template(chart string, releaseName string, ns string, outDir string, upgrade bool,
values []string, valueStrings []string, valueFiles []string) error {
args := []string{"template", "--name", releaseName, "--namespace", ns, chart, "--output-dir", outDir, "--debug"}
if upgrade {
args = append(args, "--is-upgrade")
}
for _, value := range values {
args = append(args, "--set", value)
}
for _, value := range valueStrings {
args = append(args, "--set-string", value)
}
for _, valueFile := range valueFiles {
args = append(args, "--values", valueFile)
}
if h.Debug {
log.Logger().Debugf("Generating Chart Template '%s'", util.ColorInfo(strings.Join(args, " ")))
}
err := h.runHelm(args...)
if err != nil {
return errors.Wrapf(err, "Failed to run helm %s", strings.Join(args, " "))
}
return err
}
// UpgradeChart upgrades a helm chart according with given helm flags
func (h *HelmCLI) UpgradeChart(chart string, releaseName string, ns string, version string, install bool, timeout int, force bool, wait bool, values []string, valueStrings []string, valueFiles []string, repo string, username string, password string) error {
var err error
args := []string{}
args = append(args, "upgrade")
args = append(args, "--namespace", ns)
repo, err = addUsernamePasswordToURL(repo, username, password)
if err != nil {
return err
}
if install {
args = append(args, "--install")
}
if wait {
args = append(args, "--wait")
}
if force {
args = append(args, "--force")
}
if timeout != -1 {
if h.BinVersion == V3 {
args = append(args, "--timeout", fmt.Sprintf("%ss", strconv.Itoa(timeout)))
} else {
args = append(args, "--timeout", strconv.Itoa(timeout))
}
}
if version != "" {
args = append(args, "--version", version)
}
for _, value := range values {
args = append(args, "--set", value)
}
for _, value := range valueStrings {
args = append(args, "--set-string", value)
}
for _, valueFile := range valueFiles {
args = append(args, "--values", valueFile)
}
if repo != "" {
args = append(args, "--repo", repo)
}
if username != "" {
args = append(args, "--username", username)
}
if password != "" {
args = append(args, "--password", password)
}
logLevel := os.Getenv("JX_HELM_VERBOSE")
if logLevel != "" {
args = append(args, "-v", logLevel)
}
args = append(args, releaseName, chart)
if h.Debug {
log.Logger().Infof("Upgrading Chart '%s'", util.ColorInfo(strings.Join(args, " ")))
}
err = h.runHelm(args...)
if err != nil {
return err
}
return nil
}
// DeleteRelease removes the given release
func (h *HelmCLI) DeleteRelease(ns string, releaseName string, purge bool) error {
args := []string{}
args = append(args, "delete")
if purge {
args = append(args, "--purge")
}
args = append(args, releaseName)
return h.runHelm(args...)
}
//ListReleases lists the releases in ns
func (h *HelmCLI) ListReleases(ns string) (map[string]ReleaseSummary, []string, error) {
output, err := h.runHelmWithOutput("list", "--all", "--namespace", ns)
if err != nil {
return nil, nil, errors.Wrapf(err, "running helm list --all --namespace %s", ns)
}
lines := strings.Split(strings.TrimSpace(output), "\n")
result := make(map[string]ReleaseSummary, 0)
keys := make([]string, 0)
if len(lines) > 1 {
if h.Binary == "helm" {
for _, line := range lines[1:] {
fields := strings.Fields(line)
if len(fields) == 10 || len(fields) == 11 {
chartFullName := fields[8]
lastDash := strings.LastIndex(chartFullName, "-")
releaseName := fields[0]
keys = append(keys, releaseName)
result[releaseName] = ReleaseSummary{
ReleaseName: fields[0],
Revision: fields[1],
Updated: fmt.Sprintf("%s %s %s %s %s", fields[2], fields[3], fields[4], fields[5],
fields[6]),
Status: fields[7],
ChartFullName: chartFullName,
Namespace: ns,
Chart: chartFullName[:lastDash],
ChartVersion: chartFullName[lastDash+1:],
}
} else {
return nil, nil, errors.Errorf("Cannot parse %s as helm list output", line)
}
}
} else {
for _, line := range lines[1:] {
fields := strings.Fields(line)
if len(fields) == 9 {
chartFullName := fields[8]
lastDash := strings.LastIndex(chartFullName, "-")
releaseName := fields[0]
keys = append(keys, releaseName)
result[releaseName] = ReleaseSummary{
ReleaseName: fields[0],
Revision: fields[2],
Updated: fmt.Sprintf("%s %s %s %s", fields[3], fields[4], fields[5], fields[6]),
Status: strings.ToUpper(fields[7]),
ChartFullName: chartFullName,
Namespace: ns,
Chart: chartFullName[:lastDash],
ChartVersion: chartFullName[lastDash+1:],
}
} else {
return nil, nil, errors.Errorf("Cannot parse %s as helm3 list output", line)
}
}
}
}
slice.SortStrings(keys)
return result, keys, nil
}
// FindChart find a chart in the current working directory, if no chart file is found an error is returned
func (h *HelmCLI) FindChart() (string, error) {
dir := h.CWD
chartFile := filepath.Join(dir, ChartFileName)
exists, err := util.FileExists(chartFile)
if err != nil {
return "", errors.Wrapf(err, "no Chart.yaml file found in directory '%s'", dir)
}
if !exists {
files, err := filepath.Glob(filepath.Join(dir, "*", "Chart.yaml"))
if err != nil {
return "", errors.Wrap(err, "no Chart.yaml file found")
}
if len(files) > 0 {
chartFile = files[0]
} else {
files, err = filepath.Glob(filepath.Join(dir, "*", "*", "Chart.yaml"))
if err != nil {
return "", errors.Wrap(err, "no Chart.yaml file found")
}
if len(files) > 0 {
for _, file := range files {
if !strings.HasSuffix(file, "/preview/Chart.yaml") {
return file, nil
}
}
}
}
}
return chartFile, nil
}
// StatusRelease returns the output of the helm status command for a given release
func (h *HelmCLI) StatusRelease(ns string, releaseName string) error {
return h.runHelm("status", releaseName)
}
// StatusReleaseWithOutput returns the output of the helm status command for a given release
func (h *HelmCLI) StatusReleaseWithOutput(ns string, releaseName string, outputFormat string) (string, error) {
if outputFormat == "" {
return h.runHelmWithOutput("status", releaseName)
}
return h.runHelmWithOutput("status", releaseName, "--output", outputFormat)
}
// Lint lints the helm chart from the current working directory and returns the warnings in the output
func (h *HelmCLI) Lint(valuesFiles []string) (string, error) {
args := []string{"lint",
"--set", "tags.jx-lint=true",
"--set", "global.jxLint=true",
"--set-string", "global.jxTypeEnv=lint",
}
for _, valueFile := range valuesFiles {
if valueFile != "" {
args = append(args, "--values", valueFile)
}
}
return h.runHelmWithOutput(args...)
}
// Env returns the environment variables for the helmer
func (h *HelmCLI) Env() map[string]string {
return h.Runner.CurrentEnv()
}
// Version executes the helm version command and returns its output
func (h *HelmCLI) Version(tls bool) (string, error) {
versionString, err := h.VersionWithArgs(tls)
if err != nil {
return "", errors.Wrapf(err, "unable to query helm version")
}
return h.extractSemanticVersion(versionString)
}
// VersionWithArgs executes the helm version command and returns its output
func (h *HelmCLI) VersionWithArgs(tls bool, extraArgs ...string) (string, error) {
args := []string{"version", "--short", "--client"}
if tls {
args = append(args, "--tls")
}
args = append(args, extraArgs...)
return h.runHelmWithOutput(args...)
}
// PackageChart packages the chart from the current working directory
func (h *HelmCLI) PackageChart() error {
return h.runHelm("package", h.CWD)
}
func (h *HelmCLI) DecryptSecrets(location string) error {
return h.runHelm("secrets", "dec", location)
}
// Kube returns the k8s config client
func (h *HelmCLI) Kube() kube.Kuber {
if h.kuber == nil {
h.kuber = kube.NewKubeConfig()
}
return h.kuber
}
// SetKube sets the kube config client
func (h *HelmCLI) SetKube(kuber kube.Kuber) {
h.kuber = kuber
}
// Helm really prefers to have the username and password embedded in the URL (ugh) so this
// function makes that happen
func addUsernamePasswordToURL(urlStr string, username string, password string) (string, error) {
if urlStr != "" && username != "" && password != "" {
u, err := url.Parse(urlStr)
if err != nil {
return "", err
}
u.User = url.UserPassword(username, password)
return u.String(), nil
}
return urlStr, nil
}
// extractSemanticVersion tries to extract a semantic version string substring from the specified string
func (h *HelmCLI) extractSemanticVersion(versionString string) (string, error) {
r := regexp.MustCompile(`.*v?(?P<SemVer>\d+\.\d+\.\d+).*`)
match := r.FindStringSubmatch(versionString)
if match == nil {
return "", errors.Errorf("unable to extract a semantic version from %s", versionString)
}
for i, name := range r.SubexpNames() {
if name == "SemVer" {
return match[i], nil
}
}
return "", errors.Errorf("unable to extract a semantic version from %s", versionString)
}