forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm_cli.go
368 lines (339 loc) · 9.88 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
package helm
import (
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"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
}
// NewHelmCLI creates a new HelmCLI instance configured to used the provided helm CLI in
// the given current working directory
func NewHelmCLI(binary string, version Version, cwd string, 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
}
// 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")
}
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)
}
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)
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")
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")
}
// Version executes the helm version command and returns its output
func (h *HelmCLI) Version(tls bool) (string, error) {
args := []string{}
args = append(args, "version", "--short")
if tls {
args = append(args, "--tls")
}
return h.runHelmWithOutput(args...)
}
// PackageChart packages the chart from the current working directory
func (h *HelmCLI) PackageChart() error {
return h.runHelm("package", h.CWD)
}