From dc52f8f33a50cf3f49a504c0322642034850d7cb Mon Sep 17 00:00:00 2001 From: cndoit18 Date: Fri, 28 May 2021 14:25:10 +0800 Subject: [PATCH] fix(repo option): fix a problem with helm template/install when specifying --repo Signed-off-by: cndoit18 --- cmd/helm/install_test.go | 24 ++++++++++++++++ cmd/helm/testdata/local-subchart-0.1.0.tgz | Bin 0 -> 259 bytes .../testdata/output/install-with-repo-url.txt | 6 ++++ pkg/action/install.go | 27 ++++++++++-------- pkg/downloader/chart_downloader.go | 7 +++++ pkg/repo/chartrepo.go | 4 +++ 6 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 cmd/helm/testdata/local-subchart-0.1.0.tgz create mode 100644 cmd/helm/testdata/output/install-with-repo-url.txt diff --git a/cmd/helm/install_test.go b/cmd/helm/install_test.go index 0fae79534..bba3d9fc9 100644 --- a/cmd/helm/install_test.go +++ b/cmd/helm/install_test.go @@ -19,9 +19,27 @@ package main import ( "fmt" "testing" + + "helm.sh/helm/v3/pkg/repo/repotest" ) func TestInstall(t *testing.T) { + + srv, err := repotest.NewTempServerWithCleanup(t, "testdata/*.tgz*") + srv.Stop() + if err != nil { + t.Fatal(err) + } + srv.Start() + defer srv.Stop() + if err := srv.CreateIndex(); err != nil { + t.Fatal(err) + } + + if err := srv.LinkIndices(); err != nil { + t.Fatal(err) + } + tests := []cmdTestCase{ // Install, base case { @@ -207,6 +225,12 @@ func TestInstall(t *testing.T) { name: "install chart with only crds", cmd: "install crd-test testdata/testcharts/chart-with-only-crds --namespace default", }, + // Install chart with repoURL + { + name: "install chart with only repoURL", + cmd: "install local-subchart local-subchart --repo " + srv.URL() + " --version 0.1.0", + golden: "output/install-with-repo-url.txt", + }, } runTestActionCmd(t, tests) diff --git a/cmd/helm/testdata/local-subchart-0.1.0.tgz b/cmd/helm/testdata/local-subchart-0.1.0.tgz new file mode 100644 index 0000000000000000000000000000000000000000..4853121056a718bf02392623cf46a826f7908752 GIT binary patch literal 259 zcmV+e0sQ_SiwG0|00000|0w_~VMtOiV@ORlOnEsqVl!4SWK%V1T2nbTPgYhoO;>Dc zVQyr3R8em|NM&qo0PNJuio!4y2H>vq6nTN^{F#I 0, } if c.Verify { dl.Verify = downloader.VerifyAlways diff --git a/pkg/downloader/chart_downloader.go b/pkg/downloader/chart_downloader.go index 6c600bebb..ae4910c54 100644 --- a/pkg/downloader/chart_downloader.go +++ b/pkg/downloader/chart_downloader.go @@ -72,6 +72,7 @@ type ChartDownloader struct { RegistryClient *registry.Client RepositoryConfig string RepositoryCache string + UseRepoURL bool } // DownloadTo retrieves a chart. Depending on the settings, it may also download a provenance file. @@ -158,6 +159,12 @@ func (c *ChartDownloader) ResolveChartVersion(ref, version string) (*url.URL, er if err != nil { return nil, errors.Errorf("invalid chart URL format: %s", ref) } + + // It is already resolve, no need to look for the version in the repoconfig + if c.UseRepoURL { + return u, nil + } + c.Options = append(c.Options, getter.WithURL(ref)) rf, err := loadRepoConfig(c.RepositoryConfig) diff --git a/pkg/repo/chartrepo.go b/pkg/repo/chartrepo.go index 09b94fd42..942486734 100644 --- a/pkg/repo/chartrepo.go +++ b/pkg/repo/chartrepo.go @@ -242,6 +242,10 @@ func FindChartInAuthAndTLSRepoURL(repoURL, username, password, chartName, chartV return "", errors.Wrapf(err, "looks like %q is not a valid chart repository or cannot be reached", repoURL) } + // Temporary generation needs to delete + defer os.Remove(filepath.Join(r.CachePath, helmpath.CacheIndexFile(r.Config.Name))) + defer os.Remove(filepath.Join(r.CachePath, helmpath.CacheChartsFile(r.Config.Name))) + // Read the index file for the repository to get chart information and return chart URL repoIndex, err := LoadIndexFile(idx) if err != nil {