From c37dfe44a36114d4fc1a40994a8795009db0ed03 Mon Sep 17 00:00:00 2001 From: Mike Seese Date: Mon, 3 Apr 2023 13:31:41 -0700 Subject: [PATCH] do not error for remote chart if there's a missing local repo cache Signed-off-by: Mike Seese --- pkg/downloader/chart_downloader.go | 5 +-- pkg/downloader/chart_downloader_test.go | 32 +++++++++++++++++-- .../testdata/repository-empty/.keep | 0 3 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 pkg/downloader/testdata/repository-empty/.keep diff --git a/pkg/downloader/chart_downloader.go b/pkg/downloader/chart_downloader.go index a95894e00..98ee2c4cf 100644 --- a/pkg/downloader/chart_downloader.go +++ b/pkg/downloader/chart_downloader.go @@ -54,7 +54,7 @@ const ( ) // ErrNoOwnerRepo indicates that a given chart URL can't be found in any repos. -var ErrNoOwnerRepo = errors.New("could not find a repo containing the given URL") +var ErrNoOwnerRepo = errors.New("could not find a repo containing the given URL (try 'helm repo update')") // ChartDownloader handles downloading a chart. // @@ -380,7 +380,8 @@ func (c *ChartDownloader) scanReposForURL(u string, rf *repo.File) (*repo.Entry, idxFile := filepath.Join(c.RepositoryCache, helmpath.CacheIndexFile(r.Config.Name)) i, err := repo.LoadIndexFile(idxFile) if err != nil { - return nil, errors.Wrap(err, "no cached repo found. (try 'helm repo update')") + // No cache for this repository; let's keep looking rather than erroring + continue } for _, entry := range i.Entries { diff --git a/pkg/downloader/chart_downloader_test.go b/pkg/downloader/chart_downloader_test.go index 131e21306..cdce65be3 100644 --- a/pkg/downloader/chart_downloader_test.go +++ b/pkg/downloader/chart_downloader_test.go @@ -28,8 +28,9 @@ import ( ) const ( - repoConfig = "testdata/repositories.yaml" - repoCache = "testdata/repository" + repoConfig = "testdata/repositories.yaml" + repoCache = "testdata/repository" + repoCacheEmpty = "testdata/repository-empty" ) func TestResolveChartRef(t *testing.T) { @@ -344,3 +345,30 @@ func TestScanReposForURL(t *testing.T) { t.Fatalf("expected ErrNoOwnerRepo, got %v", err) } } + +// In an scenario where a full URL is provided, chart_downloader will swallow the +// ErrNoOwnerRepo error and return an HTTP client to download the chart. This specific +// test shows that the chart will get downloaded in that scenario if the cache is empty +// (hence `repoCacheEmpty`). +func TestScanReposForURLWithEmptyCache(t *testing.T) { + c := ChartDownloader{ + Out: os.Stderr, + Verify: VerifyLater, + RepositoryConfig: repoConfig, + RepositoryCache: repoCacheEmpty, + Getters: getter.All(&cli.EnvSettings{ + RepositoryConfig: repoConfig, + RepositoryCache: repoCacheEmpty, + }), + } + + u := "http://example.com/alpine-0.2.0.tgz" + rf, err := repo.LoadFile(repoConfig) + if err != nil { + t.Fatal(err) + } + + if _, err = c.scanReposForURL(u, rf); err != ErrNoOwnerRepo { + t.Fatalf("expected ErrNoOwnerRepo, got %v", err) + } +} diff --git a/pkg/downloader/testdata/repository-empty/.keep b/pkg/downloader/testdata/repository-empty/.keep new file mode 100644 index 000000000..e69de29bb