From 10bf7465c4b0fc0b0477bb55f5189cd2b85d0d01 Mon Sep 17 00:00:00 2001 From: Philip Laine Date: Fri, 30 Aug 2024 17:35:10 +0200 Subject: [PATCH] refactor: remove printing available Helm charts and versions when the Chart is not found (#2944) Signed-off-by: Philip Laine --- src/internal/packager/helm/repo.go | 76 ++++-------------------------- src/test/e2e/25_helm_test.go | 1 - 2 files changed, 10 insertions(+), 67 deletions(-) diff --git a/src/internal/packager/helm/repo.go b/src/internal/packager/helm/repo.go index 24f3a7f4b0..378b12c9cf 100644 --- a/src/internal/packager/helm/repo.go +++ b/src/internal/packager/helm/repo.go @@ -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 @@ -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) } } @@ -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 -} diff --git a/src/test/e2e/25_helm_test.go b/src/test/e2e/25_helm_test.go index b37c3ad39a..e1814d4b1e 100644 --- a/src/test/e2e/25_helm_test.go +++ b/src/test/e2e/25_helm_test.go @@ -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")