Consolidate and refactor chart repository functions into FindChartInAuthRepoURL

-e
Signed-off-by: Payal Godhani <godhanipayal@gmail.com>
pull/13678/head
Payal Godhani 8 months ago
parent 0d66425d9a
commit ebb7643af1

@ -196,34 +196,9 @@ func (r *ChartRepository) generateIndex() error {
return nil return nil
} }
// FindChartInRepoURL finds chart in chart repository pointed by repoURL
// without adding repo to repositories
func FindChartInRepoURL(repoURL, chartName, chartVersion, certFile, keyFile, caFile string, getters getter.Providers) (string, error) {
return FindChartInAuthRepoURL(repoURL, "", "", chartName, chartVersion, certFile, keyFile, caFile, getters)
}
// FindChartInAuthRepoURL finds chart in chart repository pointed by repoURL // FindChartInAuthRepoURL finds chart in chart repository pointed by repoURL
// without adding repo to repositories, like FindChartInRepoURL, // without adding repo to repositories, and supports authentication, TLS settings, and optional passing of credentials to other domains.
// but it also receives credentials for the chart repository. func FindChartInAuthRepoURL(repoURL, username, password, chartName, chartVersion, certFile, keyFile, caFile string, insecureSkipTLSverify, passCredentialsAll bool, getters getter.Providers) (string, error) {
func FindChartInAuthRepoURL(repoURL, username, password, chartName, chartVersion, certFile, keyFile, caFile string, getters getter.Providers) (string, error) {
return FindChartInAuthAndTLSRepoURL(repoURL, username, password, chartName, chartVersion, certFile, keyFile, caFile, false, getters)
}
// FindChartInAuthAndTLSRepoURL finds chart in chart repository pointed by repoURL
// without adding repo to repositories, like FindChartInRepoURL,
// but it also receives credentials and TLS verify flag for the chart repository.
// TODO Helm 4, FindChartInAuthAndTLSRepoURL should be integrated into FindChartInAuthRepoURL.
func FindChartInAuthAndTLSRepoURL(repoURL, username, password, chartName, chartVersion, certFile, keyFile, caFile string, insecureSkipTLSverify bool, getters getter.Providers) (string, error) {
return FindChartInAuthAndTLSAndPassRepoURL(repoURL, username, password, chartName, chartVersion, certFile, keyFile, caFile, insecureSkipTLSverify, false, getters)
}
// FindChartInAuthAndTLSAndPassRepoURL finds chart in chart repository pointed by repoURL
// without adding repo to repositories, like FindChartInRepoURL,
// but it also receives credentials, TLS verify flag, and if credentials should
// be passed on to other domains.
// TODO Helm 4, FindChartInAuthAndTLSAndPassRepoURL should be integrated into FindChartInAuthRepoURL.
func FindChartInAuthAndTLSAndPassRepoURL(repoURL, username, password, chartName, chartVersion, certFile, keyFile, caFile string, insecureSkipTLSverify, passCredentialsAll bool, getters getter.Providers) (string, error) {
// Download and write the index file to a temporary location // Download and write the index file to a temporary location
buf := make([]byte, 20) buf := make([]byte, 20)
rand.Read(buf) rand.Read(buf)
@ -244,6 +219,8 @@ func FindChartInAuthAndTLSAndPassRepoURL(repoURL, username, password, chartName,
if err != nil { if err != nil {
return "", err return "", err
} }
// Download the index file from the repository
idx, err := r.DownloadIndexFile() idx, err := r.DownloadIndexFile()
if err != nil { if err != nil {
return "", errors.Wrapf(err, "looks like %q is not a valid chart repository or cannot be reached", repoURL) return "", errors.Wrapf(err, "looks like %q is not a valid chart repository or cannot be reached", repoURL)
@ -253,27 +230,31 @@ func FindChartInAuthAndTLSAndPassRepoURL(repoURL, username, password, chartName,
os.RemoveAll(filepath.Join(r.CachePath, helmpath.CacheIndexFile(r.Config.Name))) os.RemoveAll(filepath.Join(r.CachePath, helmpath.CacheIndexFile(r.Config.Name)))
}() }()
// Read the index file for the repository to get chart information and return chart URL // Read the index file to get chart information
repoIndex, err := LoadIndexFile(idx) repoIndex, err := LoadIndexFile(idx)
if err != nil { if err != nil {
return "", err return "", err
} }
// Build error message for missing chart version
errMsg := fmt.Sprintf("chart %q", chartName) errMsg := fmt.Sprintf("chart %q", chartName)
if chartVersion != "" { if chartVersion != "" {
errMsg = fmt.Sprintf("%s version %q", errMsg, chartVersion) errMsg = fmt.Sprintf("%s version %q", errMsg, chartVersion)
} }
// Find chart in the index
cv, err := repoIndex.Get(chartName, chartVersion) cv, err := repoIndex.Get(chartName, chartVersion)
if err != nil { if err != nil {
return "", errors.Errorf("%s not found in %s repository", errMsg, repoURL) return "", errors.Errorf("%s not found in %s repository", errMsg, repoURL)
} }
// Ensure the chart has downloadable URLs
if len(cv.URLs) == 0 { if len(cv.URLs) == 0 {
return "", errors.Errorf("%s has no downloadable URLs", errMsg) return "", errors.Errorf("%s has no downloadable URLs", errMsg)
} }
// Resolve the chart URL
chartURL := cv.URLs[0] chartURL := cv.URLs[0]
absoluteChartURL, err := ResolveReferenceURL(repoURL, chartURL) absoluteChartURL, err := ResolveReferenceURL(repoURL, chartURL)
if err != nil { if err != nil {
return "", errors.Wrap(err, "failed to make chart URL absolute") return "", errors.Wrap(err, "failed to make chart URL absolute")

@ -290,14 +290,14 @@ func startLocalTLSServerForTests(handler http.Handler) (*httptest.Server, error)
return httptest.NewTLSServer(handler), nil return httptest.NewTLSServer(handler), nil
} }
func TestFindChartInAuthAndTLSAndPassRepoURL(t *testing.T) { func TestChartInAuthRepoURL(t *testing.T) {
srv, err := startLocalTLSServerForTests(nil) srv, err := startLocalTLSServerForTests(nil)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
defer srv.Close() defer srv.Close()
chartURL, err := FindChartInAuthAndTLSAndPassRepoURL(srv.URL, "", "", "nginx", "", "", "", "", true, false, getter.All(&cli.EnvSettings{})) chartURL, err := FindChartInAuthRepoURL(srv.URL, "", "", "nginx", "", "", "", "", true, false, getter.All(&cli.EnvSettings{}))
if err != nil { if err != nil {
t.Fatalf("%v", err) t.Fatalf("%v", err)
} }
@ -306,7 +306,7 @@ func TestFindChartInAuthAndTLSAndPassRepoURL(t *testing.T) {
} }
// If the insecureSkipTLSVerify is false, it will return an error that contains "x509: certificate signed by unknown authority". // If the insecureSkipTLSVerify is false, it will return an error that contains "x509: certificate signed by unknown authority".
_, err = FindChartInAuthAndTLSAndPassRepoURL(srv.URL, "", "", "nginx", "0.1.0", "", "", "", false, false, getter.All(&cli.EnvSettings{})) _, err = FindChartInAuthRepoURL(srv.URL, "", "", "nginx", "0.1.0", "", "", "", false, false, getter.All(&cli.EnvSettings{}))
// Go communicates with the platform and different platforms return different messages. Go itself tests darwin // Go communicates with the platform and different platforms return different messages. Go itself tests darwin
// differently for its message. On newer versions of Darwin the message includes the "Acme Co" portion while older // differently for its message. On newer versions of Darwin the message includes the "Acme Co" portion while older
// versions of Darwin do not. As there are people developing Helm using both old and new versions of Darwin we test // versions of Darwin do not. As there are people developing Helm using both old and new versions of Darwin we test
@ -327,7 +327,7 @@ func TestFindChartInRepoURL(t *testing.T) {
} }
defer srv.Close() defer srv.Close()
chartURL, err := FindChartInRepoURL(srv.URL, "nginx", "", "", "", "", getter.All(&cli.EnvSettings{})) chartURL, err := FindChartInAuthRepoURL(srv.URL, "", "", "nginx", "", "", "", "", false, false, getter.All(&cli.EnvSettings{}))
if err != nil { if err != nil {
t.Fatalf("%v", err) t.Fatalf("%v", err)
} }
@ -335,7 +335,7 @@ func TestFindChartInRepoURL(t *testing.T) {
t.Errorf("%s is not the valid URL", chartURL) t.Errorf("%s is not the valid URL", chartURL)
} }
chartURL, err = FindChartInRepoURL(srv.URL, "nginx", "0.1.0", "", "", "", getter.All(&cli.EnvSettings{})) chartURL, err = FindChartInAuthRepoURL(srv.URL, "", "", "nginx", "0.1.0", "", "", "", false, false, getter.All(&cli.EnvSettings{}))
if err != nil { if err != nil {
t.Errorf("%s", err) t.Errorf("%s", err)
} }
@ -350,7 +350,7 @@ func TestErrorFindChartInRepoURL(t *testing.T) {
RepositoryCache: t.TempDir(), RepositoryCache: t.TempDir(),
}) })
if _, err := FindChartInRepoURL("http://someserver/something", "nginx", "", "", "", "", g); err == nil { if _, err := FindChartInAuthRepoURL("http://someserver/something", "", "", "nginx", "", "", "", "", false, false, g); err == nil {
t.Errorf("Expected error for bad chart URL, but did not get any errors") t.Errorf("Expected error for bad chart URL, but did not get any errors")
} else if !strings.Contains(err.Error(), `looks like "http://someserver/something" is not a valid chart repository or cannot be reached`) { } else if !strings.Contains(err.Error(), `looks like "http://someserver/something" is not a valid chart repository or cannot be reached`) {
t.Errorf("Expected error for bad chart URL, but got a different error (%v)", err) t.Errorf("Expected error for bad chart URL, but got a different error (%v)", err)
@ -362,19 +362,19 @@ func TestErrorFindChartInRepoURL(t *testing.T) {
} }
defer srv.Close() defer srv.Close()
if _, err = FindChartInRepoURL(srv.URL, "nginx1", "", "", "", "", g); err == nil { if _, err = FindChartInAuthRepoURL(srv.URL, "", "", "nginx1", "", "", "", "", false, false, g); err == nil {
t.Errorf("Expected error for chart not found, but did not get any errors") t.Errorf("Expected error for chart not found, but did not get any errors")
} else if err.Error() != `chart "nginx1" not found in `+srv.URL+` repository` { } else if err.Error() != `chart "nginx1" not found in `+srv.URL+` repository` {
t.Errorf("Expected error for chart not found, but got a different error (%v)", err) t.Errorf("Expected error for chart not found, but got a different error (%v)", err)
} }
if _, err = FindChartInRepoURL(srv.URL, "nginx1", "0.1.0", "", "", "", g); err == nil { if _, err = FindChartInAuthRepoURL(srv.URL, "", "", "nginx1", "0.1.0", "", "", "", false, false, g); err == nil {
t.Errorf("Expected error for chart not found, but did not get any errors") t.Errorf("Expected error for chart not found, but did not get any errors")
} else if err.Error() != `chart "nginx1" version "0.1.0" not found in `+srv.URL+` repository` { } else if err.Error() != `chart "nginx1" version "0.1.0" not found in `+srv.URL+` repository` {
t.Errorf("Expected error for chart not found, but got a different error (%v)", err) t.Errorf("Expected error for chart not found, but got a different error (%v)", err)
} }
if _, err = FindChartInRepoURL(srv.URL, "chartWithNoURL", "", "", "", "", g); err == nil { if _, err = FindChartInAuthRepoURL(srv.URL, "", "", "chartWithNoURL", "", "", "", "", false, false, g); err == nil {
t.Errorf("Expected error for no chart URLs available, but did not get any errors") t.Errorf("Expected error for no chart URLs available, but did not get any errors")
} else if err.Error() != `chart "chartWithNoURL" has no downloadable URLs` { } else if err.Error() != `chart "chartWithNoURL" has no downloadable URLs` {
t.Errorf("Expected error for chart not found, but got a different error (%v)", err) t.Errorf("Expected error for chart not found, but got a different error (%v)", err)

Loading…
Cancel
Save