Skip to content

Commit

Permalink
refactor: remove printing available Helm charts and versions when the…
Browse files Browse the repository at this point in the history
… Chart is not found (#2944)

Signed-off-by: Philip Laine <[email protected]>
  • Loading branch information
phillebaba authored Aug 30, 2024
1 parent f60b2f4 commit 10bf746
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 67 deletions.
76 changes: 10 additions & 66 deletions src/internal/packager/helm/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@ import (
"strings"

"github.com/defenseunicorns/pkg/helpers/v2"
"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/config/lang"
"github.com/zarf-dev/zarf/src/internal/git"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/transform"
"github.com/zarf-dev/zarf/src/pkg/utils"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/helmpath"
"helm.sh/helm/v3/pkg/registry"
"k8s.io/client-go/util/homedir"

"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/downloader"
"helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/registry"
"helm.sh/helm/v3/pkg/repo"
"k8s.io/client-go/util/homedir"

"github.com/zarf-dev/zarf/src/config"
"github.com/zarf-dev/zarf/src/config/lang"
"github.com/zarf-dev/zarf/src/internal/git"
"github.com/zarf-dev/zarf/src/pkg/message"
"github.com/zarf-dev/zarf/src/pkg/transform"
"github.com/zarf-dev/zarf/src/pkg/utils"
)

// PackageChart creates a chart archive from a path to a chart on the host os and builds chart dependencies
Expand Down Expand Up @@ -181,10 +180,6 @@ func (h *Helm) DownloadPublishedChart(ctx context.Context, cosignKeyPath string)

chartURL, err = repo.FindChartInAuthRepoURL(h.chart.URL, username, password, chartName, h.chart.Version, pull.CertFile, pull.KeyFile, pull.CaFile, getter.All(pull.Settings))
if err != nil {
if strings.Contains(err.Error(), "not found") {
// Intentionally dogsled this error since this is just a nice to have helper
_ = h.listAvailableChartsAndVersions(pull)
}
return fmt.Errorf("unable to pull the helm chart: %w", err)
}
}
Expand Down Expand Up @@ -337,54 +332,3 @@ func (h *Helm) loadAndValidateChart(location string) (loader.ChartLoader, *chart

return cl, chart, nil
}

func (h *Helm) listAvailableChartsAndVersions(pull *action.Pull) error {
c := repo.Entry{
URL: h.chart.URL,
CertFile: pull.CertFile,
KeyFile: pull.KeyFile,
CAFile: pull.CaFile,
Name: h.chart.Name,
}

r, err := repo.NewChartRepository(&c, getter.All(pull.Settings))
if err != nil {
return err
}
idx, err := r.DownloadIndexFile()
if err != nil {
return fmt.Errorf("looks like %q is not a valid chart repository or cannot be reached: %w", h.chart.URL, err)
}
defer func() {
os.RemoveAll(filepath.Join(r.CachePath, helmpath.CacheChartsFile(r.Config.Name)))
os.RemoveAll(filepath.Join(r.CachePath, helmpath.CacheIndexFile(r.Config.Name)))
}()

// Read the index file for the repository to get chart information and return chart URL
repoIndex, err := repo.LoadIndexFile(idx)
if err != nil {
return err
}

chartData := [][]string{}
for name, entries := range repoIndex.Entries {
versions := ""
for idx, entry := range entries {
separator := ""
if idx < len(entries)-1 {
separator = ", "
}
versions += entry.Version + separator
}

versions = helpers.Truncate(versions, 75, false)
chartData = append(chartData, []string{name, versions})
}

message.Notef("Available charts and versions from %q:", h.chart.URL)

// Print out the table for the user
header := []string{"Chart", "Versions"}
message.Table(header, chartData)
return nil
}
1 change: 0 additions & 1 deletion src/test/e2e/25_helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func testHelmChartsExample(t *testing.T) {
stdOut, stdErr, err = e2e.Zarf(t, "package", "create", evilChartLookupPath, "--tmpdir", tmpdir, "--confirm")
require.Error(t, err, stdOut, stdErr)
require.Contains(t, e2e.StripMessageFormatting(stdErr), "chart \"asdf\" version \"6.4.0\" not found")
require.Contains(t, e2e.StripMessageFormatting(stdErr), "Available charts and versions from \"https://stefanprodan.github.io/podinfo\":")

// Create a test package (with a registry override (host+subpath to host+subpath) to test that as well)
stdOut, stdErr, err = e2e.Zarf(t, "package", "create", "examples/helm-charts", "-o", "build", "--registry-override", "ghcr.io/stefanprodan=docker.io/stefanprodan", "--tmpdir", tmpdir, "--confirm")
Expand Down

0 comments on commit 10bf746

Please sign in to comment.