diff --git a/pkg/repo/chartrepo.go b/pkg/repo/chartrepo.go index 0dbacfa94..4eac81e2b 100644 --- a/pkg/repo/chartrepo.go +++ b/pkg/repo/chartrepo.go @@ -66,6 +66,7 @@ func NewChartRepository(cfg *Entry, getters getter.Providers) (*ChartRepository, client, err := getterConstructor( getter.WithURL(cfg.URL), getter.WithTLSClientConfig(cfg.CertFile, cfg.KeyFile, cfg.CAFile), + getter.WithBasicAuth(cfg.Username, cfg.Password), ) if err != nil { return nil, errors.Wrapf(err, "could not construct protocol handler for: %s", u.Scheme) @@ -124,15 +125,7 @@ func (r *ChartRepository) DownloadIndexFile(cachePath string) error { indexURL = parsedURL.String() // TODO add user-agent - g, err := getter.NewHTTPGetter( - getter.WithURL(indexURL), - getter.WithTLSClientConfig(r.Config.CertFile, r.Config.KeyFile, r.Config.CAFile), - getter.WithBasicAuth(r.Config.Username, r.Config.Password), - ) - if err != nil { - return err - } - resp, err := g.Get(indexURL) + resp, err := r.Client.Get(indexURL) if err != nil { return err } diff --git a/pkg/repo/chartrepo_test.go b/pkg/repo/chartrepo_test.go index 8e2071b01..df624cfca 100644 --- a/pkg/repo/chartrepo_test.go +++ b/pkg/repo/chartrepo_test.go @@ -17,6 +17,7 @@ limitations under the License. package repo import ( + "bytes" "io/ioutil" "net/http" "net/http/httptest" @@ -30,6 +31,8 @@ import ( "helm.sh/helm/pkg/chart" "helm.sh/helm/pkg/cli" "helm.sh/helm/pkg/getter" + + "sigs.k8s.io/yaml" ) const ( @@ -108,6 +111,64 @@ func TestIndex(t *testing.T) { verifyIndex(t, second) } +type CustomGetter struct { + repoUrls []string +} + +func (g *CustomGetter) Get(href string) (*bytes.Buffer, error) { + index := &IndexFile{ + APIVersion: "v1", + Generated: time.Now(), + } + indexBytes, err := yaml.Marshal(index) + if err != nil { + return nil, err + } + g.repoUrls = append(g.repoUrls, href) + return bytes.NewBuffer(indexBytes), nil +} + +func TestIndexCustomSchemeDownload(t *testing.T) { + repoName := "gcs-repo" + repoURL := "gs://some-gcs-bucket" + myCustomGetter := &CustomGetter{} + customGetterConstructor := func(options ...getter.Option) (getter.Getter, error) { + return myCustomGetter, nil + } + providers := getter.Providers{ + { + Schemes: []string{"gs"}, + New: customGetterConstructor, + }, + } + repo, err := NewChartRepository(&Entry{ + Name: repoName, + URL: repoURL, + }, providers) + if err != nil { + t.Fatalf("Problem loading chart repository from %s: %v", repoURL, err) + } + + tempIndexFile, err := ioutil.TempFile("", "test-repo") + if err != nil { + t.Fatalf("Failed to create temp index file: %v", err) + } + defer os.Remove(tempIndexFile.Name()) + + if err := repo.DownloadIndexFile(tempIndexFile.Name()); err != nil { + t.Fatalf("Failed to download index file: %v", err) + } + + if len(myCustomGetter.repoUrls) != 1 { + t.Fatalf("Custom Getter.Get should be called once") + } + + expectedRepoIndexURL := repoURL + "/index.yaml" + if myCustomGetter.repoUrls[0] != expectedRepoIndexURL { + t.Fatalf("Custom Getter.Get should be called with %s", expectedRepoIndexURL) + } +} + func verifyIndex(t *testing.T, actual *IndexFile) { var empty time.Time if actual.Generated == empty {