From dbb4eb6b759a80384e1962d3a1eea5dac3275920 Mon Sep 17 00:00:00 2001 From: Mathieu Parent Date: Wed, 2 Jun 2021 14:23:05 +0200 Subject: [PATCH 1/8] Ensure RawPath match Path when resolving reference Signed-off-by: Mathieu Parent --- pkg/repo/chartrepo.go | 5 ++--- pkg/repo/chartrepo_test.go | 8 ++++++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/pkg/repo/chartrepo.go b/pkg/repo/chartrepo.go index 67ede93fd..956997cc9 100644 --- a/pkg/repo/chartrepo.go +++ b/pkg/repo/chartrepo.go @@ -286,7 +286,8 @@ func FindChartInAuthAndTLSAndPassRepoURL(repoURL, username, password, chartName, // ResolveReferenceURL resolves refURL relative to baseURL. // If refURL is absolute, it simply returns refURL. func ResolveReferenceURL(baseURL, refURL string) (string, error) { - parsedBaseURL, err := url.Parse(baseURL) + // We need a trailing slash for ResolveReference to work, but make sure there isn't already one + parsedBaseURL, err := url.Parse(strings.TrimSuffix(baseURL, "/") + "/") if err != nil { return "", errors.Wrapf(err, "failed to parse %s as URL", baseURL) } @@ -296,8 +297,6 @@ func ResolveReferenceURL(baseURL, refURL string) (string, error) { return "", errors.Wrapf(err, "failed to parse %s as URL", refURL) } - // We need a trailing slash for ResolveReference to work, but make sure there isn't already one - parsedBaseURL.Path = strings.TrimSuffix(parsedBaseURL.Path, "/") + "/" return parsedBaseURL.ResolveReference(parsedRefURL).String(), nil } diff --git a/pkg/repo/chartrepo_test.go b/pkg/repo/chartrepo_test.go index 85401284e..0f317b2fd 100644 --- a/pkg/repo/chartrepo_test.go +++ b/pkg/repo/chartrepo_test.go @@ -400,4 +400,12 @@ func TestResolveReferenceURL(t *testing.T) { if chartURL != "https://charts.helm.sh/stable/nginx-0.2.0.tgz" { t.Errorf("%s", chartURL) } + + chartURL, err = ResolveReferenceURL("http://localhost:8123/charts%2fwith%2fescaped%2fslash", "nginx-0.2.0.tgz") + if err != nil { + t.Errorf("%s", err) + } + if chartURL != "http://localhost:8123/charts%2fwith%2fescaped%2fslash/nginx-0.2.0.tgz" { + t.Errorf("%s", chartURL) + } } From 2f63045f259c98187b61b00529895117c05bb02f Mon Sep 17 00:00:00 2001 From: Josh Dolitsky Date: Wed, 7 Jul 2021 10:53:35 -0500 Subject: [PATCH 2/8] Add build-pr github actions workflow Signed-off-by: Josh Dolitsky --- .github/workflows/build-pr.yml | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 .github/workflows/build-pr.yml diff --git a/.github/workflows/build-pr.yml b/.github/workflows/build-pr.yml new file mode 100644 index 000000000..ecb85f62e --- /dev/null +++ b/.github/workflows/build-pr.yml @@ -0,0 +1,26 @@ +name: build-pr +on: + pull_request: + branches: + - main +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout source code + uses: actions/checkout@v2 + - name: Setup Go + uses: actions/setup-go@v1 + with: + go-version: '1.16' + - name: Install golangci-lint + run: | + curl -sSL https://github.com/golangci/golangci-lint/releases/download/v$GOLANGCI_LINT_VERSION/golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64.tar.gz | tar xz + sudo mv golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64/golangci-lint /usr/local/bin/golangci-lint + rm -rf golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64 + env: + GOLANGCI_LINT_VERSION: '1.36.0' + - name: Test style + run: make test-style + - name: Run unit tests + run: make test-coverage From 7605db034de36dc991871c1eb90030f88c484dc0 Mon Sep 17 00:00:00 2001 From: Josh Dolitsky Date: Wed, 7 Jul 2021 12:19:59 -0500 Subject: [PATCH 3/8] Use setup-go v2 Signed-off-by: Josh Dolitsky --- .github/workflows/build-pr.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-pr.yml b/.github/workflows/build-pr.yml index ecb85f62e..e94efbbf9 100644 --- a/.github/workflows/build-pr.yml +++ b/.github/workflows/build-pr.yml @@ -10,7 +10,7 @@ jobs: - name: Checkout source code uses: actions/checkout@v2 - name: Setup Go - uses: actions/setup-go@v1 + uses: actions/setup-go@v2 with: go-version: '1.16' - name: Install golangci-lint From f9b1445b63a541347dbabfe5f1cda9c4123f1843 Mon Sep 17 00:00:00 2001 From: Kally Fox Date: Thu, 8 Jul 2021 21:14:06 +0800 Subject: [PATCH 4/8] refactor: use os instead of ioutil's ReadDir Signed-off-by: Kally Fox --- cmd/helm/dependency_update_test.go | 3 +-- cmd/helm/search_repo.go | 3 ++- internal/third_party/dep/fs/fs.go | 3 +-- pkg/action/lint.go | 2 +- pkg/downloader/manager.go | 2 +- 5 files changed, 6 insertions(+), 7 deletions(-) diff --git a/cmd/helm/dependency_update_test.go b/cmd/helm/dependency_update_test.go index 9f7b0f303..642c9aeca 100644 --- a/cmd/helm/dependency_update_test.go +++ b/cmd/helm/dependency_update_test.go @@ -17,7 +17,6 @@ package main import ( "fmt" - "io/ioutil" "os" "path/filepath" "strings" @@ -188,7 +187,7 @@ func TestDependencyUpdateCmd_DoNotDeleteOldChartsOnError(t *testing.T) { } // Make sure charts dir still has dependencies - files, err := ioutil.ReadDir(filepath.Join(dir(chartname), "charts")) + files, err := os.ReadDir(filepath.Join(dir(chartname), "charts")) if err != nil { t.Fatal(err) } diff --git a/cmd/helm/search_repo.go b/cmd/helm/search_repo.go index ba692a2e7..3a5a23848 100644 --- a/cmd/helm/search_repo.go +++ b/cmd/helm/search_repo.go @@ -22,6 +22,7 @@ import ( "fmt" "io" "io/ioutil" + "os" "path/filepath" "strings" @@ -340,7 +341,7 @@ func compListCharts(toComplete string, includeFiles bool) ([]string, cobra.Shell // listing the entire content of the current directory which will // be too many choices for the user to find the real repos) if includeFiles && len(completions) > 0 && len(toComplete) > 0 { - if files, err := ioutil.ReadDir("."); err == nil { + if files, err := os.ReadDir("."); err == nil { for _, file := range files { if strings.HasPrefix(file.Name(), toComplete) { // We are completing a file prefix diff --git a/internal/third_party/dep/fs/fs.go b/internal/third_party/dep/fs/fs.go index 832592197..4e4eacc60 100644 --- a/internal/third_party/dep/fs/fs.go +++ b/internal/third_party/dep/fs/fs.go @@ -33,7 +33,6 @@ package fs import ( "io" - "io/ioutil" "os" "path/filepath" "runtime" @@ -119,7 +118,7 @@ func CopyDir(src, dst string) error { return errors.Wrapf(err, "cannot mkdir %s", dst) } - entries, err := ioutil.ReadDir(src) + entries, err := os.ReadDir(src) if err != nil { return errors.Wrapf(err, "cannot read directory %s", dst) } diff --git a/pkg/action/lint.go b/pkg/action/lint.go index 2292c14bf..bdb93dcc2 100644 --- a/pkg/action/lint.go +++ b/pkg/action/lint.go @@ -96,7 +96,7 @@ func lintChart(path string, vals map[string]interface{}, namespace string, stric return linter, errors.Wrap(err, "unable to extract tarball") } - files, err := ioutil.ReadDir(tempDir) + files, err := os.ReadDir(tempDir) if err != nil { return linter, errors.Wrapf(err, "unable to read temporary output directory %s", tempDir) } diff --git a/pkg/downloader/manager.go b/pkg/downloader/manager.go index ff5f9c4e7..9491f41ad 100644 --- a/pkg/downloader/manager.go +++ b/pkg/downloader/manager.go @@ -870,7 +870,7 @@ func tarFromLocalDir(chartpath, name, repo, version string) (string, error) { // move files from tmppath to destpath func move(tmpPath, destPath string) error { - files, _ := ioutil.ReadDir(tmpPath) + files, _ := os.ReadDir(tmpPath) for _, file := range files { filename := file.Name() tmpfile := filepath.Join(tmpPath, filename) From b16091a01a9c32164a9c9eb3879b8c4933c133d4 Mon Sep 17 00:00:00 2001 From: Josh Dolitsky Date: Thu, 8 Jul 2021 09:59:23 -0500 Subject: [PATCH 5/8] verify golangci-lint sha256 checksum Signed-off-by: Josh Dolitsky --- .github/workflows/build-pr.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-pr.yml b/.github/workflows/build-pr.yml index e94efbbf9..d35be3f4b 100644 --- a/.github/workflows/build-pr.yml +++ b/.github/workflows/build-pr.yml @@ -15,11 +15,14 @@ jobs: go-version: '1.16' - name: Install golangci-lint run: | - curl -sSL https://github.com/golangci/golangci-lint/releases/download/v$GOLANGCI_LINT_VERSION/golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64.tar.gz | tar xz + curl -sSLO https://github.com/golangci/golangci-lint/releases/download/v$GOLANGCI_LINT_VERSION/golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64.tar.gz + shasum -a 256 golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64.tar.gz | grep "^$GOLANGCI_LINT_SHA256 " > /dev/null + tar -xf golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64.tar.gz sudo mv golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64/golangci-lint /usr/local/bin/golangci-lint - rm -rf golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64 + rm -rf golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64* env: GOLANGCI_LINT_VERSION: '1.36.0' + GOLANGCI_LINT_SHA256: '9b8856b3a1c9bfbcf3a06b78e94611763b79abd9751c245246787cd3bf0e78a5' - name: Test style run: make test-style - name: Run unit tests From 07a25a26f62e55a6e05a5d701590368c212a4c14 Mon Sep 17 00:00:00 2001 From: Josh Dolitsky Date: Thu, 8 Jul 2021 10:06:16 -0500 Subject: [PATCH 6/8] Use official golangci-lint action Signed-off-by: Josh Dolitsky --- .github/workflows/build-pr.yml | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/.github/workflows/build-pr.yml b/.github/workflows/build-pr.yml index d35be3f4b..34f4061c1 100644 --- a/.github/workflows/build-pr.yml +++ b/.github/workflows/build-pr.yml @@ -12,17 +12,11 @@ jobs: - name: Setup Go uses: actions/setup-go@v2 with: - go-version: '1.16' + go-version: 1.16 - name: Install golangci-lint - run: | - curl -sSLO https://github.com/golangci/golangci-lint/releases/download/v$GOLANGCI_LINT_VERSION/golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64.tar.gz - shasum -a 256 golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64.tar.gz | grep "^$GOLANGCI_LINT_SHA256 " > /dev/null - tar -xf golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64.tar.gz - sudo mv golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64/golangci-lint /usr/local/bin/golangci-lint - rm -rf golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64* - env: - GOLANGCI_LINT_VERSION: '1.36.0' - GOLANGCI_LINT_SHA256: '9b8856b3a1c9bfbcf3a06b78e94611763b79abd9751c245246787cd3bf0e78a5' + uses: golangci/golangci-lint-action@v2 + with: + version: v1.36.0 - name: Test style run: make test-style - name: Run unit tests From 28f44b82691f71220f610bfaa1f544f49253a2bd Mon Sep 17 00:00:00 2001 From: Josh Dolitsky Date: Thu, 8 Jul 2021 10:10:37 -0500 Subject: [PATCH 7/8] Revert "Use official golangci-lint action" Signed-off-by: Josh Dolitsky --- .github/workflows/build-pr.yml | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build-pr.yml b/.github/workflows/build-pr.yml index 34f4061c1..d35be3f4b 100644 --- a/.github/workflows/build-pr.yml +++ b/.github/workflows/build-pr.yml @@ -12,11 +12,17 @@ jobs: - name: Setup Go uses: actions/setup-go@v2 with: - go-version: 1.16 + go-version: '1.16' - name: Install golangci-lint - uses: golangci/golangci-lint-action@v2 - with: - version: v1.36.0 + run: | + curl -sSLO https://github.com/golangci/golangci-lint/releases/download/v$GOLANGCI_LINT_VERSION/golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64.tar.gz + shasum -a 256 golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64.tar.gz | grep "^$GOLANGCI_LINT_SHA256 " > /dev/null + tar -xf golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64.tar.gz + sudo mv golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64/golangci-lint /usr/local/bin/golangci-lint + rm -rf golangci-lint-$GOLANGCI_LINT_VERSION-linux-amd64* + env: + GOLANGCI_LINT_VERSION: '1.36.0' + GOLANGCI_LINT_SHA256: '9b8856b3a1c9bfbcf3a06b78e94611763b79abd9751c245246787cd3bf0e78a5' - name: Test style run: make test-style - name: Run unit tests From 6951cd50769d195d13d195956a5a04ac970cba4f Mon Sep 17 00:00:00 2001 From: Marc Khouzam Date: Fri, 18 Jun 2021 15:12:39 -0400 Subject: [PATCH 8/8] feat(cmd): Allow to specify which repos to update This is a port to helm v3 of #5182. A little more flexible than the v2 version, it allows to specify a list of repositories that should be updated. Signed-off-by: Marc Khouzam --- cmd/helm/repo_update.go | 68 ++++++++++++++++++++++++----- cmd/helm/repo_update_test.go | 51 +++++++++++++++++++++- cmd/helm/testdata/repositories.yaml | 5 +++ 3 files changed, 110 insertions(+), 14 deletions(-) diff --git a/cmd/helm/repo_update.go b/cmd/helm/repo_update.go index 23dca194a..43036847d 100644 --- a/cmd/helm/repo_update.go +++ b/cmd/helm/repo_update.go @@ -32,6 +32,10 @@ import ( const updateDesc = ` Update gets the latest information about charts from the respective chart repositories. Information is cached locally, where it is used by commands like 'helm search'. + +You can optionally specify a list of repositories you want to update. + $ helm repo update ... +To update all the repositories, use 'helm repo update'. ` var errNoRepositories = errors.New("no repositories found. You must add one before updating") @@ -40,21 +44,25 @@ type repoUpdateOptions struct { update func([]*repo.ChartRepository, io.Writer) repoFile string repoCache string + names []string } func newRepoUpdateCmd(out io.Writer) *cobra.Command { o := &repoUpdateOptions{update: updateCharts} cmd := &cobra.Command{ - Use: "update", - Aliases: []string{"up"}, - Short: "update information of available charts locally from chart repositories", - Long: updateDesc, - Args: require.NoArgs, - ValidArgsFunction: noCompletions, + Use: "update [REPO1 [REPO2 ...]]", + Aliases: []string{"up"}, + Short: "update information of available charts locally from chart repositories", + Long: updateDesc, + Args: require.MinimumNArgs(0), + ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { + return compListRepos(toComplete, args), cobra.ShellCompDirectiveNoFileComp + }, RunE: func(cmd *cobra.Command, args []string) error { o.repoFile = settings.RepositoryConfig o.repoCache = settings.RepositoryCache + o.names = args return o.run(out) }, } @@ -73,15 +81,26 @@ func (o *repoUpdateOptions) run(out io.Writer) error { } var repos []*repo.ChartRepository - for _, cfg := range f.Repositories { - r, err := repo.NewChartRepository(cfg, getter.All(settings)) - if err != nil { + updateAllRepos := len(o.names) == 0 + + if !updateAllRepos { + // Fail early if the user specified an invalid repo to update + if err := checkRequestedRepos(o.names, f.Repositories); err != nil { return err } - if o.repoCache != "" { - r.CachePath = o.repoCache + } + + for _, cfg := range f.Repositories { + if updateAllRepos || isRepoRequested(cfg.Name, o.names) { + r, err := repo.NewChartRepository(cfg, getter.All(settings)) + if err != nil { + return err + } + if o.repoCache != "" { + r.CachePath = o.repoCache + } + repos = append(repos, r) } - repos = append(repos, r) } o.update(repos, out) @@ -105,3 +124,28 @@ func updateCharts(repos []*repo.ChartRepository, out io.Writer) { wg.Wait() fmt.Fprintln(out, "Update Complete. ⎈Happy Helming!⎈") } + +func checkRequestedRepos(requestedRepos []string, validRepos []*repo.Entry) error { + for _, requestedRepo := range requestedRepos { + found := false + for _, repo := range validRepos { + if requestedRepo == repo.Name { + found = true + break + } + } + if !found { + return errors.Errorf("no repositories found matching '%s'. Nothing will be updated", requestedRepo) + } + } + return nil +} + +func isRepoRequested(repoName string, requestedRepos []string) bool { + for _, requestedRepo := range requestedRepos { + if repoName == requestedRepo { + return true + } + } + return false +} diff --git a/cmd/helm/repo_update_test.go b/cmd/helm/repo_update_test.go index 83ef24349..d769c8aa7 100644 --- a/cmd/helm/repo_update_test.go +++ b/cmd/helm/repo_update_test.go @@ -48,8 +48,54 @@ func TestUpdateCmd(t *testing.T) { t.Fatal(err) } - if got := out.String(); !strings.Contains(got, "charts") { - t.Errorf("Expected 'charts' got %q", got) + if got := out.String(); !strings.Contains(got, "charts") || + !strings.Contains(got, "firstexample") || + !strings.Contains(got, "secondexample") { + t.Errorf("Expected 'charts', 'firstexample' and 'secondexample' but got %q", got) + } +} + +func TestUpdateCmdMultiple(t *testing.T) { + var out bytes.Buffer + // Instead of using the HTTP updater, we provide our own for this test. + // The TestUpdateCharts test verifies the HTTP behavior independently. + updater := func(repos []*repo.ChartRepository, out io.Writer) { + for _, re := range repos { + fmt.Fprintln(out, re.Config.Name) + } + } + o := &repoUpdateOptions{ + update: updater, + repoFile: "testdata/repositories.yaml", + names: []string{"firstexample", "charts"}, + } + if err := o.run(&out); err != nil { + t.Fatal(err) + } + + if got := out.String(); !strings.Contains(got, "charts") || + !strings.Contains(got, "firstexample") || + strings.Contains(got, "secondexample") { + t.Errorf("Expected 'charts' and 'firstexample' but not 'secondexample' but got %q", got) + } +} + +func TestUpdateCmdInvalid(t *testing.T) { + var out bytes.Buffer + // Instead of using the HTTP updater, we provide our own for this test. + // The TestUpdateCharts test verifies the HTTP behavior independently. + updater := func(repos []*repo.ChartRepository, out io.Writer) { + for _, re := range repos { + fmt.Fprintln(out, re.Config.Name) + } + } + o := &repoUpdateOptions{ + update: updater, + repoFile: "testdata/repositories.yaml", + names: []string{"firstexample", "invalid"}, + } + if err := o.run(&out); err == nil { + t.Fatal("expected error but did not get one") } } @@ -111,4 +157,5 @@ func TestUpdateCharts(t *testing.T) { func TestRepoUpdateFileCompletion(t *testing.T) { checkFileCompletion(t, "repo update", false) + checkFileCompletion(t, "repo update repo1", false) } diff --git a/cmd/helm/testdata/repositories.yaml b/cmd/helm/testdata/repositories.yaml index 423b9f195..6be26b771 100644 --- a/cmd/helm/testdata/repositories.yaml +++ b/cmd/helm/testdata/repositories.yaml @@ -2,3 +2,8 @@ apiVersion: v1 repositories: - name: charts url: "https://charts.helm.sh/stable" + - name: firstexample + url: "http://firstexample.com" + - name: secondexample + url: "http://secondexample.com" +