Skip to content

Commit

Permalink
Add extra error info when downloading remote charts
Browse files Browse the repository at this point in the history
Refers to: #3160

Signed-off-by: Xavi Garcia <xavi.garcia@suse.com>
  • Loading branch information
0xavi0 committed Feb 18, 2025
1 parent 08d9ccb commit 9785c69
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
22 changes: 21 additions & 1 deletion integrationtests/cli/apply/helm_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package apply

import (
"fmt"
"io/fs"
"os"
"path/filepath"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/rancher/fleet/integrationtests/cli"
"github.com/rancher/fleet/internal/bundlereader"
"github.com/rancher/fleet/internal/cmd/cli/apply"
fleet "github.com/rancher/fleet/pkg/apis/fleet.cattle.io/v1alpha1"
"sigs.k8s.io/yaml"
)

const (
Expand Down Expand Up @@ -163,14 +167,30 @@ func testHelmRepo(path, port string) {
authEnabled = true
})
It("fleet apply fails when --helm-repo-url-regex is not valid", func() {
// read the fleet.yaml file, so we can later check if the error message
// contains the expected chart information.
assetsDir := filepath.Join(cli.AssetsPath, path)
data, err := os.ReadFile(filepath.Join(assetsDir, "fleet.yaml"))
Expect(err).ToNot(HaveOccurred())

fy := &fleet.FleetYAML{}
err = yaml.Unmarshal(data, fy)
Expect(err).ToNot(HaveOccurred())

Eventually(func() string {
err := fleetApply("helm", []string{cli.AssetsPath + path}, apply.Options{
Auth: bundlereader.Auth{Username: username, Password: password},
HelmRepoURLRegex: "a(b",
})
Expect(err).To(HaveOccurred())
return err.Error()
}).Should(Equal("error parsing regexp: missing closing ): `a(b`"))
}).Should(Equal(
fmt.Sprintf(
"repo=%s chart=%s version=%s: error parsing regexp: missing closing ): `a(b`",
fy.Helm.Repo,
fy.Helm.Chart,
fy.Helm.Version,
)))
})
})

Expand Down
14 changes: 12 additions & 2 deletions internal/bundlereader/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,15 @@ func addRemoteCharts(directories []directory, base string, charts []*fleet.HelmO
if _, err := os.Stat(filepath.Join(base, chart.Chart)); os.IsNotExist(err) || chart.Repo != "" {
shouldAddAuthToRequest, err := shouldAddAuthToRequest(helmRepoURLRegex, chart.Repo, chart.Chart)
if err != nil {
return nil, err
return nil, downloadChartError(*chart, err)
}
if !shouldAddAuthToRequest {
auth = Auth{}
}

chartURL, err := chartURL(*chart, auth)
if err != nil {
return nil, err
return nil, downloadChartError(*chart, err)
}

directories = append(directories, directory{
Expand All @@ -178,6 +178,16 @@ func addRemoteCharts(directories []directory, base string, charts []*fleet.HelmO
return directories, nil
}

func downloadChartError(c fleet.HelmOptions, e error) error {
return fmt.Errorf(
"repo=%s chart=%s version=%s: %w",
c.Repo,
c.Chart,
c.Version,
e,
)
}

func shouldAddAuthToRequest(helmRepoURLRegex, repo, chart string) (bool, error) {
if helmRepoURLRegex == "" {
return true, nil
Expand Down

0 comments on commit 9785c69

Please # to comment.