-
Notifications
You must be signed in to change notification settings - Fork 787
/
common_helm.go
509 lines (457 loc) · 14.7 KB
/
common_helm.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
package cmd
import (
"context"
"fmt"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/jenkins-x/jx/pkg/helm"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/kube/services"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/jenkins-x/jx/pkg/version"
"github.com/pkg/errors"
"gopkg.in/AlecAivazis/survey.v1"
"gopkg.in/src-d/go-git.v4"
gitconfig "gopkg.in/src-d/go-git.v4/config"
)
func (o *CommonOptions) registerLocalHelmRepo(repoName, ns string) error {
if repoName == "" {
repoName = kube.LocalHelmRepoName
}
// TODO we should use the auth package to keep a list of server login/pwds
// TODO we have a chartmuseumAuth.yaml now but sure yet if that's the best thing to do
username := "admin"
password := "admin"
// lets check if we have a local helm repository
client, err := o.KubeClient()
if err != nil {
return errors.Wrap(err, "failed to create the kube client")
}
u, err := services.FindServiceURL(client, ns, kube.ServiceChartMuseum)
if err != nil {
return errors.Wrapf(err, "failed to find the service URL of the ChartMuseum")
}
u2, err := url.Parse(u)
if err != nil {
return errors.Wrap(err, "failed to parse the ChartMuseum URL")
}
if u2.User == nil {
u2.User = url.UserPassword(username, password)
}
helmUrl := u2.String()
// lets check if we already have the helm repo installed or if we need to add it or remove + add it
remove := false
repos, err := o.Helm().ListRepos()
if err != nil {
return errors.Wrap(err, "failed to list the repositories")
}
for repo, repoURL := range repos {
if repo == repoName {
if repoURL == helmUrl {
return nil
} else {
remove = true
}
}
}
if remove {
err = o.Helm().RemoveRepo(repoName)
if err != nil {
return errors.Wrapf(err, "failed to remove the repository '%s'", repoName)
}
}
return o.Helm().AddRepo(repoName, helmUrl, "", "")
}
// addHelmRepoIfMissing adds the given helm repo if its not already added
func (o *CommonOptions) addHelmRepoIfMissing(helmUrl, repoName, username, password string) error {
return o.addHelmBinaryRepoIfMissing(helmUrl, repoName, username, password)
}
func (o *CommonOptions) addHelmBinaryRepoIfMissing(helmUrl, repoName, username, password string) error {
missing, err := o.Helm().IsRepoMissing(helmUrl)
if err != nil {
return errors.Wrapf(err, "failed to check if the repository with URL '%s' is missing", helmUrl)
}
if missing {
log.Infof("Adding missing Helm repo: %s %s\n", util.ColorInfo(repoName), util.ColorInfo(helmUrl))
err = o.Helm().AddRepo(repoName, helmUrl, username, password)
if err == nil {
log.Infof("Successfully added Helm repository %s.\n", repoName)
}
return errors.Wrapf(err, "failed to add the repository '%s' with URL '%s'", repoName, helmUrl)
}
return nil
}
// installChart installs the given chart
func (o *CommonOptions) installChart(releaseName string, chart string, version string, ns string, helmUpdate bool,
setValues []string, valueFiles []string, repo string) error {
return o.installChartOptions(helm.InstallChartOptions{ReleaseName: releaseName, Chart: chart, Version: version,
Ns: ns, HelmUpdate: helmUpdate, SetValues: setValues, ValueFiles: valueFiles, Repository: repo})
}
// installChartAt installs the given chart
func (o *CommonOptions) installChartAt(dir string, releaseName string, chart string, version string, ns string,
helmUpdate bool, setValues []string, valueFiles []string, repo string) error {
return o.installChartOptions(helm.InstallChartOptions{Dir: dir, ReleaseName: releaseName, Chart: chart,
Version: version, Ns: ns, HelmUpdate: helmUpdate, SetValues: setValues, ValueFiles: valueFiles, Repository: repo})
}
func (o *CommonOptions) installChartOptions(options helm.InstallChartOptions) error {
client, err := o.KubeClient()
if err != nil {
return err
}
if options.VersionsDir == "" {
options.VersionsDir, err = o.cloneJXVersionsRepo("")
}
return helm.InstallFromChartOptions(options, o.Helm(), client, defaultInstallTimeout)
}
// clones the jenkins-x versions repo to a local working dir
func (o *CommonOptions) cloneJXVersionsRepo(versionRepository string) (string, error) {
surveyOpts := survey.WithStdio(o.In, o.Out, o.Err)
configDir, err := util.ConfigDir()
if err != nil {
return "", fmt.Errorf("error determining config dir %v", err)
}
wrkDir := filepath.Join(configDir, "jenkins-x-versions")
o.Debugf("Current configuration dir: %s\n", configDir)
o.Debugf("versionRepository: %s\n", versionRepository)
if versionRepository == "" {
versionRepository = DefaultVersionsURL
}
// If the repo already exists let's try to fetch the latest version
if exists, err := util.DirExists(wrkDir); err == nil && exists {
repo, err := git.PlainOpen(wrkDir)
if err == nil {
remote, err := repo.Remote("origin")
if err == nil {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
err := remote.FetchContext(ctx, &git.FetchOptions{
RefSpecs: []gitconfig.RefSpec{
gitconfig.RefSpec("+refs/heads/master:refs/remotes/origin/master"),
},
})
// The repository is up to date
if err == git.NoErrAlreadyUpToDate {
return wrkDir, nil
}
if err == nil {
flag := false
if o.BatchMode {
flag = true
} else {
confirm := &survey.Confirm{
Message: "A local Jenkins X versions repository already exists, pull the latest?",
Default: true,
}
err := survey.AskOne(confirm, &flag, nil, surveyOpts)
if err != nil {
return wrkDir, err
}
}
if !flag {
return wrkDir, err
}
w, err := repo.Worktree()
if err == nil {
err := w.Pull(&git.PullOptions{RemoteName: "origin"})
if err != nil {
return "", errors.Wrap(err, "pulling the latest")
}
}
}
}
}
}
// If it exists a this stage most likely its content is not consistent
if exists, err := util.DirExists(wrkDir); err == nil && exists {
err := util.DeleteDirContents(wrkDir)
if err != nil {
return "", errors.Wrapf(err, "cleaning the content of %q dir", wrkDir)
}
}
log.Infof("Cloning the Jenkins X versions repo to %s\n", wrkDir)
_, err = git.PlainClone(wrkDir, false, &git.CloneOptions{
URL: versionRepository,
ReferenceName: "refs/heads/master",
SingleBranch: true,
Progress: o.Out,
})
if err != nil {
return "", errors.Wrapf(err, "cloning %q repository into %q dir", versionRepository, wrkDir)
}
return wrkDir, nil
}
// getVersionNumber returns the version number for the given kind and name or blank string if there is no locked version
func (o *CommonOptions) getVersionNumber(kind version.VersionKind, name string) (string, error) {
versionsDir, err := o.cloneJXVersionsRepo("")
if err != nil {
return "", err
}
return version.LoadStableVersionNumber(versionsDir, kind, name)
}
// deleteChart deletes the given chart
func (o *CommonOptions) deleteChart(releaseName string, purge bool) error {
_, ns, err := o.KubeClientAndNamespace()
if err != nil {
return err
}
return o.Helm().DeleteRelease(ns, releaseName, purge)
}
func (o *CommonOptions) FindHelmChart() (string, error) {
dir, err := os.Getwd()
if err != nil {
return "", errors.Wrap(err, "failed to get the current working directory")
}
o.Helm().SetCWD(dir)
return o.Helm().FindChart()
}
func (o *CommonOptions) DiscoverAppName() (string, error) {
answer := ""
chartFile, err := o.FindHelmChart()
if err != nil {
return answer, err
}
if chartFile != "" {
return helm.LoadChartName(chartFile)
}
gitInfo, err := o.Git().Info("")
if err != nil {
return answer, err
}
if gitInfo == nil {
return answer, fmt.Errorf("no git info found to discover app name from")
}
answer = gitInfo.Name
if answer == "" {
}
return answer, nil
}
func (o *CommonOptions) isHelmRepoMissing(helmUrlString string) (bool, error) {
return o.Helm().IsRepoMissing(helmUrlString)
}
func (o *CommonOptions) addChartRepos(dir string, helmBinary string, chartRepos map[string]string) error {
installedChartRepos, err := o.getInstalledChartRepos(helmBinary)
if err != nil {
return errors.Wrap(err, "failed to retrieve the install charts")
}
repoCounter := len(installedChartRepos)
if chartRepos != nil {
for name, url := range chartRepos {
if !util.StringMapHasValue(installedChartRepos, url) {
repoCounter++
err = o.addHelmBinaryRepoIfMissing(url, name, "", "")
if err != nil {
return errors.Wrapf(err, "failed to add the Helm repository with name '%s' and URL '%s'", name, url)
}
}
}
}
reqfile := filepath.Join(dir, "requirements.yaml")
exists, err := util.FileExists(reqfile)
if err != nil {
return errors.Wrapf(err, "requirements.yaml file not found in the chart directory '%s'", dir)
}
if exists {
requirements, err := helm.LoadRequirementsFile(reqfile)
if err != nil {
return errors.Wrap(err, "failed to load the Helm requirements file")
}
if requirements != nil {
for _, dep := range requirements.Dependencies {
repo := dep.Repository
if repo != "" && !util.StringMapHasValue(installedChartRepos, repo) && repo != defaultChartRepo && !strings.HasPrefix(repo, "file:") && !strings.HasPrefix(repo, "alias:") {
repoCounter++
// TODO we could provide some mechanism to customise the names of repos somehow?
err = o.addHelmBinaryRepoIfMissing(repo, "repo"+strconv.Itoa(repoCounter), "", "")
if err != nil {
return errors.Wrapf(err, "failed to add Helm repository '%s'", repo)
}
}
}
}
}
return nil
}
func (o *CommonOptions) getInstalledChartRepos(helmBinary string) (map[string]string, error) {
return o.Helm().ListRepos()
}
func (o *CommonOptions) helmInit(dir string) error {
o.Helm().SetCWD(dir)
if o.Helm().HelmBinary() == "helm" {
// need to check the tiller settings at this point
_, noTiller, helmTemplate, err := o.TeamHelmBin()
if err != nil {
return errors.Wrap(err, "failed to access team settings")
}
if noTiller || helmTemplate {
return o.Helm().Init(true, "", "", false)
} else {
return o.Helm().Init(false, "", "", true)
}
} else {
return o.Helm().Init(false, "", "", false)
}
}
func (o *CommonOptions) helmInitDependency(dir string, chartRepos map[string]string) (string, error) {
o.Helm().SetCWD(dir)
err := o.Helm().RemoveRequirementsLock()
if err != nil {
return o.Helm().HelmBinary(),
errors.Wrapf(err, "failed to remove requirements.lock file from chart '%s'", dir)
}
if o.Helm().HelmBinary() == "helm" {
// need to check the tiller settings at this point
_, noTiller, helmTemplate, err := o.TeamHelmBin()
if err != nil {
return o.Helm().HelmBinary(),
errors.Wrap(err, "failed to access team settings")
}
if noTiller || helmTemplate {
err = o.Helm().Init(true, "", "", false)
} else {
err = o.Helm().Init(false, "", "", true)
}
} else {
err = o.Helm().Init(false, "", "", false)
}
if err != nil {
return o.Helm().HelmBinary(),
errors.Wrap(err, "failed to initialize Helm")
}
err = o.addChartRepos(dir, o.Helm().HelmBinary(), chartRepos)
if err != nil {
return o.Helm().HelmBinary(),
errors.Wrap(err, "failed to add chart repositories")
}
return o.Helm().HelmBinary(), nil
}
func (o *CommonOptions) helmInitDependencyBuild(dir string, chartRepos map[string]string) (string, error) {
helmBin, err := o.helmInitDependency(dir, chartRepos)
if err != nil {
return helmBin, err
}
// TODO due to this issue: https://github.com/kubernetes/helm/issues/4230
// lets stick with helm2 for this step
//
helmBinary := o.Helm().HelmBinary()
o.Helm().SetHelmBinary("helm")
o.Helm().SetCWD(dir)
err = o.Helm().BuildDependency()
if err != nil {
return helmBinary, errors.Wrapf(err, "failed to build the dependencies of chart '%s'", dir)
}
o.Helm().SetHelmBinary(helmBinary)
_, err = o.Helm().Lint()
if err != nil {
return helmBinary, errors.Wrapf(err, "failed to lint the chart '%s'", dir)
}
return helmBinary, nil
}
func (o *CommonOptions) helmInitRecursiveDependencyBuild(dir string, chartRepos map[string]string) error {
_, err := o.helmInitDependency(dir, chartRepos)
if err != nil {
return errors.Wrap(err, "initializing Helm")
}
helmBinary := o.Helm().HelmBinary()
o.Helm().SetHelmBinary("helm")
o.Helm().SetCWD(dir)
err = o.Helm().BuildDependency()
if err != nil {
return errors.Wrapf(err, "failed to build the dependencies of chart '%s'", dir)
}
reqFilePath := filepath.Join(dir, "requirements.yaml")
reqs, err := helm.LoadRequirementsFile(reqFilePath)
if err != nil {
return errors.Wrap(err, "loading the requirements file")
}
type chartDep struct {
path string
deps []*helm.Dependency
}
baseChartPath := filepath.Join(dir, "charts")
depQueue := []chartDep{{
path: baseChartPath,
deps: reqs.Dependencies,
}}
for {
if len(depQueue) == 0 {
break
}
currChartDep := depQueue[0]
depQueue = depQueue[1:]
for _, dep := range currChartDep.deps {
chartArchive := filepath.Join(currChartDep.path, fmt.Sprintf("%s-%s.tgz", dep.Name, dep.Version))
chartPath := filepath.Join(currChartDep.path, dep.Name)
err := os.MkdirAll(chartPath, os.ModePerm)
if err != nil {
return errors.Wrap(err, "creating directory")
}
err = util.UnTargz(chartArchive, chartPath, []string{})
if err != nil {
return errors.Wrap(err, "extracting chart")
}
o.Helm().SetCWD(chartPath)
err = o.Helm().BuildDependency()
if err != nil {
return errors.Wrap(err, "building Helm dependency")
}
chartReqFile := filepath.Join(chartPath, "requirements.yaml")
reqs, err := helm.LoadRequirementsFile(chartReqFile)
if err != nil {
return errors.Wrap(err, "loading the requirements file")
}
if len(reqs.Dependencies) > 0 {
depQueue = append(depQueue, chartDep{
path: filepath.Join(chartPath, "charts"),
deps: reqs.Dependencies,
})
}
}
}
o.Helm().SetHelmBinary(helmBinary)
_, err = o.Helm().Lint()
if err != nil {
return errors.Wrapf(err, "linting the chart '%s'", dir)
}
return nil
}
func (o *CommonOptions) defaultReleaseCharts() map[string]string {
releasesURL := o.releaseChartMuseumUrl()
answer := map[string]string{
"jenkins-x": kube.DefaultChartMuseumURL,
}
if releasesURL != "" {
answer["releases"] = releasesURL
}
return answer
}
func (o *CommonOptions) releaseChartMuseumUrl() string {
chartRepo := os.Getenv("CHART_REPOSITORY")
if chartRepo == "" {
if o.IsInCDPipeline() {
chartRepo = defaultChartRepo
log.Warnf("No $CHART_REPOSITORY defined so using the default value of: %s\n", defaultChartRepo)
} else {
return ""
}
}
return chartRepo
}
func (o *CommonOptions) ensureHelm() error {
_, err := o.Helm().Version(false)
if err == nil {
return nil
}
err = o.installHelm()
if err != nil {
return errors.Wrap(err, "failed to install Helm")
}
initOpts := InitOptions{
CommonOptions: *o,
}
return initOpts.initHelm()
}