refactor: Remove duplicate `FindChartIn*RepoURL` funcs

Signed-off-by: George Jenkins <gvjenkins@gmail.com>
pull/13579/head
George Jenkins 9 months ago
parent 0d66425d9a
commit 7ea1d1df66

@ -789,8 +789,18 @@ func (c *ChartPathOptions) LocateChart(name string, settings *cli.EnvSettings) (
dl.Verify = downloader.VerifyAlways dl.Verify = downloader.VerifyAlways
} }
if c.RepoURL != "" { if c.RepoURL != "" {
chartURL, err := repo.FindChartInAuthAndTLSAndPassRepoURL(c.RepoURL, c.Username, c.Password, name, version, chartURL, err := repo.FindChartInRepoURL(
c.CertFile, c.KeyFile, c.CaFile, c.InsecureSkipTLSverify, c.PassCredentialsAll, getter.All(settings)) c.RepoURL,
name,
version,
c.CertFile,
c.KeyFile,
c.CaFile,
getter.All(settings),
repo.WithUsernamePassword(c.Username, c.Password),
repo.WithInsecureSkipTLSverify(c.InsecureSkipTLSverify),
repo.WithPassCredentialsAll(c.PassCredentialsAll),
)
if err != nil { if err != nil {
return "", err return "", err
} }

@ -117,7 +117,18 @@ func (p *Pull) Run(chartRef string) (string, error) {
} }
if p.RepoURL != "" { if p.RepoURL != "" {
chartURL, err := repo.FindChartInAuthAndTLSAndPassRepoURL(p.RepoURL, p.Username, p.Password, chartRef, p.Version, p.CertFile, p.KeyFile, p.CaFile, p.InsecureSkipTLSverify, p.PassCredentialsAll, getter.All(p.Settings)) chartURL, err := repo.FindChartInRepoURL(
p.RepoURL,
chartRef,
p.Version,
p.CertFile,
p.KeyFile,
p.CaFile,
getter.All(p.Settings),
repo.WithUsernamePassword(p.Username, p.Password),
repo.WithInsecureSkipTLSverify(p.InsecureSkipTLSverify),
repo.WithPassCredentialsAll(p.PassCredentialsAll),
)
if err != nil { if err != nil {
return out.String(), err return out.String(), err
} }

@ -196,33 +196,45 @@ func (r *ChartRepository) generateIndex() error {
return nil return nil
} }
// FindChartInRepoURL finds chart in chart repository pointed by repoURL type findChartInRepoURLOptions struct {
// without adding repo to repositories Username string
func FindChartInRepoURL(repoURL, chartName, chartVersion, certFile, keyFile, caFile string, getters getter.Providers) (string, error) { Password string
return FindChartInAuthRepoURL(repoURL, "", "", chartName, chartVersion, certFile, keyFile, caFile, getters) PassCredentialsAll bool
InsecureSkipTLSverify bool
}
type FindChartInRepoURLOption func(*findChartInRepoURLOptions)
// WithUsernamePassword specifies the username/password credntials for the repository
func WithUsernamePassword(username, password string) FindChartInRepoURLOption {
return func(options *findChartInRepoURLOptions) {
options.Username = username
options.Password = password
}
} }
// FindChartInAuthRepoURL finds chart in chart repository pointed by repoURL // WithPassCredentialsAll flags whether credentials should be passed on to other domains
// without adding repo to repositories, like FindChartInRepoURL, func WithPassCredentialsAll(passCredentialsAll bool) FindChartInRepoURLOption {
// but it also receives credentials for the chart repository. return func(options *findChartInRepoURLOptions) {
func FindChartInAuthRepoURL(repoURL, username, password, chartName, chartVersion, certFile, keyFile, caFile string, getters getter.Providers) (string, error) { options.PassCredentialsAll = passCredentialsAll
return FindChartInAuthAndTLSRepoURL(repoURL, username, password, chartName, chartVersion, certFile, keyFile, caFile, false, getters) }
} }
// FindChartInAuthAndTLSRepoURL finds chart in chart repository pointed by repoURL // WithInsecureSkipTLSverify skips TLS verification for repostory communication
// without adding repo to repositories, like FindChartInRepoURL, func WithInsecureSkipTLSverify(insecureSkipTLSverify bool) FindChartInRepoURLOption {
// but it also receives credentials and TLS verify flag for the chart repository. return func(options *findChartInRepoURLOptions) {
// TODO Helm 4, FindChartInAuthAndTLSRepoURL should be integrated into FindChartInAuthRepoURL. options.InsecureSkipTLSverify = insecureSkipTLSverify
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 // FindChartInRepoURL finds chart in chart repository pointed by repoURL
// without adding repo to repositories, like FindChartInRepoURL, // without adding repo to repositories
// but it also receives credentials, TLS verify flag, and if credentials should func FindChartInRepoURL(repoURL, chartName, chartVersion, certFile, keyFile, caFile string, getters getter.Providers, options ...FindChartInRepoURLOption) (string, error) {
// be passed on to other domains.
// TODO Helm 4, FindChartInAuthAndTLSAndPassRepoURL should be integrated into FindChartInAuthRepoURL. opts := findChartInRepoURLOptions{}
func FindChartInAuthAndTLSAndPassRepoURL(repoURL, username, password, chartName, chartVersion, certFile, keyFile, caFile string, insecureSkipTLSverify, passCredentialsAll bool, getters getter.Providers) (string, error) { for _, option := range options {
option(&opts)
}
// 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)
@ -231,14 +243,14 @@ func FindChartInAuthAndTLSAndPassRepoURL(repoURL, username, password, chartName,
c := Entry{ c := Entry{
URL: repoURL, URL: repoURL,
Username: username, Username: opts.Username,
Password: password, Password: opts.Password,
PassCredentialsAll: passCredentialsAll, PassCredentialsAll: opts.PassCredentialsAll,
CertFile: certFile, CertFile: certFile,
KeyFile: keyFile, KeyFile: keyFile,
CAFile: caFile, CAFile: caFile,
Name: name, Name: name,
InsecureSkipTLSverify: insecureSkipTLSverify, InsecureSkipTLSverify: opts.InsecureSkipTLSverify,
} }
r, err := NewChartRepository(&c, getters) r, err := NewChartRepository(&c, getters)
if err != nil { if err != nil {

@ -297,7 +297,16 @@ func TestFindChartInAuthAndTLSAndPassRepoURL(t *testing.T) {
} }
defer srv.Close() defer srv.Close()
chartURL, err := FindChartInAuthAndTLSAndPassRepoURL(srv.URL, "", "", "nginx", "", "", "", "", true, false, getter.All(&cli.EnvSettings{})) chartURL, err := FindChartInRepoURL(
srv.URL,
"nginx",
"",
"",
"",
"",
getter.All(&cli.EnvSettings{}),
WithInsecureSkipTLSverify(true),
)
if err != nil { if err != nil {
t.Fatalf("%v", err) t.Fatalf("%v", err)
} }
@ -306,7 +315,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 = FindChartInRepoURL(srv.URL, "nginx", "0.1.0", "", "", "", 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

Loading…
Cancel
Save