forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_helm.go
435 lines (393 loc) · 12.4 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
package cmd
import (
"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/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
)
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 := kube.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 string, repoName string) error {
return o.addHelmBinaryRepoIfMissing(helmUrl, repoName)
}
func (o *CommonOptions) addHelmBinaryRepoIfMissing(helmUrl string, repoName 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.retry(6, 10*time.Second, func() (err error) {
err = o.Helm().AddRepo(repoName, helmUrl)
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)
})
if err != nil {
return err
}
}
return nil
}
// installChart installs the given chart
func (o *CommonOptions) installChart(releaseName string, chart string, version string, ns string, helmUpdate bool, setValues []string) error {
return o.installChartOptions(InstallChartOptions{ReleaseName: releaseName, Chart: chart, Version: version, Ns: ns, HelmUpdate: helmUpdate, SetValues: setValues})
}
// installChartAt installs the given chart
func (o *CommonOptions) installChartAt(dir string, releaseName string, chart string, version string, ns string, helmUpdate bool, setValues []string) error {
return o.installChartOptions(InstallChartOptions{Dir: dir, ReleaseName: releaseName, Chart: chart, Version: version, Ns: ns, HelmUpdate: helmUpdate, SetValues: setValues})
}
type InstallChartOptions struct {
Dir string
ReleaseName string
Chart string
Version string
Ns string
HelmUpdate bool
SetValues []string
ValueFiles []string
}
func (o *CommonOptions) installChartOptions(options InstallChartOptions) error {
if options.HelmUpdate {
log.Infoln("Updating Helm repository...")
err := o.Helm().UpdateRepo()
if err != nil {
return errors.Wrap(err, "failed to update repository")
}
log.Infoln("Helm repository update done.")
}
if options.Ns != "" {
kubeClient, _, err := o.KubeClient()
if err != nil {
return errors.Wrap(err, "failed to create the kube client")
}
annotations := map[string]string{"jenkins-x.io/created-by": "Jenkins X"}
kube.EnsureNamespaceCreated(kubeClient, options.Ns, nil, annotations)
}
timeout, err := strconv.Atoi(defaultInstallTimeout)
if err != nil {
return errors.Wrap(err, "failed to convert the timeout to an int")
}
o.Helm().SetCWD(options.Dir)
return o.Helm().UpgradeChart(options.Chart, options.ReleaseName, options.Ns, &options.Version, true,
&timeout, true, false, options.SetValues, options.ValueFiles)
}
// deleteChart deletes the given chart
func (o *CommonOptions) deleteChart(releaseName string, purge bool) error {
_, ns, err := o.KubeClient()
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:") {
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)
_, err := o.Helm().Version(false)
if err != nil {
return errors.Wrap(err, "failed to read the Helm version")
}
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 chat '%s'", dir)
}
_, err = o.Helm().Version(false)
if err != nil {
return o.Helm().HelmBinary(),
errors.Wrap(err, "failed to read the Helm version")
}
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 {
return map[string]string{
"releases": o.releaseChartMuseumUrl(),
"jenkins-x": DEFAULT_CHARTMUSEUM_URL,
}
}
func (o *CommonOptions) releaseChartMuseumUrl() string {
chartRepo := os.Getenv("CHART_REPOSITORY")
if chartRepo == "" {
chartRepo = defaultChartRepo
log.Warnf("No $CHART_REPOSITORY defined so using the default value of: %s\n", defaultChartRepo)
}
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()
}