-
Notifications
You must be signed in to change notification settings - Fork 1
/
helm_cli.go
429 lines (388 loc) · 11.6 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
package helm
import (
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/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.Command
Debug bool
}
// 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, " ")
for _, z := range y {
a = append(a, z)
}
}
runner := &util.Command{
Args: a,
Name: binary,
Dir: cwd,
}
cli := &HelmCLI{
Binary: binary,
BinVersion: version,
CWD: cwd,
Runner: runner,
}
return cli
}
// SetHost is used to point at a locally running tiller
func (h *HelmCLI) SetHost(tillerAddress string) {
if h.Runner.Env == nil {
h.Runner.Env = map[string]string{}
}
if h.Debug {
log.Infof("Setting tiller address to %s\n", util.ColorInfo(tillerAddress))
}
h.Runner.Env["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.Name = h.Binary
h.Runner.Dir = h.CWD
h.Runner.Args = args
_, err := h.Runner.RunWithoutRetry()
return err
}
func (h *HelmCLI) runHelmWithOutput(args ...string) (string, error) {
h.Runner.Dir = h.CWD
h.Runner.Name = h.Binary
h.Runner.Args = 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.Infof("Initialising Helm '%s'\n", 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 string, URL string) error {
return h.runHelm("repo", "add", repo, URL)
}
// 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")
if err != nil {
return nil, errors.Wrap(err, "failed to list repositories")
}
repos := map[string]string{}
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) ([]ChartSummary, error) {
answer := []ChartSummary{}
output, err := h.runHelmWithOutput("search", filter)
if err != nil {
return nil, errors.Wrap(err, "failed to search charts")
}
lines := strings.Split(output, "\n")
for _, line := range lines[1:] {
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
func (h *HelmCLI) IsRepoMissing(URL string) (bool, 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 _, 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")
}
if url.Host == searchedURL.Host {
return false, 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 {
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, valueFiles []string) error {
args := []string{}
args = append(args, "install", "--wait", "--name", releaseName, "--namespace", ns, chart)
if timeout != nil {
args = append(args, "--timeout", strconv.Itoa(*timeout))
}
if version != nil {
args = append(args, "--version", *version)
}
for _, value := range values {
args = append(args, "--set", value)
}
for _, valueFile := range valueFiles {
args = append(args, "--values", valueFile)
}
if h.Debug {
log.Infof("Installing Chart '%s'\n", 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, 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 _, valueFile := range valueFiles {
args = append(args, "--values", valueFile)
}
if h.Debug {
log.Infof("Generating Chart Template '%s'\n", util.ColorInfo(strings.Join(args, " ")))
}
return h.runHelm(args...)
}
// 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, valueFiles []string) error {
args := []string{}
args = append(args, "upgrade")
args = append(args, "--namespace", ns)
if install {
args = append(args, "--install")
}
if wait {
args = append(args, "--wait")
}
if force {
args = append(args, "--force")
}
if timeout != nil {
args = append(args, "--timeout", strconv.Itoa(*timeout))
}
if version != nil {
args = append(args, "--version", *version)
}
for _, value := range values {
args = append(args, "--set", value)
}
for _, valueFile := range valueFiles {
args = append(args, "--values", valueFile)
}
args = append(args, releaseName, chart)
if h.Debug {
log.Infof("Upgrading Chart '%s'\n", util.ColorInfo(strings.Join(args, " ")))
}
return h.runHelm(args...)
}
// DeleteRelease removes the given release
func (h *HelmCLI) DeleteRelease(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...)
}
// ListCharts execute the helm list command and returns its output
func (h *HelmCLI) ListCharts() (string, error) {
return h.runHelmWithOutput("list")
}
// SearchChartVersions search all version of the given chart
func (h *HelmCLI) SearchChartVersions(chart string) ([]string, error) {
output, err := h.runHelmWithOutput("search", chart, "--versions")
if err != nil {
return nil, errors.Wrapf(err, "failed to search chart '%s'", chart)
}
versions := []string{}
lines := strings.Split(strings.TrimSpace(output), "\n")
if len(lines) > 1 {
for _, line := range lines[1:] {
fields := strings.Fields(line)
if len(fields) > 1 {
v := fields[1]
if v != "" {
versions = append(versions, v)
}
}
}
}
return versions, 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, "Chart.yaml")
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("*/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("*/*/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(releaseName string) error {
return h.runHelm("status", releaseName)
}
// StatusReleases returns the status of all installed releases
func (h *HelmCLI) StatusReleases() (map[string]string, error) {
output, err := h.ListCharts()
if err != nil {
return nil, errors.Wrap(err, "failed to list the installed chart releases")
}
lines := strings.Split(output, "\n")
statusMap := map[string]string{}
for _, line := range lines[1:] {
fields := strings.Split(line, "\t")
if len(fields) > 3 {
release := strings.TrimSpace(fields[0])
status := strings.TrimSpace(fields[3])
statusMap[release] = status
}
}
return statusMap, nil
}
// Lint lints the helm chart from the current working directory and returns the warnings in the output
func (h *HelmCLI) Lint() (string, error) {
return h.runHelmWithOutput("lint")
}
// Env returns the environment variables for the helmer
func (h *HelmCLI) Env() map[string]string {
return h.Runner.Env
}
// Version executes the helm version command and returns its output
func (h *HelmCLI) Version(tls bool) (string, error) {
return h.VersionWithArgs(tls)
}
// Version executes the helm version command and returns its output
func (h *HelmCLI) VersionWithArgs(tls bool, extraArgs ...string) (string, error) {
args := []string{"version", "--short"}
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)
}