Skip to content

Commit

Permalink
downloading icons when running make charts
Browse files Browse the repository at this point in the history
  • Loading branch information
diogoasouza committed Jan 12, 2024
1 parent df3f147 commit de302cd
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pkg/helm/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package helm

import (
"fmt"
"github.com/rancher/charts-build-scripts/pkg/icons"
"helm.sh/helm/v3/pkg/chartutil"
"math"
"net/url"
"os"
"path/filepath"

Expand Down Expand Up @@ -33,6 +36,24 @@ func ExportHelmChart(rootFs, fs billy.Filesystem, helmChartPath string, packageV
if err != nil {
return fmt.Errorf("could not load Helm chart: %s", err)
}
//checking if icon is pointing to a valid http/https url
u, err := url.Parse(chart.Metadata.Icon)
if err == nil && (u.Scheme == "http" || u.Scheme == "https") {
logrus.Infof("Chart icon is pointing to a remote url. Downloading it...")
// download icon and change the icon property to point to it
p, err := icons.Download(rootFs, chart.Metadata)
if err == nil { // managed to download the icon and save it locally
chart.Metadata.Icon = fmt.Sprintf("file://%s", p)
} else {
logrus.Errorf("failed to download icon for chart %s, err: %s", chart.Name(), err)
}
}

chartYamlPath := fmt.Sprintf("%s/Chart.yaml", absHelmChartPath)
err = chartutil.SaveChartfile(chartYamlPath, chart.Metadata)
if err != nil {
return err
}
if err := chart.Validate(); err != nil {
return fmt.Errorf("failed while trying to validate Helm chart: %s", err)
}
Expand Down
44 changes: 44 additions & 0 deletions pkg/icons/icons.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package icons

import (
"fmt"
"github.com/rancher/charts-build-scripts/pkg/path"
"io"
"mime"
"net/http"

"github.com/go-git/go-billy/v5"
"github.com/sirupsen/logrus"
"helm.sh/helm/v3/pkg/chart"
)

// Download receives a chart metadata and the filesystem pointing to the root of the project.
// From the metadata, gets the icon and name of the chart.
// It downloads the icon, infers the type using the content-type header from the response
// and saves the file locally to path.RepositoryLogosDir using the name of the chart as the file name.
func Download(rootFs billy.Filesystem, metadata *chart.Metadata) (string, error) {
icon, err := http.Get(metadata.Icon)
if err != nil {
logrus.Errorf(err.Error())
return "", fmt.Errorf("err: %w", err)
}

byType, err := mime.ExtensionsByType(icon.Header.Get("Content-Type"))
if err != nil || len(byType) == 0 || icon.StatusCode != http.StatusOK {
return "", fmt.Errorf("invalid icon")
}
path := fmt.Sprintf("%s/%s%s", path.RepositoryLogosDir, metadata.Name, byType[0])
create, err := rootFs.Create(path)
if err != nil {
logrus.Errorf(err.Error())
return "", fmt.Errorf("err: %w", err)
}
defer create.Close()
_, err = io.Copy(create, icon.Body)
defer icon.Body.Close()
if err != nil {
logrus.Errorf(err.Error())
return "", fmt.Errorf("err: %w", err)
}
return path, nil
}
3 changes: 3 additions & 0 deletions pkg/path/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,7 @@ const (

// DefaultCachePath represents the default place to put a cache on pulled values
DefaultCachePath = ".charts-build-scripts/.cache"

// RepositoryLogosDir is a directory on your Staging/Live branch that contains the files with the logos of each chart
RepositoryLogosDir = "assets/logos"
)

0 comments on commit de302cd

Please sign in to comment.