From 9cc6a9b25ca0056d51f0f0d041a35cf2d835f61d Mon Sep 17 00:00:00 2001 From: Jack Wilsdon Date: Wed, 9 Aug 2023 09:09:24 +0100 Subject: [PATCH] fix(helm): introduce --remote-repo flag Ignore local directories with the same name as the chart being installed if --remote-repo is passed. Fixes #11141 Signed-off-by: Jack Wilsdon --- cmd/helm/flags.go | 1 + cmd/helm/install_test.go | 11 +++++++++++ pkg/action/install.go | 34 +++++++++++++++++++++------------- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/cmd/helm/flags.go b/cmd/helm/flags.go index 3f89aae29..63cabace6 100644 --- a/cmd/helm/flags.go +++ b/cmd/helm/flags.go @@ -63,6 +63,7 @@ func addChartPathOptionsFlags(f *pflag.FlagSet, c *action.ChartPathOptions) { f.BoolVar(&c.InsecureSkipTLSverify, "insecure-skip-tls-verify", false, "skip tls certificate checks for the chart download") f.StringVar(&c.CaFile, "ca-file", "", "verify certificates of HTTPS-enabled servers using this CA bundle") f.BoolVar(&c.PassCredentialsAll, "pass-credentials", false, "pass credentials to all domains") + f.BoolVar(&c.RemoteRepo, "remote-repo", false, "force use of chart from remote repository instead of local filesystem") } // bindOutputFlag will add the output flag to the given command and bind the diff --git a/cmd/helm/install_test.go b/cmd/helm/install_test.go index b34d1455c..70b151f93 100644 --- a/cmd/helm/install_test.go +++ b/cmd/helm/install_test.go @@ -252,6 +252,17 @@ func TestInstall(t *testing.T) { cmd: fmt.Sprintf("install aeneas test/reqtest --username username --password password --repository-config %s --repository-cache %s", repoFile, srv.Root()), golden: "output/install.txt", }, + // Verify that a remote chart is used with --remote-repo + { + name: "basic install with --remote-repo", + cmd: "install aeneas reqtest --namespace default --repo " + srv.URL() + " --username username --password password --remote-repo", + golden: "output/install.txt", + }, + { + name: "existing local path with --remote-repo", + cmd: "install aeneas testdata/testcharts/empty --namespace default --repo " + srv.URL() + " --username username --password password --remote-repo", + wantError: true, + }, } runTestCmd(t, tests) diff --git a/pkg/action/install.go b/pkg/action/install.go index 1860b32f3..3750782d3 100644 --- a/pkg/action/install.go +++ b/pkg/action/install.go @@ -118,6 +118,7 @@ type ChartPathOptions struct { Keyring string // --keyring Password string // --password PassCredentialsAll bool // --pass-credentials + RemoteRepo bool // --remote-repo RepoURL string // --repo Username string // --username Verify bool // --verify @@ -727,24 +728,31 @@ func (c *ChartPathOptions) LocateChart(name string, settings *cli.EnvSettings) ( } name = strings.TrimSpace(name) - version := strings.TrimSpace(c.Version) - if _, err := os.Stat(name); err == nil { - abs, err := filepath.Abs(name) - if err != nil { - return abs, err - } - if c.Verify { - if _, err := downloader.VerifyChart(abs, c.Keyring); err != nil { - return "", err + if !c.RemoteRepo { + if _, err := os.Stat(name); err == nil { + abs, err := filepath.Abs(name) + if err != nil { + return abs, err + } + if c.Verify { + if _, err := downloader.VerifyChart(abs, c.Keyring); err != nil { + return "", err + } } + return abs, nil + } + if filepath.IsAbs(name) || strings.HasPrefix(name, ".") { + return name, errors.Errorf("path %q not found", name) } - return abs, nil - } - if filepath.IsAbs(name) || strings.HasPrefix(name, ".") { - return name, errors.Errorf("path %q not found", name) } + return c.locateRemoteChart(name, settings) +} + +func (c *ChartPathOptions) locateRemoteChart(name string, settings *cli.EnvSettings) (string, error) { + version := strings.TrimSpace(c.Version) + dl := downloader.ChartDownloader{ Out: os.Stdout, Keyring: c.Keyring,