From 230e37ca522829b6bc8001e2cc0644d5efb6b3c5 Mon Sep 17 00:00:00 2001 From: Josh Dolitsky Date: Mon, 28 Jun 2021 12:43:33 -0400 Subject: [PATCH] remove hardcoded oci strings, add constant/util Signed-off-by: Josh Dolitsky --- internal/experimental/action/push.go | 3 ++- internal/experimental/pusher/ocipusher.go | 2 +- internal/experimental/pusher/pusher.go | 2 +- internal/experimental/pusher/pusher_test.go | 3 ++- internal/experimental/registry/constants.go | 3 +++ internal/experimental/registry/util.go | 7 +++++++ internal/experimental/uploader/chart_uploader.go | 3 ++- internal/resolver/resolver.go | 3 ++- pkg/action/install.go | 3 ++- pkg/action/pull.go | 3 ++- pkg/downloader/chart_downloader.go | 2 +- pkg/downloader/manager.go | 14 +++++++------- pkg/getter/getter.go | 2 +- pkg/getter/ocigetter.go | 2 +- 14 files changed, 34 insertions(+), 18 deletions(-) diff --git a/internal/experimental/action/push.go b/internal/experimental/action/push.go index cd20cedc4..5cd83624b 100644 --- a/internal/experimental/action/push.go +++ b/internal/experimental/action/push.go @@ -20,6 +20,7 @@ import ( "strings" "helm.sh/helm/v3/internal/experimental/pusher" + "helm.sh/helm/v3/internal/experimental/registry" "helm.sh/helm/v3/internal/experimental/uploader" "helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/cli" @@ -62,7 +63,7 @@ func (p *Push) Run(chartRef string, remote string) (string, error) { Options: []pusher.Option{}, } - if strings.HasPrefix(remote, "oci://") { + if registry.IsOCI(remote) { c.Options = append(c.Options, pusher.WithRegistryClient(p.cfg.RegistryClient)) } diff --git a/internal/experimental/pusher/ocipusher.go b/internal/experimental/pusher/ocipusher.go index 11da8fecd..67a0153c3 100644 --- a/internal/experimental/pusher/ocipusher.go +++ b/internal/experimental/pusher/ocipusher.go @@ -76,7 +76,7 @@ func (pusher *OCIPusher) push(chartRef, href string) error { } ref := fmt.Sprintf("%s:%s", - path.Join(strings.TrimPrefix(href, "oci://"), meta.Metadata.Name), + path.Join(strings.TrimPrefix(href, fmt.Sprintf("%s://", registry.OCIScheme)), meta.Metadata.Name), meta.Metadata.Version) _, err = client.Push(chartBytes, ref, pushOpts...) diff --git a/internal/experimental/pusher/pusher.go b/internal/experimental/pusher/pusher.go index 05edb8295..32c1351e9 100644 --- a/internal/experimental/pusher/pusher.go +++ b/internal/experimental/pusher/pusher.go @@ -83,7 +83,7 @@ func (p Providers) ByScheme(scheme string) (Pusher, error) { } var ociProvider = Provider{ - Schemes: []string{"oci"}, + Schemes: []string{registry.OCIScheme}, New: NewOCIPusher, } diff --git a/internal/experimental/pusher/pusher_test.go b/internal/experimental/pusher/pusher_test.go index 5e289bda9..a99b1a5db 100644 --- a/internal/experimental/pusher/pusher_test.go +++ b/internal/experimental/pusher/pusher_test.go @@ -18,6 +18,7 @@ package pusher import ( "testing" + "helm.sh/helm/v3/internal/experimental/registry" "helm.sh/helm/v3/pkg/cli" ) @@ -61,7 +62,7 @@ func TestAll(t *testing.T) { func TestByScheme(t *testing.T) { env := cli.New() g := All(env) - if _, err := g.ByScheme("oci"); err != nil { + if _, err := g.ByScheme(registry.OCIScheme); err != nil { t.Error(err) } } diff --git a/internal/experimental/registry/constants.go b/internal/experimental/registry/constants.go index 5c16f2f24..7f870bc20 100644 --- a/internal/experimental/registry/constants.go +++ b/internal/experimental/registry/constants.go @@ -17,6 +17,9 @@ limitations under the License. package registry // import "helm.sh/helm/v3/internal/experimental/registry" const ( + // OCIScheme is the URL scheme for OCI-based requests + OCIScheme = "oci" + // CredentialsFileBasename is the filename for auth credentials file CredentialsFileBasename = "config.json" diff --git a/internal/experimental/registry/util.go b/internal/experimental/registry/util.go index 21bc1d344..257e7af87 100644 --- a/internal/experimental/registry/util.go +++ b/internal/experimental/registry/util.go @@ -19,7 +19,9 @@ package registry // import "helm.sh/helm/v3/internal/experimental/registry" import ( "bytes" "context" + "fmt" "io" + "strings" "github.com/sirupsen/logrus" orascontext "oras.land/oras-go/pkg/context" @@ -28,6 +30,11 @@ import ( "helm.sh/helm/v3/pkg/chart/loader" ) +// IsOCI determines whether or not a URL is to be treated as an OCI URL +func IsOCI(url string) bool { + return strings.HasPrefix(url, fmt.Sprintf("%s://", OCIScheme)) +} + // extractChartMeta is used to extract a chart metadata from a byte array func extractChartMeta(chartData []byte) (*chart.Metadata, error) { ch, err := loader.LoadArchive(bytes.NewReader(chartData)) diff --git a/internal/experimental/uploader/chart_uploader.go b/internal/experimental/uploader/chart_uploader.go index 9ce46fab7..87a9d5772 100644 --- a/internal/experimental/uploader/chart_uploader.go +++ b/internal/experimental/uploader/chart_uploader.go @@ -16,6 +16,7 @@ limitations under the License. package uploader import ( + "fmt" "io" "net/url" @@ -45,7 +46,7 @@ func (c *ChartUploader) UploadTo(ref, remote string) error { } if u.Scheme == "" { - return errors.New("scheme prefix missing from remote (e.g. \"oci://\")") + return errors.New(fmt.Sprintf("scheme prefix missing from remote (e.g. \"%s://\")", registry.OCIScheme)) } p, err := c.Pushers.ByScheme(u.Scheme) diff --git a/internal/resolver/resolver.go b/internal/resolver/resolver.go index de0634093..0fcbe5e31 100644 --- a/internal/resolver/resolver.go +++ b/internal/resolver/resolver.go @@ -26,6 +26,7 @@ import ( "github.com/Masterminds/semver/v3" "github.com/pkg/errors" + "helm.sh/helm/v3/internal/experimental/registry" "helm.sh/helm/v3/pkg/chart" "helm.sh/helm/v3/pkg/chart/loader" "helm.sh/helm/v3/pkg/gates" @@ -112,7 +113,7 @@ func (r *Resolver) Resolve(reqs []*chart.Dependency, repoNames map[string]string var version string var ok bool found := true - if !strings.HasPrefix(d.Repository, "oci://") { + if !registry.IsOCI(d.Repository) { repoIndex, err := repo.LoadIndexFile(filepath.Join(r.cachepath, helmpath.CacheIndexFile(repoName))) if err != nil { return nil, errors.Wrapf(err, "no cached repository for %s found. (try 'helm repo update')", repoName) diff --git a/pkg/action/install.go b/pkg/action/install.go index 00ddf8b21..10f69d12c 100644 --- a/pkg/action/install.go +++ b/pkg/action/install.go @@ -36,6 +36,7 @@ import ( "k8s.io/cli-runtime/pkg/resource" "sigs.k8s.io/yaml" + "helm.sh/helm/v3/internal/experimental/registry" "helm.sh/helm/v3/pkg/chart" "helm.sh/helm/v3/pkg/chartutil" "helm.sh/helm/v3/pkg/cli" @@ -664,7 +665,7 @@ func (c *ChartPathOptions) LocateChart(name string, settings *cli.EnvSettings) ( RepositoryCache: settings.RepositoryCache, } - if strings.HasPrefix(name, "oci://") { + if registry.IsOCI(name) { if version == "" { return "", errors.New("version is explicitly required for OCI registries") } diff --git a/pkg/action/pull.go b/pkg/action/pull.go index fa1247054..2f5127ea9 100644 --- a/pkg/action/pull.go +++ b/pkg/action/pull.go @@ -25,6 +25,7 @@ import ( "github.com/pkg/errors" + "helm.sh/helm/v3/internal/experimental/registry" "helm.sh/helm/v3/pkg/chartutil" "helm.sh/helm/v3/pkg/cli" "helm.sh/helm/v3/pkg/downloader" @@ -90,7 +91,7 @@ func (p *Pull) Run(chartRef string) (string, error) { RepositoryCache: p.Settings.RepositoryCache, } - if strings.HasPrefix(chartRef, "oci://") { + if registry.IsOCI(chartRef) { if p.Version == "" { return out.String(), errors.Errorf("--version flag is explicitly required for OCI registries") } diff --git a/pkg/downloader/chart_downloader.go b/pkg/downloader/chart_downloader.go index 2c0d55a55..dba3452c3 100644 --- a/pkg/downloader/chart_downloader.go +++ b/pkg/downloader/chart_downloader.go @@ -102,7 +102,7 @@ func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *proven } name := filepath.Base(u.Path) - if u.Scheme == "oci" { + if u.Scheme == registry.OCIScheme { name = fmt.Sprintf("%s-%s.tgz", name, version) } diff --git a/pkg/downloader/manager.go b/pkg/downloader/manager.go index b8c8a78bb..b01f046f0 100644 --- a/pkg/downloader/manager.go +++ b/pkg/downloader/manager.go @@ -342,7 +342,7 @@ func (m *Manager) downloadAll(deps []*chart.Dependency) error { } version := "" - if strings.HasPrefix(churl, "oci://") { + if registry.IsOCI(churl) { if !resolver.FeatureGateOCI.IsEnabled() { return errors.Wrapf(resolver.FeatureGateOCI.Error(), "the repository %s is an OCI registry", churl) @@ -402,7 +402,7 @@ func (m *Manager) downloadAll(deps []*chart.Dependency) error { } func parseOCIRef(chartRef string) (string, string, error) { - refTagRegexp := regexp.MustCompile(`^(oci://[^:]+(:[0-9]{1,5})?[^:]+):(.*)$`) + refTagRegexp := regexp.MustCompile(fmt.Sprintf(`^(%s://[^:]+(:[0-9]{1,5})?[^:]+):(.*)$`, registry.OCIScheme)) caps := refTagRegexp.FindStringSubmatch(chartRef) if len(caps) != 4 { return "", "", errors.Errorf("improperly formatted oci chart reference: %s", chartRef) @@ -460,7 +460,7 @@ func (m *Manager) hasAllRepos(deps []*chart.Dependency) error { Loop: for _, dd := range deps { // If repo is from local path or OCI, continue - if strings.HasPrefix(dd.Repository, "file://") || strings.HasPrefix(dd.Repository, "oci://") { + if strings.HasPrefix(dd.Repository, "file://") || registry.IsOCI(dd.Repository) { continue } @@ -498,7 +498,7 @@ func (m *Manager) ensureMissingRepos(repoNames map[string]string, deps []*chart. } // OCI-based dependencies do not have a local cache, so skip them - if strings.HasPrefix(dd.Repository, "oci://") { + if registry.IsOCI(dd.Repository) { continue } @@ -567,7 +567,7 @@ func (m *Manager) resolveRepoNames(deps []*chart.Dependency) (map[string]string, for _, dd := range deps { // Don't map the repository, we don't need to download chart from charts directory // When OCI is used there is no Helm repository - if dd.Repository == "" || strings.HasPrefix(dd.Repository, "oci://") { + if dd.Repository == "" || registry.IsOCI(dd.Repository) { continue } // if dep chart is from local path, verify the path is valid @@ -583,7 +583,7 @@ func (m *Manager) resolveRepoNames(deps []*chart.Dependency) (map[string]string, continue } - if strings.HasPrefix(dd.Repository, "oci://") { + if registry.IsOCI(dd.Repository) { reposMap[dd.Name] = dd.Repository continue } @@ -697,7 +697,7 @@ func (m *Manager) parallelRepoUpdate(repos []*repo.Entry) error { // // If it finds a URL that is "relative", it will prepend the repoURL. func (m *Manager) findChartURL(name, version, repoURL string, repos map[string]*repo.ChartRepository) (url, username, password string, insecureskiptlsverify, passcredentialsall bool, caFile, certFile, keyFile string, err error) { - if strings.HasPrefix(repoURL, "oci://") { + if registry.IsOCI(repoURL) { return fmt.Sprintf("%s/%s:%s", repoURL, name, version), "", "", false, false, "", "", "", nil } diff --git a/pkg/getter/getter.go b/pkg/getter/getter.go index 78add728a..3a0567a87 100644 --- a/pkg/getter/getter.go +++ b/pkg/getter/getter.go @@ -169,7 +169,7 @@ var httpProvider = Provider{ } var ociProvider = Provider{ - Schemes: []string{"oci"}, + Schemes: []string{registry.OCIScheme}, New: NewOCIGetter, } diff --git a/pkg/getter/ocigetter.go b/pkg/getter/ocigetter.go index 025d44952..45c92749c 100644 --- a/pkg/getter/ocigetter.go +++ b/pkg/getter/ocigetter.go @@ -39,7 +39,7 @@ func (g *OCIGetter) Get(href string, options ...Option) (*bytes.Buffer, error) { func (g *OCIGetter) get(href string) (*bytes.Buffer, error) { client := g.opts.registryClient - ref := strings.TrimPrefix(href, "oci://") + ref := strings.TrimPrefix(href, fmt.Sprintf("%s://", registry.OCIScheme)) var pullOpts []registry.PullOption requestingProv := strings.HasSuffix(ref, ".prov")