-
Notifications
You must be signed in to change notification settings - Fork 51
/
resolve.go
1184 lines (1064 loc) · 36.1 KB
/
resolve.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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package resolve
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/jenkins-x-plugins/jx-gitops/pkg/apis/gitops/v1alpha1"
"github.com/jenkins-x-plugins/jx-gitops/pkg/helmfiles"
"github.com/jenkins-x-plugins/jx-gitops/pkg/jxtmpl/reqvalues"
"github.com/jenkins-x-plugins/jx-gitops/pkg/pipelinecatalogs"
"github.com/jenkins-x-plugins/jx-gitops/pkg/quickstarthelpers"
"github.com/jenkins-x/jx-helpers/v3/pkg/termcolor"
"github.com/jenkins-x/jx-helpers/v3/pkg/yamls"
"github.com/helmfile/helmfile/pkg/state"
"github.com/jenkins-x-plugins/jx-gitops/pkg/cmd/helmfile/structure"
"github.com/jenkins-x-plugins/jx-gitops/pkg/helmhelpers"
"github.com/jenkins-x-plugins/jx-gitops/pkg/plugins"
"github.com/jenkins-x-plugins/jx-gitops/pkg/versionstreamer"
jxcore "github.com/jenkins-x/jx-api/v4/pkg/apis/core/v4beta1"
"github.com/jenkins-x/jx-helpers/v3/pkg/cmdrunner"
"github.com/jenkins-x/jx-helpers/v3/pkg/files"
"github.com/jenkins-x/jx-helpers/v3/pkg/gitclient"
"github.com/jenkins-x/jx-helpers/v3/pkg/gitclient/cli"
"github.com/jenkins-x/jx-helpers/v3/pkg/stringhelpers"
"github.com/jenkins-x/jx-helpers/v3/pkg/yaml2s"
"github.com/jenkins-x-plugins/jx-gitops/pkg/rootcmd"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/helper"
"github.com/jenkins-x/jx-helpers/v3/pkg/cobras/templates"
"github.com/jenkins-x/jx-helpers/v3/pkg/versionstream"
"github.com/jenkins-x/jx-logging/v3/pkg/log"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
const (
versionStreamDir = "versionStream"
useHelmfileRepos = false
)
var (
info = termcolor.ColorInfo
cmdLong = templates.LongDesc(`
Resolves the helmfile.yaml from the version stream to specify versions and helm values
`)
cmdExample = templates.Examples(`
# resolves the versions and values in the helmfile.yaml
%s helmfile resolve
`)
valueFileNames = []string{"values.yaml.gotmpl", "values.yaml"}
)
// Options the options for the command
type Options struct {
versionstreamer.Options
Namespace string
GitCommitMessage string
Helmfile string
Helmfiles []helmfiles.Helmfile
KptBinary string
HelmfileBinary string
HelmBinary string
BatchMode bool
UpdateMode bool
DoGitCommit bool
TestOutOfCluster bool
Gitter gitclient.Interface
prefixes *versionstream.RepositoryPrefixes
Results Results
AddEnvironmentPipelines bool
}
type Results struct {
RequirementsValuesFileName string
}
// NewCmdHelmfileResolve creates a command object for the command
func NewCmdHelmfileResolve() (*cobra.Command, *Options) {
o := &Options{}
cmd := &cobra.Command{
Use: "resolve",
Short: "Resolves any missing versions or values files in the helmfile.yaml file from the version stream",
Long: cmdLong,
Example: fmt.Sprintf(cmdExample, rootcmd.BinaryName),
Run: func(cmd *cobra.Command, args []string) {
err := o.Run()
helper.CheckErr(err)
},
}
o.BaseOptions.AddBaseFlags(cmd)
cmd.Flags().BoolVarP(&o.UpdateMode, "update", "", false, "updates versions from the version stream if they have changed")
if useHelmfileRepos {
cmd.Flags().StringVarP(&o.HelmfileBinary, "helmfile-binary", "", "", "specifies the helmfile binary location to use. If not specified defaults to using the downloaded helmfile plugin")
}
cmd.Flags().StringVarP(&o.HelmBinary, "helm-binary", "", "", "specifies the helm binary location to use. If not specified defaults to using the downloaded helm plugin")
o.AddFlags(cmd, "")
return cmd, o
}
func (o *Options) AddFlags(cmd *cobra.Command, prefix string) {
cmd.Flags().StringVarP(&o.Helmfile, "helmfile", "", "", "the helmfile to resolve. If not specified defaults to 'helmfile.yaml' in the dir")
cmd.Flags().StringVarP(&o.GitCommitMessage, prefix+"commit-message", "", "chore: generated kubernetes resources from helm chart", "the git commit message used")
cmd.Flags().StringVarP(&o.Namespace, "namespace", "", "jx", "the default namespace if none is specified in the helmfile.yaml")
cmd.Flags().BoolVarP(&o.AddEnvironmentPipelines, "add-environment-pipelines", "", false, "skips the custom upgrade step for adding .lighthouse folder")
// git commit stuff....
cmd.Flags().BoolVarP(&o.DoGitCommit, prefix+"git-commit", "", false, "if set then the template command will git commit the modified helmfile.yaml files")
cmd.Flags().StringVarP(&o.VersionStreamDir, "version-stream-dir", "", "", "the directory for the version stream. Defaults to 'versionStream' in the current --dir")
}
// Validate validates the options and populates any missing values
func (o *Options) Validate() error {
err := o.Options.Validate()
if err != nil {
return errors.Wrapf(err, "failed to ")
}
if o.Helmfile == "" {
o.Helmfile = "helmfile.yaml"
}
if useHelmfileRepos {
if o.HelmfileBinary == "" {
o.HelmfileBinary, err = plugins.GetHelmfileBinary(plugins.HelmfileVersion)
if err != nil {
return errors.Wrapf(err, "failed to download helmfile plugin")
}
}
}
if o.HelmBinary == "" {
o.HelmBinary, err = plugins.GetHelmBinary(plugins.HelmVersion)
if err != nil {
return errors.Wrapf(err, "failed to download helm plugin")
}
}
if o.Dir == "" {
o.Dir = "."
}
helmfiles, err := helmfiles.GatherHelmfiles(o.Helmfile, o.Dir)
if err != nil {
return errors.Wrapf(err, "failed to gather nested helmfiles")
}
o.Helmfiles = helmfiles
if o.GitCommitMessage == "" {
o.GitCommitMessage = "chore: resolved charts and values from the version stream"
}
o.prefixes, err = o.Options.Resolver.GetRepositoryPrefixes()
if err != nil {
return errors.Wrapf(err, "failed to load repository prefixes at %s", o.VersionStreamDir)
}
if o.CommandRunner == nil {
o.CommandRunner = cmdrunner.QuietCommandRunner
}
return nil
}
// Run implements the command
func (o *Options) Run() error {
err := o.Validate()
if err != nil {
return errors.Wrapf(err, "failed to ")
}
resolver := o.Options.Resolver
if resolver == nil {
return errors.Errorf("failed to create the VersionResolver")
}
count := 0
if o.UpdateMode {
increment, err := o.upgradeHelmfileStructure(o.Dir)
count += increment
if err != nil {
return errors.Wrapf(err, "failed to perform custom upgrades")
}
err = o.upgradePipelineCatalog()
if err != nil {
return errors.Wrapf(err, "failed to upgrade pipeline catalog")
}
}
if useHelmfileRepos {
// lets add the helm repositories
args := []string{}
if o.HelmBinary != "" {
args = append(args, "--helm-binary", o.HelmBinary)
}
args = append(args, "repos")
c := &cmdrunner.Command{
Dir: o.Dir,
Name: o.HelmfileBinary,
Args: args,
}
_, err = o.QuietCommandRunner(c)
if err != nil {
return errors.Wrapf(err, "failed to run command %s in dir %s", c.CLI(), o.Dir)
}
}
helmfiles, err := helmfiles.GatherHelmfiles(o.Helmfile, o.Dir)
if err != nil {
return errors.Wrapf(err, "error gathering helmfiles")
}
for _, helmfile := range helmfiles {
err := o.processHelmfile(helmfile)
if err != nil {
return errors.Wrapf(err, "failed to process helmfile %s", helmfile.Filepath)
}
// ToDo: What are we trying to do here?
count += 0
}
if !o.DoGitCommit {
return nil
}
if count > 0 {
log.Logger().Infof("committing changes: %s", o.GitCommitMessage)
err = o.GitCommit(o.Dir, o.GitCommitMessage)
if err != nil {
return errors.Wrapf(err, "failed to commit changes")
}
}
return nil
}
func (o *Options) processHelmfile(helmfile helmfiles.Helmfile) error {
helmState := state.HelmState{}
path := helmfile.Filepath
err := yaml2s.LoadFile(path, &helmState)
if err != nil {
return errors.Wrapf(err, "failed to load helmfile %s", helmfile)
}
if o.UpdateMode {
err = o.CustomUpgrades(&helmState)
if err != nil {
return errors.Wrapf(err, "failed to perform custom upgrades")
}
}
if helmfile.RelativePathToRoot != "" {
helmfileDir := filepath.Dir(path)
ns := helmState.OverrideNamespace
if ns == "" {
_, ns = filepath.Split(helmfileDir)
}
err := o.saveNamespaceJXValuesFile(helmfileDir, ns)
if err != nil {
return errors.Wrapf(err, "failed to generate jx-values.yaml for namespace %s", ns)
}
}
err = o.resolveHelmfile(&helmState, helmfile)
if err != nil {
return errors.Wrapf(err, "failed to resolve helmfile %s", helmfile)
}
err = yaml2s.SaveFile(helmState, path)
if err != nil {
return errors.Wrapf(err, "failed to save file %s", helmfile)
}
return nil
}
func (o *Options) saveNamespaceJXValuesFile(helmfileDir, ns string) error {
jxReqValuesFileName := filepath.Join(helmfileDir, reqvalues.RequirementsValuesFileName)
o.Results.RequirementsValuesFileName = reqvalues.RequirementsValuesFileName
requirements := *o.Options.Requirements
subDomain := strings.ReplaceAll(requirements.Ingress.NamespaceSubDomain, "jx", ns)
requirements.Ingress.NamespaceSubDomain = subDomain
envKey := ""
for k := range requirements.Environments {
e := requirements.Environments[k]
if (ns == e.Namespace) || (strings.TrimPrefix(ns, "jx-") == e.Key) {
envKey = e.Key
break
}
}
// lets see if there is a custom ingress value for this namespace
for k := range requirements.Environments {
e := requirements.Environments[k]
if e.Ingress != nil && e.Key == envKey {
requirements.Ingress = *e.Ingress
if requirements.Ingress.NamespaceSubDomain == "" {
requirements.Ingress.NamespaceSubDomain = subDomain
}
if requirements.Ingress.Domain == "" {
requirements.Ingress.Domain = o.Options.Requirements.Ingress.Domain
}
}
}
// lets make sure we use a default domain name to avoid the validation of the Ingress
// resources from failing
if requirements.Ingress.Domain == "" {
requirements.Ingress.Domain = v1alpha1.DomainPlaceholder
}
err := reqvalues.SaveRequirementsValuesFile(&requirements, o.Dir, jxReqValuesFileName)
if err != nil {
return errors.Wrapf(err, "failed to save jx-values.yaml file")
}
return nil
}
func (o *Options) upgradeHelmfileStructure(dir string) (int, error) {
count := 0
err := o.processHelmfile(o.Helmfiles[0])
if err != nil {
return 0, errors.Wrapf(err, "error processing parent helmfile before restructure")
}
count += 0
if exists, _ := files.DirExists(filepath.Join(dir, structure.HelmfileFolder)); !exists {
so := structure.Options{
Dir: dir,
}
err = so.Run()
if err != nil {
return 0, errors.Wrapf(err, "error restructuring helmfiles during resolve upgrade")
}
}
count++
helmfiles, err := helmfiles.GatherHelmfiles(o.Helmfile, o.Dir)
if err != nil {
return 0, errors.Wrapf(err, "error gathering helmfiles")
}
o.Helmfiles = helmfiles
err = gitclient.Add(o.Git(), o.Dir, "helmfiles")
if err != nil {
return count, errors.Wrapf(err, "failed to add files to git")
}
return count, nil
}
func (o *Options) resolveHelmfile(helmState *state.HelmState, helmfile helmfiles.Helmfile) error {
var err error
var ignoreRepositories []string
if !helmhelpers.IsInCluster() || o.TestOutOfCluster {
ignoreRepositories, err = helmhelpers.FindClusterLocalRepositoryURLs(helmState.Repositories)
if err != nil {
return errors.Wrapf(err, "failed to find cluster local repositories")
}
}
if helmfile.RelativePathToRoot != "" {
// ensure we have added the jx-values.yaml file in the envirionment
if helmState.Environments == nil {
helmState.Environments = map[string]state.EnvironmentSpec{}
}
// lets remove any old legacy files in the root dir
oldFiles := []string{
filepath.Join("..", "..", reqvalues.RequirementsValuesFileName),
filepath.Join("..", "..", "versionStream", "src", "fake-secrets.yaml.gotmpl"),
}
envSpec := helmState.Environments["default"]
for _, f := range oldFiles {
for i, v := range envSpec.Values {
s, ok := v.(string)
if ok && s == f {
newValues := envSpec.Values[0:i]
if len(envSpec.Values) > i+1 {
newValues = append(newValues, envSpec.Values[i+1:]...)
}
envSpec.Values = newValues
helmState.Environments["default"] = envSpec
break
}
}
}
envSpec = helmState.Environments["default"]
foundValuesFile := false
for _, v := range envSpec.Values {
s, ok := v.(string)
if ok && s == reqvalues.RequirementsValuesFileName {
foundValuesFile = true
break
}
}
if !foundValuesFile {
envValue := helmState.Environments["default"]
envValue.Values = append(envValue.Values, reqvalues.RequirementsValuesFileName)
helmState.Environments["default"] = envValue
}
}
for i := range helmState.Releases {
release := helmState.Releases[i]
repository := ""
fullChartName := release.Chart
parts := strings.Split(fullChartName, "/")
prefix := ""
chartName := release.Chart
if len(parts) > 1 {
prefix = parts[0]
chartName = parts[1]
}
if release.Name == "" {
release.Name = chartName
}
// ignore remote charts
if strings.Contains(fullChartName, "::") {
log.Logger().Debugf("ignoring remote chart %s release %s", fullChartName, release.Name)
continue
}
// lets not try resolve repository / versions for local charts
if prefix != "." && prefix != ".." {
// lets resolve the chart prefix from a local repository from the file or from a
// prefix in the versions stream
if prefix != "" {
for k := range helmState.Repositories {
r := helmState.Repositories[k]
if r.Name == prefix {
repository = r.URL
}
}
}
if repository == "" && prefix != "" {
repository, err = versionstreamer.MatchRepositoryPrefix(o.prefixes, prefix)
if err != nil {
return errors.Wrapf(err, "failed to match prefix %s with repositories from versionstream %s", prefix, o.VersionStreamURL)
}
}
if repository == "" && prefix != "" {
return errors.Wrapf(err, "failed to find repository URL, not defined in helmfile.yaml or versionstream %s", o.VersionStreamURL)
}
if repository != "" && prefix != "" {
// lets ensure we've got a repository for this URL in the apps file
found := false
for k := range helmState.Repositories {
r := helmState.Repositories[k]
if r.Name == prefix {
if r.URL != repository {
return errors.Errorf("release %s has prefix %s for repository URL %s which is also mapped to prefix %s", release.Name, prefix, r.URL, r.Name)
}
found = true
break
}
}
if !found {
helmState.Repositories = append(helmState.Repositories, state.RepositorySpec{
Name: prefix,
URL: repository,
})
}
}
// lets look for an override version label
if stringhelpers.StringArrayIndex(ignoreRepositories, repository) < 0 && !IsLabelValue(&release, helmhelpers.VersionLabel, helmhelpers.LockLabelValue) {
// first try and match using the prefix and release name as we might have a version stream folder that uses helm alias
versionProperties, err := o.Options.Resolver.StableVersion(versionstream.KindChart, prefix+"/"+release.Name)
if err != nil {
return errors.Wrapf(err, "failed to find version number for chart %s", release.Name)
}
// lets fall back to using the full chart name
if versionProperties.Version == "" {
versionProperties, err = o.Options.Resolver.StableVersion(versionstream.KindChart, fullChartName)
if err != nil {
return errors.Wrapf(err, "failed to find version number for chart %s", fullChartName)
}
}
version := versionProperties.Version
if release.Version == "" && version == "" {
log.Logger().Debugf("could not find version for chart %s so using latest found in helm repository %s", fullChartName, repository)
}
versionChanged := false
if release.Version == "" {
release.Version = version
versionChanged = true
} else if o.UpdateMode && release.Version != version && version != "" {
release.Version = version
versionChanged = true
}
if versionChanged {
log.Logger().Debugf("resolved chart %s version %s", fullChartName, version)
}
if release.Namespace == "" && helmState.OverrideNamespace == "" && versionProperties.Namespace != "" {
release.Namespace = versionProperties.Namespace
}
}
}
if release.Namespace == "" && helmState.OverrideNamespace == "" {
release.Namespace = o.Namespace
if release.Namespace == "" {
release.Namespace = jxcore.DefaultNamespace
}
}
releaseNames := []string{chartName}
if release.Name != "" && release.Name != chartName {
releaseNames = []string{release.Name, chartName}
}
// lets try resolve any values files in the version stream using the prefix and chart name first
if !IsLabelValue(&release, helmhelpers.ValuesLabel, helmhelpers.LockLabelValue) {
found, err := o.addValues(helmfile, filepath.Join(prefix, release.Name), &release)
if err != nil {
return errors.Wrapf(err, "failed to add values")
}
if !found {
// next try the full chart name
// ToDo: use this found value
found, err = o.addValues(helmfile, fullChartName, &release) //nolint:ineffassign,staticcheck
if err != nil {
return errors.Wrapf(err, "failed to add values")
}
}
// lets try discover any local files
found = false
for _, releaseName := range releaseNames {
for _, valueFileName := range valueFileNames {
path := filepath.Join(helmfile.RelativePathToRoot, "values", releaseName, valueFileName)
appValuesFile := filepath.Join(o.Dir, path)
exists, err := files.FileExists(appValuesFile)
if err != nil {
return errors.Wrapf(err, "failed to check if release values file exists %s", appValuesFile)
}
if exists {
if !valuesContains(release.Values, path) {
release.Values = append(release.Values, path)
}
found = true
break
}
}
if found {
break
}
}
if helmfile.RelativePathToRoot != "" {
foundValuesFile := false
for _, v := range release.Values {
s, ok := v.(string)
if ok && s == reqvalues.RequirementsValuesFileName {
foundValuesFile = true
break
}
}
if !foundValuesFile {
release.Values = append(release.Values, reqvalues.RequirementsValuesFileName)
}
}
}
helmState.Releases[i] = release
}
return nil
}
// IsLabelValue returns true if the release is labelled with the given label with a value
func IsLabelValue(release *state.ReleaseSpec, label, value string) bool {
answer := false
if release.Labels != nil {
lockVersionValue := strings.TrimSpace(release.Labels[label])
if lockVersionValue == value {
answer = true
}
}
return answer
}
func (o *Options) addValues(helmfile helmfiles.Helmfile, name string, release *state.ReleaseSpec) (bool, error) {
found := false
for _, valueFileName := range valueFileNames {
versionStreamValuesFile := filepath.Join(o.Resolver.VersionsDir, "charts", name, valueFileName)
exists, err := files.FileExists(versionStreamValuesFile)
if err != nil {
return false, errors.Wrapf(err, "failed to check if version stream values file exists %s", versionStreamValuesFile)
}
if exists {
path := filepath.Join(helmfile.RelativePathToRoot, versionStreamDir, "charts", name, valueFileName)
if !valuesContains(release.Values, path) {
release.Values = append(release.Values, path)
}
found = true
break
}
if found {
break
}
}
return found, nil
}
// HasHelmfile returns true if there is a helmfile
func (o *Options) HasHelmfile() (bool, error) {
if o.Helmfile == "" {
o.Helmfile = filepath.Join(o.Dir, "helmfile.yaml")
} else {
o.Helmfile = filepath.Join(o.Dir, o.Helmfile)
}
exists, err := files.FileExists(o.Helmfile)
if err != nil {
return false, errors.Wrapf(err, "failed to check for file %s", o.Helmfile)
}
return exists, nil
}
func valuesContains(values []interface{}, value string) bool {
for _, v := range values {
if v == value {
return true
}
}
return false
}
// Git returns the gitter - lazily creating one if required
func (o *Options) Git() gitclient.Interface {
if o.Gitter == nil {
o.Gitter = cli.NewCLIClient("", o.CommandRunner)
}
return o.Gitter
}
func (o *Options) GitCommit(outDir, commitMessage string) error {
gitter := o.Git()
_, err := gitter.Command(outDir, "add", "*")
if err != nil {
return errors.Wrapf(err, "failed to add generated resources to git in dir %s", outDir)
}
err = gitclient.CommitIfChanges(gitter, outDir, commitMessage)
if err != nil {
return errors.Wrapf(err, "failed to commit changes to git in dir %s", outDir)
}
return nil
}
// CustomUpgrades performs custom upgrades outside of the version stream/kpt approach
func (o *Options) CustomUpgrades(helmstate *state.HelmState) error {
err := o.migrateRequirementsToV4()
if err != nil {
return errors.Wrapf(err, "failed to migrate jx-requirements.yml")
}
err = o.renameImagePullSecretsFile()
if err != nil {
return errors.Wrapf(err, "failed to rename old image pull secrets file")
}
err = o.migrateQuickstartsFile()
if err != nil {
return errors.Wrapf(err, "failed to migrate quickstarts file")
}
var versionStreamPath string
if helmstate.OverrideNamespace == "" {
versionStreamPath = "versionStream"
} else {
versionStreamPath = "../../versionStream"
// lets remove the old top level jx-values.yaml as we are using multi-level helmfiles
oldJXValues := filepath.Join(o.Dir, "jx-values.yaml")
exists, err := files.FileExists(oldJXValues)
if err != nil {
return errors.Wrapf(err, "failed to check if file exists %s", oldJXValues)
}
if exists {
err = os.Remove(oldJXValues)
if err != nil {
return errors.Wrapf(err, "failed to remove old file %s", oldJXValues)
}
}
}
requirementsResource, _, err := jxcore.LoadRequirementsConfig(o.Dir, false)
if err != nil {
return errors.Wrapf(err, "failed to load the requirements configuration")
}
requirements := &requirementsResource.Spec
// lets replace the old tekton repositories if they are being used
for i := range helmstate.Repositories {
repo := &helmstate.Repositories[i]
cleanURL := strings.TrimSuffix(repo.URL, "/")
switch cleanURL {
case "https://kubernetes-charts.storage.googleapis.com":
repo.URL = "https://charts.helm.sh/stable"
case "https://comcast.github.io/kuberhealthy/helm-repos":
repo.URL = "https://kuberhealthy.github.io/kuberhealthy/helm-repos"
case "https://godaddy.github.io/kubernetes-external-secrets":
repo.URL = "https://external-secrets.github.io/kubernetes-external-secrets"
case "https://chrismellard.github.io/kubernetes-external-secrets":
repo.URL = "https://external-secrets.github.io/kubernetes-external-secrets"
case "https://storage.googleapis.com/jenkinsxio/charts":
if repo.Name == "external-secrets" {
repo.URL = "https://external-secrets.github.io/kubernetes-external-secrets"
}
case "http://chartmuseum.jenkins-x.io":
repo.URL = "https://storage.googleapis.com/chartmuseum.jenkins-x.io"
}
}
// lets replace the old tekton chart if its being used
for i := range helmstate.Releases {
release := &helmstate.Releases[i]
if release.Chart == "jenkins-x/tekton" {
release.Chart = "cdf/tekton-pipeline"
release.Namespace = "tekton-pipelines"
// lets make sure we have a cdf repository
found := false
for k := range helmstate.Repositories {
repo := helmstate.Repositories[k]
if repo.Name == "cdf" {
found = true
break
}
}
if !found {
helmstate.Repositories = append(helmstate.Repositories, state.RepositorySpec{
Name: "cdf",
URL: "https://cdfoundation.github.io/tekton-helm-chart",
})
}
break
}
}
addedJxgh := false
// lets replace the old jx3 charts
for i := range helmstate.Releases {
release := &helmstate.Releases[i]
names := strings.SplitN(release.Chart, "/", 2)
if len(names) == 2 && names[0] == "jx3" {
chartName := names[1]
name := release.Name
release.Chart = "jxgh/" + chartName
addedJxgh = true
for j := range release.Values {
v := release.Values[j]
s, ok := v.(string)
if !ok {
continue
}
// lets switch invalid paths to the one inside a chart repo folder
if s == fmt.Sprintf("%s/charts/jx3/%s/values.yaml.gotmpl", versionStreamPath, chartName) {
release.Values[j] = fmt.Sprintf("%s/charts/jxgh/%s/values.yaml.gotmpl", versionStreamPath, chartName)
break
}
if s == fmt.Sprintf("%s/charts/jx3/%s/values.yaml.gotmpl", versionStreamPath, name) {
release.Values[j] = fmt.Sprintf("%s/charts/jxgh/%s/values.yaml.gotmpl", versionStreamPath, name)
break
}
}
}
}
// lets replace the old jenkins-x charts
for _, chartName := range []string{"jxboot-helmfile-resources", "bucketrepo", "nexus"} {
for i := range helmstate.Releases {
release := &helmstate.Releases[i]
if release.Chart == "jenkins-x/"+chartName {
release.Chart = "jxgh/" + chartName
addedJxgh = true
for j := range release.Values {
v := release.Values[j]
s, ok := v.(string)
// lets switch invalid paths to the one inside a chart repo folder
if ok && s == fmt.Sprintf("%s/charts/jenkins-x/%s/values.yaml.gotmpl", versionStreamPath, chartName) {
release.Values[j] = fmt.Sprintf("%s/charts/jxgh/%s/values.yaml.gotmpl", versionStreamPath, chartName)
break
}
}
}
}
}
// lets replace the old lighthouse chart repository location if its being used
for i := range helmstate.Releases {
release := &helmstate.Releases[i]
if release.Chart == "jenkins-x/lighthouse" {
release.Chart = "jxgh/lighthouse"
addedJxgh = true
for i := range release.Values {
v := release.Values[i]
s, ok := v.(string)
// lets switch invalid paths to the one inside a chart repo folder
if ok && s == fmt.Sprintf("%s/charts/jenkins-x/lighthouse/values.yaml.gotmpl", versionStreamPath) {
release.Values[i] = fmt.Sprintf("%s/charts/jxgh/lighthouse/values.yaml.gotmpl", versionStreamPath)
break
}
}
}
}
// lets check for terraform installed vault
if requirements.SecretStorage == jxcore.SecretStorageTypeVault && requirements.TerraformVault {
for i := range helmstate.Releases {
release := &helmstate.Releases[i]
if release.Chart == "jxgh/vault-instance" || release.Chart == "banzaicloud-stable/vault-operator" {
log.Logger().Infof("Terraform installed detected and Vault chart %s still present in helmfile. Please migrate secrets as necessary and remove this chart from your helmfile", release.Chart)
}
}
}
// lets replace the old chartmuseum chart if its being used
for i := range helmstate.Releases {
release := &helmstate.Releases[i]
if release.Chart == "jenkins-x/chartmuseum" {
release.Chart = "stable/chartmuseum"
o.updateVersionFromVersionStream(release)
release.Values = []interface{}{fmt.Sprintf("%s/charts/stable/chartmuseum/values.yaml.gotmpl", versionStreamPath)}
// lets make sure we have a cdf repository
found := false
for k := range helmstate.Repositories {
repo := helmstate.Repositories[k]
if repo.Name == "stable" {
found = true
break
}
}
if !found {
helmstate.Repositories = append(helmstate.Repositories, state.RepositorySpec{
Name: "stable",
URL: "https://charts.helm.sh/stable",
})
}
break
}
}
// Replace old nginx ingress chart
for i := range helmstate.Releases {
release := &helmstate.Releases[i]
switch release.Chart {
case "stable/nginx-ingress":
release.Chart = "ingress-nginx/ingress-nginx"
o.updateVersionFromVersionStream(release)
release.Values = []interface{}{fmt.Sprintf("%s/charts/ingress-nginx/ingress-nginx/values.yaml.gotmpl", versionStreamPath)}
// lets make sure we have the ingress-nginx repository
found := false
for k := range helmstate.Repositories {
repo := helmstate.Repositories[k]
if repo.Name == "ingress-nginx" {
found = true
break
}
}
if !found {
helmstate.Repositories = append(helmstate.Repositories, state.RepositorySpec{
Name: "ingress-nginx",
URL: "https://kubernetes.github.io/ingress-nginx",
})
}
case "ingress-nginx/ingress-nginx":
for i := range release.Values {
v := release.Values[i]
s, ok := v.(string)
// lets switch invalid paths to the one inside a chart repo folder
if ok && s == fmt.Sprintf("%s/charts/ingress-nginx/values.yaml.gotmpl", versionStreamPath) {
release.Values[i] = fmt.Sprintf("%s/charts/ingress-nginx/ingress-nginx/values.yaml.gotmpl", versionStreamPath)
break
}
}
}
}
ns := jxcore.DefaultNamespace
if requirements.SecretStorage == jxcore.SecretStorageTypeLocal && helmstate.OverrideNamespace == ns {
// lets make sure the local external secrets chart is included
found := false
for i := range helmstate.Releases {
release := &helmstate.Releases[i]
if release.Chart == "jxgh/local-external-secrets" {
found = true
break
}
}
if !found {
release := state.ReleaseSpec{
Chart: "jxgh/local-external-secrets",
}
addedJxgh = true
o.updateVersionFromVersionStream(&release)
helmstate.Releases = append(helmstate.Releases, release)
}
}
// lets replace the old jx-labs/ charts...
for _, name := range []string{"jenkins-x-crds", "pusher-wave", "vault-instance"} {
chartName := "jx-labs/" + name
for i := range helmstate.Releases {
release := &helmstate.Releases[i]
if release.Chart == chartName {
release.Chart = "jxgh/" + name
addedJxgh = true
if name == "jenkins-x-crds" {
release.Values = []interface{}{fmt.Sprintf("%s/charts/jxgh/jenkins-x-crds/values.yaml.gotmpl", versionStreamPath)}
}
o.updateVersionFromVersionStream(release)
break
}
}
}
// remove jx-labs repository if we have no more charts left using the prefix
jxLabsCount := 0
for i := range helmstate.Releases {
release := &helmstate.Releases[i]
if strings.HasPrefix(release.Chart, "jx-labs/") {
jxLabsCount++
}
}
if jxLabsCount == 0 {
for i := range helmstate.Repositories {
if helmstate.Repositories[i].Name == "jx-labs" {
helmstate.Repositories = append(helmstate.Repositories[0:i], helmstate.Repositories[i+1:]...)
break
}
}
}
// lets ensure we have the jx-build-controller installed
if helmstate.OverrideNamespace == "jx" && isDevCluster(helmstate) {
found := false
for i := range helmstate.Releases {
release := &helmstate.Releases[i]
if release.Chart == "jxgh/jx-build-controller" {
found = true
break
}
}
if !found && helmstate.OverrideNamespace == "jx" {
helmstate.Releases = append(helmstate.Releases, state.ReleaseSpec{
Chart: "jxgh/jx-build-controller",
})
addedJxgh = true
}
}
if addedJxgh {
// lets make sure we have a jxgh repository
found := false
for k := range helmstate.Repositories {
repo := helmstate.Repositories[k]
if repo.Name == "jxgh" {
found = true
break
}
}
if !found {
helmstate.Repositories = append(helmstate.Repositories, state.RepositorySpec{
Name: "jxgh",
URL: "https://jenkins-x-charts.github.io/repo",
})
}
}
// let's remove any unused jx3 repo
removeRedundantRepositories(helmstate)
// TODO lets remove the jx-labs repository if its no longer referenced...
if o.AddEnvironmentPipelines {
lighthouseTriggerFile := filepath.Join(o.Dir, ".lighthouse", "jenkins-x", "triggers.yaml")
exists, err := files.FileExists(lighthouseTriggerFile)
if err != nil {
return errors.Wrapf(err, "failed to detect file %s", lighthouseTriggerFile)
}
if !exists {
bin := o.KptBinary
if bin == "" {
bin, err = plugins.GetKptBinary(plugins.KptVersion)
if err != nil {