Skip to content

Commit b887354

Browse files
authored
Merge pull request #127 from diogoasouza/icons-script
adding make icon script instead of doing it inside make charts
2 parents 10fb598 + d6ecb5a commit b887354

File tree

5 files changed

+69
-39
lines changed

5 files changed

+69
-39
lines changed

main.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,12 @@ func main() {
230230
Usage: "Checks if there are any images with RC tags or charts with RC versions in the charts repository",
231231
Action: checkRCTagsAndVersions,
232232
},
233+
{
234+
Name: "icon",
235+
Usage: "Download the chart icon locally and use it",
236+
Action: downloadIcon,
237+
Flags: []cli.Flag{packageFlag, configFlag, cacheFlag},
238+
},
233239
}
234240

235241
if err := app.Run(os.Args); err != nil {
@@ -290,13 +296,23 @@ func generateCharts(c *cli.Context) {
290296
logrus.Infof("No packages found.")
291297
return
292298
}
293-
validateOnly := false
294-
if c.Command.Name == "validate" {
295-
validateOnly = true
296-
}
297299
chartsScriptOptions := parseScriptOptions()
298300
for _, p := range packages {
299-
if err := p.GenerateCharts(chartsScriptOptions.OmitBuildMetadataOnExport, validateOnly); err != nil {
301+
if err := p.GenerateCharts(chartsScriptOptions.OmitBuildMetadataOnExport); err != nil {
302+
logrus.Fatal(err)
303+
}
304+
}
305+
}
306+
307+
func downloadIcon(c *cli.Context) {
308+
packages := getPackages()
309+
if len(packages) == 0 {
310+
logrus.Infof("No packages found.")
311+
return
312+
}
313+
for _, p := range packages {
314+
err := p.DownloadIcon()
315+
if err != nil {
300316
logrus.Fatal(err)
301317
}
302318
}

pkg/charts/additionalchart.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,11 @@ func (c *AdditionalChart) GeneratePatch(rootFs, pkgFs billy.Filesystem) error {
214214
}
215215

216216
// GenerateChart generates the chart and stores it in the assets and charts directory
217-
func (c *AdditionalChart) GenerateChart(rootFs, pkgFs billy.Filesystem, packageVersion *int, version *semver.Version, omitBuildMetadataOnExport, validateOnly bool) error {
217+
func (c *AdditionalChart) GenerateChart(rootFs, pkgFs billy.Filesystem, packageVersion *int, version *semver.Version, omitBuildMetadataOnExport bool) error {
218218
if c.upstreamChartVersion == nil {
219219
return fmt.Errorf("cannot generate chart since it has never been prepared: upstreamChartVersion is not set")
220220
}
221-
if err := helm.ExportHelmChart(rootFs, pkgFs, c.WorkingDir, packageVersion, version, *c.upstreamChartVersion, omitBuildMetadataOnExport, validateOnly); err != nil {
221+
if err := helm.ExportHelmChart(rootFs, pkgFs, c.WorkingDir, packageVersion, version, *c.upstreamChartVersion, omitBuildMetadataOnExport); err != nil {
222222
return fmt.Errorf("encountered error while trying to export Helm chart for %s: %s", c.WorkingDir, err)
223223
}
224224
return nil

pkg/charts/chart.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ func (c *Chart) GeneratePatch(rootFs, pkgFs billy.Filesystem) error {
103103
}
104104

105105
// GenerateChart generates the chart and stores it in the assets and charts directory
106-
func (c *Chart) GenerateChart(rootFs, pkgFs billy.Filesystem, packageVersion *int, version *semver.Version, omitBuildMetadataOnExport, validateOnly bool) error {
106+
func (c *Chart) GenerateChart(rootFs, pkgFs billy.Filesystem, packageVersion *int, version *semver.Version, omitBuildMetadataOnExport bool) error {
107107
if c.upstreamChartVersion == nil {
108108
return fmt.Errorf("cannot generate chart since it has never been prepared: upstreamChartVersion is not set")
109109
}
110-
if err := helm.ExportHelmChart(rootFs, pkgFs, c.WorkingDir, packageVersion, version, *c.upstreamChartVersion, omitBuildMetadataOnExport, validateOnly); err != nil {
110+
if err := helm.ExportHelmChart(rootFs, pkgFs, c.WorkingDir, packageVersion, version, *c.upstreamChartVersion, omitBuildMetadataOnExport); err != nil {
111111
return fmt.Errorf("encountered error while trying to export Helm chart for %s: %s", c.WorkingDir, err)
112112
}
113113
return nil

pkg/charts/package.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ package charts
22

33
import (
44
"fmt"
5+
"net/url"
56
"path/filepath"
67

78
"github.com/blang/semver"
89
"github.com/go-git/go-billy/v5"
910
"github.com/rancher/charts-build-scripts/pkg/filesystem"
1011
"github.com/rancher/charts-build-scripts/pkg/helm"
12+
"github.com/rancher/charts-build-scripts/pkg/icons"
13+
"github.com/rancher/charts-build-scripts/pkg/path"
1114
"github.com/sirupsen/logrus"
15+
helmLoader "helm.sh/helm/v3/pkg/chart/loader"
16+
"helm.sh/helm/v3/pkg/chartutil"
1217
)
1318

1419
// Package represents the configuration of a particular forked Helm chart
@@ -85,8 +90,43 @@ func (p *Package) GeneratePatch() error {
8590
return nil
8691
}
8792

93+
// DownloadIcon Downloads the icon from the charts.yaml file to the assets/logos folder
94+
// and changes the chart.yaml file to use it
95+
func (p *Package) DownloadIcon() error {
96+
exists, err := filesystem.PathExists(p.fs, path.RepositoryChartsDir)
97+
if err != nil {
98+
return fmt.Errorf("failed to check for charts dir. Err: %w", err)
99+
}
100+
if !exists {
101+
logrus.Infof("Charts dir does not exists. Please run `make prepare` first")
102+
return nil
103+
}
104+
absHelmChartPath := filesystem.GetAbsPath(p.fs, path.RepositoryChartsDir)
105+
chart, err := helmLoader.Load(absHelmChartPath)
106+
if err != nil {
107+
return fmt.Errorf("could not load Helm chart: %s", err)
108+
}
109+
u, err := url.Parse(chart.Metadata.Icon)
110+
if err == nil && (u.Scheme == "http" || u.Scheme == "https") {
111+
logrus.Infof("Chart icon is pointing to a remote url. Downloading it...")
112+
// download icon and change the icon property to point to it
113+
p, err := icons.Download(p.rootFs, chart.Metadata)
114+
if err == nil { // managed to download the icon and save it locally
115+
chart.Metadata.Icon = fmt.Sprintf("file://%s", p)
116+
} else {
117+
logrus.Errorf("failed to download icon for chart %s, err: %s", chart.Name(), err)
118+
}
119+
chartYamlPath := fmt.Sprintf("%s/Chart.yaml", absHelmChartPath)
120+
err = chartutil.SaveChartfile(chartYamlPath, chart.Metadata)
121+
if err != nil {
122+
return fmt.Errorf("failed to save chart.yaml file. err: %w", err)
123+
}
124+
}
125+
return nil
126+
}
127+
88128
// GenerateCharts creates Helm chart archives for each chart after preparing it
89-
func (p *Package) GenerateCharts(omitBuildMetadataOnExport bool, validateOnly bool) error {
129+
func (p *Package) GenerateCharts(omitBuildMetadataOnExport bool) error {
90130
if p.DoNotRelease {
91131
logrus.Infof("Skipping package marked doNotRelease")
92132
return nil
@@ -95,12 +135,12 @@ func (p *Package) GenerateCharts(omitBuildMetadataOnExport bool, validateOnly bo
95135
return fmt.Errorf("encountered error while trying to prepare package: %s", err)
96136
}
97137
// Add PackageVersion to format
98-
err := p.Chart.GenerateChart(p.rootFs, p.fs, p.PackageVersion, p.Version, omitBuildMetadataOnExport, validateOnly)
138+
err := p.Chart.GenerateChart(p.rootFs, p.fs, p.PackageVersion, p.Version, omitBuildMetadataOnExport)
99139
if err != nil {
100140
return fmt.Errorf("encountered error while exporting main chart: %s", err)
101141
}
102142
for _, additionalChart := range p.AdditionalCharts {
103-
err = additionalChart.GenerateChart(p.rootFs, p.fs, p.PackageVersion, p.Version, omitBuildMetadataOnExport, validateOnly)
143+
err = additionalChart.GenerateChart(p.rootFs, p.fs, p.PackageVersion, p.Version, omitBuildMetadataOnExport)
104144
if err != nil {
105145
return fmt.Errorf("encountered error while exporting %s: %s", additionalChart.WorkingDir, err)
106146
}

pkg/helm/export.go

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@ package helm
33
import (
44
"fmt"
55
"math"
6-
"net/url"
76
"os"
87
"path/filepath"
98

10-
"github.com/rancher/charts-build-scripts/pkg/icons"
11-
"helm.sh/helm/v3/pkg/chartutil"
12-
139
"github.com/blang/semver"
1410
"github.com/go-git/go-billy/v5"
1511
"github.com/rancher/charts-build-scripts/pkg/filesystem"
@@ -30,7 +26,7 @@ var (
3026

3127
// ExportHelmChart creates a Helm chart archive and an unarchived Helm chart at RepositoryAssetDirpath and RepositoryChartDirPath
3228
// helmChartPath is a relative path (rooted at the package level) that contains the chart.
33-
func ExportHelmChart(rootFs, fs billy.Filesystem, helmChartPath string, packageVersion *int, version *semver.Version, upstreamChartVersion string, omitBuildMetadata, validateOnly bool) error {
29+
func ExportHelmChart(rootFs, fs billy.Filesystem, helmChartPath string, packageVersion *int, version *semver.Version, upstreamChartVersion string, omitBuildMetadata bool) error {
3430
// Try to load the chart to see if it can be exported
3531
absHelmChartPath := filesystem.GetAbsPath(fs, helmChartPath)
3632
chart, err := helmLoader.Load(absHelmChartPath)
@@ -63,28 +59,6 @@ func ExportHelmChart(rootFs, fs billy.Filesystem, helmChartPath string, packageV
6359
}
6460
chartVersion := chartVersionSemver.String()
6561

66-
// If its not only validation then download icons
67-
if !validateOnly {
68-
//checking if icon is pointing to a valid http/https url
69-
u, err := url.Parse(chart.Metadata.Icon)
70-
if err == nil && (u.Scheme == "http" || u.Scheme == "https") {
71-
logrus.Infof("Chart icon is pointing to a remote url. Downloading it...")
72-
// download icon and change the icon property to point to it
73-
p, err := icons.Download(rootFs, chart.Metadata)
74-
if err == nil { // managed to download the icon and save it locally
75-
chart.Metadata.Icon = fmt.Sprintf("file://%s", p)
76-
} else {
77-
logrus.Errorf("failed to download icon for chart %s, err: %s", chart.Name(), err)
78-
}
79-
}
80-
}
81-
82-
chartYamlPath := fmt.Sprintf("%s/Chart.yaml", absHelmChartPath)
83-
err = chartutil.SaveChartfile(chartYamlPath, chart.Metadata)
84-
if err != nil {
85-
return err
86-
}
87-
8862
// Assets are indexed by chart name, independent of which package that chart is contained within
8963
chartAssetsDirpath := filepath.Join(path.RepositoryAssetsDir, chart.Metadata.Name)
9064
// All generated charts are indexed by chart name and version

0 commit comments

Comments
 (0)