-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
1644 lines (1415 loc) · 44.5 KB
/
main.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 main
import (
"fmt"
"os"
"path"
"path/filepath"
"sort"
"strconv"
"strings"
"github.com/Masterminds/semver/v3"
"github.com/go-git/go-git/v5"
"github.com/rancher/charts-build-scripts/pkg/charts"
"github.com/rancher/charts-build-scripts/pkg/filesystem"
"github.com/samuelattwood/partner-charts-ci/pkg/conform"
"github.com/samuelattwood/partner-charts-ci/pkg/fetcher"
"github.com/samuelattwood/partner-charts-ci/pkg/parse"
"github.com/samuelattwood/partner-charts-ci/pkg/validate"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/repo"
)
const (
annotationAutoInstall = "catalog.cattle.io/auto-install"
annotationCertified = "catalog.cattle.io/certified"
annotationDisplayName = "catalog.cattle.io/display-name"
annotationExperimental = "catalog.cattle.io/experimental"
annotationFeatured = "catalog.cattle.io/featured"
annotationHidden = "catalog.cattle.io/hidden"
annotationKubeVersion = "catalog.cattle.io/kube-version"
annotationNamespace = "catalog.cattle.io/namespace"
annotationReleaseName = "catalog.cattle.io/release-name"
//indexFile sets the filename for the repo index yaml
indexFile = "index.yaml"
//packageEnvVariable sets the environment variable to check for a package name
packageEnvVariable = "PACKAGE"
//repositoryAssetsDir sets the directory name for chart asset files
repositoryAssetsDir = "assets"
//repositoryChartsDir sets the directory name for stored charts
repositoryChartsDir = "charts"
//repositoryPackagesDir sets the directory name for package configurations
repositoryPackagesDir = "packages"
configOptionsFile = "configuration.yaml"
featuredMax = 5
)
var (
version = "v0.0.0"
commit = "HEAD"
)
// PackageWrapper is a representation of relevant package metadata
type PackageWrapper struct {
//Additional Chart annotations
Annotations map[string]string
//Chart Display Name
DisplayName string
//Filtered subset of versions to-be-fetched
FetchVersions repo.ChartVersions
//Indicator to generate patch files
GenPatch bool
//Path stores the package path in current repository
Path string
//LatestStored stores the latest version of the chart currently in the repo
LatestStored repo.ChartVersion
//ManualUpdate evaluates true if package does not provide upstream yaml for automated update
ManualUpdate bool
//Chart name
Name string
//Untracked upstream versions newer than latest tracked
NewerUntracked []*semver.Version
//Force only pulling the latest version
OnlyLatest bool
//Indicator to write chart to disk
Save bool
//SourceMetadata represents metadata fetched from the upstream repository
SourceMetadata *fetcher.ChartSourceMetadata
//UpstreamYaml represents the values set in the package's upstream.yaml file
UpstreamYaml *parse.UpstreamYaml
//Chart vendor
Vendor string
//Formatted version of chart vendor
ParsedVendor string
}
type PackageList []PackageWrapper
func (p PackageList) Len() int {
return len(p)
}
func (p PackageList) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p PackageList) Less(i, j int) bool {
if p[i].SourceMetadata != nil && p[j].SourceMetadata != nil {
if p[i].ParsedVendor != p[j].ParsedVendor {
return p[i].ParsedVendor < p[j].ParsedVendor
}
return p[i].Name < p[j].Name
}
return false
}
// Generates patch files from prepared chart
func (packageWrapper PackageWrapper) patch() error {
var err error
var packageYaml *parse.PackageYaml
if !packageWrapper.ManualUpdate {
packageYaml, err = writePackageYaml(
packageWrapper.Path,
packageWrapper.UpstreamYaml.PackageVersion,
packageWrapper.SourceMetadata.Commit,
packageWrapper.SourceMetadata.SubDirectory,
packageWrapper.FetchVersions[0].URLs[0],
true,
)
if err != nil {
return err
}
}
pkg, err := generatePackage(packageWrapper.Path)
if err != nil {
return err
}
err = pkg.GeneratePatch()
if err != nil {
logrus.Error(err)
err = fmt.Errorf("unable to generate patch files")
return err
}
if !packageWrapper.ManualUpdate {
packageYaml.Remove()
}
return nil
}
func (packageWrapper *PackageWrapper) populateManual() (bool, error) {
//Preparing the chart to pull the actual release-name and version
err := prepareManualPackage(packageWrapper.Path)
if err != nil {
return false, err
}
pkg, err := generatePackage(packageWrapper.Path)
if err != nil {
return false, err
}
chartPath := path.Join(packageWrapper.Path, repositoryChartsDir)
helmChart, err := loader.Load(chartPath)
if err != nil {
return false, err
}
sourceMetadata := fetcher.ChartSourceMetadata{
Source: "direct",
Versions: make(repo.ChartVersions, 1),
}
packageWrapper.Name = helmChart.Name()
packageWrapper.Vendor, packageWrapper.ParsedVendor = parseVendor("", packageWrapper.Name, packageWrapper.Path)
chartVersion := repo.ChartVersion{
Metadata: &chart.Metadata{
Name: packageWrapper.Name,
},
URLs: make([]string, 1),
}
fixedVersion := ""
if pkg.Version != nil {
fixedVersion = pkg.Version.String()
}
chartVersion.URLs[0] = pkg.Upstream.GetOptions().URL
chartVersion.Version, err = conform.GeneratePackageVersion(
helmChart.Metadata.Version,
pkg.PackageVersion,
fixedVersion)
if err != nil {
return false, err
}
packageWrapper.SourceMetadata = &sourceMetadata
packageWrapper.SourceMetadata.Versions[0] = &chartVersion
packageWrapper.FetchVersions, err = filterVersions(
packageWrapper.SourceMetadata.Versions,
"",
nil)
if err != nil {
return false, err
}
packageWrapper.LatestStored, err = getLatestStoredVersion(helmChart.Name())
if err != nil {
return false, err
}
err = cleanPackage(packageWrapper.Path, true)
if err != nil {
return false, err
}
if len(packageWrapper.FetchVersions) == 0 {
return false, nil
}
return true, nil
}
// Populates package wrapper with relevant data from upstream, checks for updates,
// writes out package yaml file, and generates package object
// Returns true if newer package version is available
func (packageWrapper *PackageWrapper) populate() (bool, error) {
var err error
if packageWrapper.ManualUpdate {
return packageWrapper.populateManual()
} else {
packageWrapper.UpstreamYaml, err = parseUpstream(packageWrapper.Path)
if err != nil {
return false, err
}
sourceMetadata, err := generateChartSourceMetadata(*packageWrapper.UpstreamYaml)
if err != nil {
return false, err
}
packageWrapper.SourceMetadata = sourceMetadata
packageWrapper.Name = sourceMetadata.Versions[0].Name
packageWrapper.Vendor, packageWrapper.ParsedVendor = parseVendor(packageWrapper.UpstreamYaml.Vendor, packageWrapper.Name, packageWrapper.Path)
if packageWrapper.OnlyLatest {
packageWrapper.UpstreamYaml.Fetch = "latest"
if packageWrapper.UpstreamYaml.TrackVersions != nil {
packageWrapper.UpstreamYaml.TrackVersions = []string{packageWrapper.UpstreamYaml.TrackVersions[0]}
}
}
packageWrapper.FetchVersions, err = filterVersions(
packageWrapper.SourceMetadata.Versions,
packageWrapper.UpstreamYaml.Fetch,
packageWrapper.UpstreamYaml.TrackVersions)
if err != nil {
return false, err
}
packageWrapper.LatestStored, err = getLatestStoredVersion(packageWrapper.Name)
if err != nil {
return false, err
}
if packageWrapper.UpstreamYaml.DisplayName != "" {
packageWrapper.DisplayName = packageWrapper.UpstreamYaml.DisplayName
} else {
packageWrapper.DisplayName = packageWrapper.Name
}
}
if len(packageWrapper.FetchVersions) == 0 {
return false, nil
}
return true, nil
}
func (packageWrapper PackageWrapper) annotate(annotation, value string, remove, onlyLatest bool) error {
var versionsToUpdate repo.ChartVersions
chartName := packageWrapper.LatestStored.Name
allStoredVersions, err := getStoredVersions(chartName)
if err != nil {
return err
}
if onlyLatest {
versionsToUpdate = repo.ChartVersions{allStoredVersions[0]}
} else {
versionsToUpdate = allStoredVersions
}
for _, version := range versionsToUpdate {
modified := false
assetsPath := filepath.Join(
getRepoRoot(),
repositoryAssetsDir,
packageWrapper.ParsedVendor,
)
versionPath := path.Join(
getRepoRoot(),
repositoryChartsDir,
packageWrapper.ParsedVendor,
chartName,
)
helmChart, err := loader.LoadFile(version.URLs[0])
if err != nil {
return err
}
if remove {
modified = conform.RemoveChartAnnotations(helmChart, map[string]string{annotation: value})
} else {
modified = conform.ApplyChartAnnotations(helmChart, map[string]string{annotation: value}, true)
}
if modified {
logrus.Debugf("Modified annotations of %s (%s)\n", packageWrapper.Name, helmChart.Metadata.Version)
err = os.RemoveAll(versionPath)
if err != nil {
return err
}
err = conform.ExportChartAsset(helmChart, assetsPath)
if err != nil {
return err
}
conform.ExportChartDirectory(helmChart, versionPath)
if err != nil {
return err
}
err = removeVersionFromIndex(chartName, *version)
if err != nil {
return err
}
}
}
err = writeIndex()
return err
}
// Fetches absolute repository root path
func getRepoRoot() string {
repoRoot, err := os.Getwd()
if err != nil {
logrus.Fatal(err)
}
return repoRoot
}
func getRelativePath(packagePath string) string {
packagePath = filepath.ToSlash(packagePath)
packagesPath := filepath.Join(getRepoRoot(), repositoryPackagesDir)
return strings.TrimPrefix(packagePath, packagesPath)
}
func gitCleanup() error {
r, err := git.PlainOpen(getRepoRoot())
if err != nil {
return err
}
wt, err := r.Worktree()
if err != nil {
return err
}
cleanOptions := git.CleanOptions{
Dir: true,
}
branch, err := r.Head()
if err != nil {
return err
}
logrus.Debugf("Branch: %s\n", branch.Name())
checkoutOptions := git.CheckoutOptions{
Branch: branch.Name(),
Force: true,
}
err = wt.Clean(&cleanOptions)
if err != nil {
return err
}
err = wt.Checkout(&checkoutOptions)
return err
}
// Commits changes to index file, assets, charts, and packages
func commitChanges(updatedList PackageList) error {
var additions, updates string
commitOptions := git.CommitOptions{}
r, err := git.PlainOpen(getRepoRoot())
if err != nil {
return err
}
wt, err := r.Worktree()
if err != nil {
return err
}
logrus.Info("Committing changes")
for _, packageWrapper := range updatedList {
assetsPath := path.Join(
repositoryAssetsDir,
packageWrapper.ParsedVendor)
chartsPath := path.Join(
repositoryChartsDir,
packageWrapper.ParsedVendor,
packageWrapper.Name)
packagesPath := path.Join(
repositoryPackagesDir,
packageWrapper.ParsedVendor,
packageWrapper.Name)
wt.Add(assetsPath)
wt.Add(chartsPath)
wt.Add(packagesPath)
gitStatus, err := wt.Status()
if err != nil {
return err
}
for f, s := range gitStatus {
if s.Worktree == git.Deleted {
_, err = wt.Remove(f)
if err != nil {
return err
}
}
}
}
wt.Add(indexFile)
commitMessage := "Charts CI\n```"
sort.Sort(updatedList)
for _, packageWrapper := range updatedList {
lineItem := fmt.Sprintf(" %s/%s:\n",
packageWrapper.ParsedVendor,
packageWrapper.Name)
for _, version := range packageWrapper.FetchVersions {
lineItem += fmt.Sprintf(" - %s\n", version.Version)
}
if packageWrapper.LatestStored.Digest == "" {
additions += lineItem
} else {
updates += lineItem
}
}
if additions != "" {
commitMessage += fmt.Sprintf("\nAdded:\n%s", additions)
}
if updates != "" {
commitMessage += fmt.Sprintf("\nUpdated:\n%s", updates)
}
commitMessage += "```"
_, err = wt.Commit(commitMessage, &commitOptions)
if err != nil {
return err
}
gitStatus, err := wt.Status()
if err != nil {
return err
}
if !gitStatus.IsClean() {
logrus.Fatal("Git status is not clean")
}
return nil
}
// Cleans up ephemeral chart directory files from package prepare
func cleanPackage(packagePath string, manualUpdate bool) error {
packageName := strings.TrimPrefix(getRelativePath(packagePath), "/")
logrus.Infof("Cleaning package %s\n", packageName)
if manualUpdate {
logrus.Debugf("Generating package %s\n", packageName)
pkg, err := generatePackage(packagePath)
if err != nil {
return err
}
err = pkg.Clean()
if err != nil {
return err
}
} else {
os.RemoveAll(path.Join(packagePath, repositoryChartsDir))
}
return nil
}
// Prepares package for modification via patch
func prepareManualPackage(packagePath string) error {
logrus.Debugf("Generated package from %s", packagePath)
pkg, err := generatePackage(packagePath)
if err != nil {
logrus.Error(err)
}
conform.LinkOverlayFiles(packagePath)
err = pkg.Prepare()
if err != nil {
err = cleanPackage(packagePath, true)
if err != nil {
logrus.Error(err)
}
logrus.Error("Unable to prepare package. Cleaning up and skipping...")
return err
}
patchOrigPath := path.Join(packagePath, repositoryChartsDir, "Chart.yaml.orig")
if _, err := os.Stat(patchOrigPath); !os.IsNotExist(err) {
os.Remove(patchOrigPath)
}
return nil
}
// Prepares package for modification via patch and overlay
func preparePackage(packagePath string, sourceMetadata *fetcher.ChartSourceMetadata, chartVersion *repo.ChartVersion) error {
var chart *chart.Chart
var err error
logrus.Debugf("Preparing package from %s", packagePath)
if sourceMetadata.Source == "Git" {
chart, err = fetcher.LoadChartFromGit(chartVersion.URLs[0], sourceMetadata.SubDirectory, sourceMetadata.Commit)
} else {
chart, err = fetcher.LoadChartFromUrl(chartVersion.URLs[0])
}
if err != nil {
return err
}
exportPath := path.Join(packagePath, repositoryChartsDir)
err = conform.ExportChartDirectory(chart, exportPath)
if err != nil {
logrus.Error(err)
}
patchOrigPath := path.Join(packagePath, repositoryChartsDir, "Chart.yaml.orig")
if _, err := os.Stat(patchOrigPath); !os.IsNotExist(err) {
os.Remove(patchOrigPath)
}
return nil
}
// Creates Package object to operate upon based on package path
func generatePackage(packagePath string) (*charts.Package, error) {
logrus.Debugf("Generating package from %s\n", packagePath)
packageRelativePath := getRelativePath(packagePath)
rootFs := filesystem.GetFilesystem(getRepoRoot())
pkg, err := charts.GetPackage(rootFs, packageRelativePath)
if err != nil {
return nil, err
}
return pkg, nil
}
func writePackageYaml(packagePath string, packageVersion int, commit string, subdirectory string, url string, overWrite bool) (*parse.PackageYaml, error) {
logrus.Debugf("Generating package yaml in %s\n", packagePath)
packageYaml := parse.PackageYaml{
Commit: commit,
PackageVersion: packageVersion,
Path: packagePath,
SubDirectory: subdirectory,
Url: url,
}
logrus.Debugf("Writing package yaml in %s\n", packagePath)
err := packageYaml.Write(overWrite)
if err != nil {
logrus.Error(err)
}
return &packageYaml, nil
}
func collectTrackedVersions(upstreamVersions repo.ChartVersions, tracked []string) map[string]repo.ChartVersions {
trackedVersions := make(map[string]repo.ChartVersions)
for _, trackedVersion := range tracked {
versionList := make(repo.ChartVersions, 0)
for _, version := range upstreamVersions {
semVer, err := semver.NewVersion(version.Version)
if err != nil {
logrus.Errorf("%s: %s", version.Version, err)
continue
}
trackedSemVer, err := semver.NewVersion(trackedVersion)
if err != nil {
logrus.Errorf("%s: %s", version.Version, err)
continue
}
logrus.Debugf("Comparing upstream version %s (%s) to tracked version %s\n", version.Name, version.Version, trackedVersion)
if semVer.Major() == trackedSemVer.Major() && semVer.Minor() == trackedSemVer.Minor() {
logrus.Debugf("Appending version %s tracking %s\n", version.Version, trackedVersion)
versionList = append(versionList, version)
} else if semVer.Major() < trackedSemVer.Major() || (semVer.Major() == trackedSemVer.Major() && semVer.Minor() < trackedSemVer.Minor()) {
break
}
}
trackedVersions[trackedVersion] = versionList
}
return trackedVersions
}
func collectNonStoredVersions(versions repo.ChartVersions, storedVersions repo.ChartVersions, fetch string) repo.ChartVersions {
nonStoredVersions := make(repo.ChartVersions, 0)
for i, version := range versions {
parsedVersion, err := semver.NewVersion(version.Version)
if err != nil {
logrus.Error(err)
}
stored := false
logrus.Debugf("Checking if version %s is stored\n", version.Version)
for _, storedVersion := range storedVersions {
strippedStoredVersion := conform.StripPackageVersion(storedVersion.Version)
if storedVersion.Version == parsedVersion.String() {
logrus.Debugf("Found version %s\n", storedVersion.Version)
stored = true
break
} else if strippedStoredVersion == parsedVersion.String() {
logrus.Debugf("Found modified version %s\n", storedVersion.Version)
stored = true
break
}
}
if stored && i == 0 && (strings.ToLower(fetch) == "" || strings.ToLower(fetch) == "latest") {
logrus.Debugf("Latest version already stored")
break
}
if !stored {
if fetch == strings.ToLower("newer") {
var semVer *semver.Version
semVer, err := semver.NewVersion(version.Version)
if err != nil {
logrus.Error(err)
continue
}
if len(storedVersions) > 0 {
strippedStoredLatest := conform.StripPackageVersion(storedVersions[0].Version)
storedLatestSemVer, err := semver.NewVersion(strippedStoredLatest)
if err != nil {
logrus.Error(err)
continue
}
if semVer.GreaterThan(storedLatestSemVer) {
logrus.Debugf("Version: %s > %s\n", semVer.String(), storedVersions[0].Version)
nonStoredVersions = append(nonStoredVersions, version)
}
} else {
nonStoredVersions = append(nonStoredVersions, version)
}
} else if fetch == strings.ToLower("all") {
nonStoredVersions = append(nonStoredVersions, version)
} else {
nonStoredVersions = append(nonStoredVersions, version)
break
}
}
}
return nonStoredVersions
}
func stripPreRelease(versions repo.ChartVersions) repo.ChartVersions {
strippedVersions := make(repo.ChartVersions, 0)
for _, version := range versions {
semVer, err := semver.NewVersion(version.Version)
if err != nil {
logrus.Error(err)
continue
}
if semVer.Prerelease() == "" {
strippedVersions = append(strippedVersions, version)
}
}
return strippedVersions
}
func checkNewerUntracked(tracked []string, upstreamVersions repo.ChartVersions) []string {
newerUntracked := make([]string, 0)
latestTracked := getLatestTracked(tracked)
logrus.Debugf("Tracked Versions: %s\n", tracked)
logrus.Debugf("Checking for versions newer than latest tracked %s\n", latestTracked)
if len(tracked) == 0 {
return newerUntracked
}
for _, upstreamVersion := range upstreamVersions {
semVer, err := semver.NewVersion(upstreamVersion.Version)
if err != nil {
logrus.Error(err)
}
if semVer.Major() > latestTracked.Major() || (semVer.Major() == latestTracked.Major() && semVer.Minor() > latestTracked.Minor()) {
logrus.Debugf("Found version %s newer than latest tracked %s", semVer.String(), latestTracked.String())
newerUntracked = append(newerUntracked, semVer.String())
} else if semVer.Major() == latestTracked.Major() && semVer.Minor() == latestTracked.Minor() {
break
}
}
return newerUntracked
}
func filterVersions(upstreamVersions repo.ChartVersions, fetch string, tracked []string) (repo.ChartVersions, error) {
logrus.Debugf("Filtering versions for %s\n", upstreamVersions[0].Name)
upstreamVersions = stripPreRelease(upstreamVersions)
if len(tracked) > 0 {
if newerUntracked := checkNewerUntracked(tracked, upstreamVersions); len(newerUntracked) > 0 {
logrus.Warnf("Newer untracked version available: %s (%s)", upstreamVersions[0].Name, strings.Join(newerUntracked, ", "))
} else {
logrus.Debug("No newer untracked versions found")
}
}
if len(upstreamVersions) == 0 {
err := fmt.Errorf("No versions available in upstream or all versions are marked pre-release")
return repo.ChartVersions{}, err
}
filteredVersions := make(repo.ChartVersions, 0)
allStoredVersions, err := getStoredVersions(upstreamVersions[0].Name)
if len(tracked) > 0 {
allTrackedVersions := collectTrackedVersions(upstreamVersions, tracked)
storedTrackedVersions := collectTrackedVersions(allStoredVersions, tracked)
if err != nil {
return filteredVersions, err
}
for _, trackedVersion := range tracked {
nonStoredVersions := collectNonStoredVersions(allTrackedVersions[trackedVersion], storedTrackedVersions[trackedVersion], fetch)
filteredVersions = append(filteredVersions, nonStoredVersions...)
}
} else {
filteredVersions = collectNonStoredVersions(upstreamVersions, allStoredVersions, fetch)
}
return filteredVersions, nil
}
// Generates source metadata representation based on upstream repository
func generateChartSourceMetadata(upstreamYaml parse.UpstreamYaml) (*fetcher.ChartSourceMetadata, error) {
sourceMetadata, err := fetcher.FetchUpstream(upstreamYaml)
if err != nil {
return nil, err
}
return &sourceMetadata, nil
}
func parseVendor(upstreamYamlVendor, chartName, packagePath string) (string, string) {
var vendor, vendorPath string
packagePath = filepath.ToSlash(packagePath)
packageRelativePath := getRelativePath(packagePath)
if len(strings.Split(packageRelativePath, "/")) > 2 {
vendorPath = strings.TrimPrefix(filepath.Dir(packageRelativePath), "/")
} else {
vendorPath = strings.TrimPrefix(packageRelativePath, "/")
}
if upstreamYamlVendor != "" {
vendor = upstreamYamlVendor
} else if len(vendorPath) > 0 {
vendor = vendorPath
} else {
vendor = chartName
}
parsedVendor := strings.ReplaceAll(strings.ToLower(vendor), " ", "-")
return vendor, parsedVendor
}
// Prepares and standardizes chart, then returns loaded chart object
func initializeChart(packagePath string, sourceMetadata fetcher.ChartSourceMetadata, chartVersion repo.ChartVersion, manualUpdate bool) (*chart.Chart, error) {
var err error
if manualUpdate {
err = prepareManualPackage(packagePath)
} else {
err = preparePackage(packagePath, &sourceMetadata, &chartVersion)
}
if err != nil {
return nil, err
}
chartDirectoryPath := path.Join(packagePath, repositoryChartsDir)
conform.StandardizeChartDirectory(chartDirectoryPath, "")
err = conform.ApplyOverlayFiles(packagePath)
if err != nil {
return nil, err
}
helmChart, err := loader.Load(chartDirectoryPath)
if err != nil {
return nil, err
}
helmChart.Metadata.Version = chartVersion.Version
return helmChart, nil
}
// Mutates chart with necessary alterations for repository
func conformPackage(packageWrapper PackageWrapper) error {
var err error
logrus.Debugf("Conforming package from %s\n", packageWrapper.Path)
for _, chartVersion := range packageWrapper.FetchVersions {
logrus.Debugf("Conforming package %s (%s)\n", chartVersion.Name, chartVersion.Version)
helmChart, err := initializeChart(
packageWrapper.Path,
*packageWrapper.SourceMetadata,
*chartVersion,
packageWrapper.ManualUpdate,
)
if err != nil {
return err
}
if autoInstall := packageWrapper.UpstreamYaml.AutoInstall; autoInstall != "" {
packageWrapper.Annotations[annotationAutoInstall] = autoInstall
}
if packageWrapper.UpstreamYaml.Experimental {
packageWrapper.Annotations[annotationExperimental] = "true"
}
if packageWrapper.UpstreamYaml.Hidden {
packageWrapper.Annotations[annotationHidden] = "true"
}
if !packageWrapper.UpstreamYaml.RemoteDependencies {
for _, d := range helmChart.Metadata.Dependencies {
d.Repository = fmt.Sprintf("file://./charts/%s", d.Name)
}
}
if packageWrapper.ManualUpdate {
packageWrapper.Name = helmChart.Name()
chartVersion.Version = helmChart.Metadata.Version
pkg, err := generatePackage(packageWrapper.Path)
if err != nil {
return err
}
if packageWrapper.GenPatch {
err = pkg.GeneratePatch()
if err != nil {
return err
}
}
} else {
packageWrapper.Annotations[annotationCertified] = "partner"
packageWrapper.Annotations[annotationDisplayName] = packageWrapper.DisplayName
if packageWrapper.UpstreamYaml.ReleaseName != "" {
packageWrapper.Annotations[annotationReleaseName] = packageWrapper.UpstreamYaml.ReleaseName
} else {
packageWrapper.Annotations[annotationReleaseName] = packageWrapper.Name
}
conform.OverlayChartMetadata(helmChart, packageWrapper.UpstreamYaml.ChartYaml)
if val, ok := getByAnnotation(annotationFeatured, "")[packageWrapper.Name]; ok {
logrus.Debugf("Migrating featured annotation to latest version %s\n", packageWrapper.Name)
featuredIndex := val[0].Annotations[annotationFeatured]
packageWrapper.annotate(annotationFeatured, "", true, false)
packageWrapper.Annotations[annotationFeatured] = featuredIndex
}
if packageWrapper.UpstreamYaml.Namespace != "" {
packageWrapper.Annotations[annotationNamespace] = packageWrapper.UpstreamYaml.Namespace
}
if helmChart.Metadata.KubeVersion != "" && packageWrapper.UpstreamYaml.ChartYaml.KubeVersion != "" {
packageWrapper.Annotations[annotationKubeVersion] = packageWrapper.UpstreamYaml.ChartYaml.KubeVersion
helmChart.Metadata.KubeVersion = packageWrapper.UpstreamYaml.ChartYaml.KubeVersion
} else if helmChart.Metadata.KubeVersion != "" {
packageWrapper.Annotations[annotationKubeVersion] = helmChart.Metadata.KubeVersion
} else if packageWrapper.UpstreamYaml.ChartYaml.KubeVersion != "" {
packageWrapper.Annotations[annotationKubeVersion] = packageWrapper.UpstreamYaml.ChartYaml.KubeVersion
}
if packageVersion := packageWrapper.UpstreamYaml.PackageVersion; packageVersion != 0 {
helmChart.Metadata.Version, err = conform.GeneratePackageVersion(helmChart.Metadata.Version, &packageVersion, "")
if err != nil {
logrus.Error(err)
}
}
conform.ApplyChartAnnotations(helmChart, packageWrapper.Annotations, false)
}
if packageWrapper.Save {
err = cleanPackage(packageWrapper.Path, packageWrapper.ManualUpdate)
if err != nil {
logrus.Debug(err)
}
assetsPath := filepath.Join(
getRepoRoot(),
repositoryAssetsDir,
packageWrapper.ParsedVendor)
chartsPath := filepath.Join(
getRepoRoot(),
repositoryChartsDir,
packageWrapper.ParsedVendor,
helmChart.Metadata.Name)
if _, err := os.Stat(chartsPath); !os.IsNotExist(err) {
os.RemoveAll(chartsPath)
}
err = saveChart(helmChart, assetsPath, chartsPath)
if err != nil {
return err
}
}
}
return err
}
// Saves chart to disk as asset gzip and directory
func saveChart(helmChart *chart.Chart, assetsPath, chartsPath string) error {
logrus.Debugf("Exporting chart assets to %s\n", assetsPath)
err := conform.ExportChartAsset(helmChart, assetsPath)
if err != nil {
return err
}
assetFile := fmt.Sprintf("%s-%s.tgz", helmChart.Name(), helmChart.Metadata.Version)
assetFile = path.Join(assetsPath, assetFile)
err = conform.Gunzip(assetFile, chartsPath)
if err != nil {
logrus.Error(err)
}
logrus.Debugf("Exporting chart to %s\n", chartsPath)
err = conform.ExportChartDirectory(helmChart, chartsPath)
if err != nil {
return err
}
return nil
}
func getLatestTracked(tracked []string) *semver.Version {
var latestTracked *semver.Version
for _, version := range tracked {
semVer, err := semver.NewVersion(version)
if err != nil {
logrus.Error(err)
}
if latestTracked == nil || semVer.GreaterThan(latestTracked) {
latestTracked = semVer
}
}
return latestTracked
}
func getStoredVersions(chartName string) (repo.ChartVersions, error) {
helmIndexYaml, err := readIndex()
storedVersions := repo.ChartVersions{}