Skip to content

Commit

Permalink
remove hardcoded oci strings, add constant/util
Browse files Browse the repository at this point in the history
Signed-off-by: Josh Dolitsky <josh@dolit.ski>
  • Loading branch information
jdolitsky committed Jun 28, 2021
1 parent bfa5036 commit 230e37c
Show file tree
Hide file tree
Showing 14 changed files with 34 additions and 18 deletions.
3 changes: 2 additions & 1 deletion internal/experimental/action/push.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
}

Expand Down
2 changes: 1 addition & 1 deletion internal/experimental/pusher/ocipusher.go
Expand Up @@ -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...)
Expand Down
2 changes: 1 addition & 1 deletion internal/experimental/pusher/pusher.go
Expand Up @@ -83,7 +83,7 @@ func (p Providers) ByScheme(scheme string) (Pusher, error) {
}

var ociProvider = Provider{
Schemes: []string{"oci"},
Schemes: []string{registry.OCIScheme},
New: NewOCIPusher,
}

Expand Down
3 changes: 2 additions & 1 deletion internal/experimental/pusher/pusher_test.go
Expand Up @@ -18,6 +18,7 @@ package pusher
import (
"testing"

"helm.sh/helm/v3/internal/experimental/registry"
"helm.sh/helm/v3/pkg/cli"
)

Expand Down Expand Up @@ -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)
}
}
3 changes: 3 additions & 0 deletions internal/experimental/registry/constants.go
Expand Up @@ -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"

Expand Down
7 changes: 7 additions & 0 deletions internal/experimental/registry/util.go
Expand Up @@ -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"
Expand All @@ -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))
Expand Down
3 changes: 2 additions & 1 deletion internal/experimental/uploader/chart_uploader.go
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package uploader

import (
"fmt"
"io"
"net/url"

Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion internal/resolver/resolver.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pkg/action/install.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/action/pull.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/downloader/chart_downloader.go
Expand Up @@ -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)
}

Expand Down
14 changes: 7 additions & 7 deletions pkg/downloader/manager.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/getter/getter.go
Expand Up @@ -169,7 +169,7 @@ var httpProvider = Provider{
}

var ociProvider = Provider{
Schemes: []string{"oci"},
Schemes: []string{registry.OCIScheme},
New: NewOCIGetter,
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/getter/ocigetter.go
Expand Up @@ -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")
Expand Down

0 comments on commit 230e37c

Please sign in to comment.