Skip to content

Commit

Permalink
Move OCI tag semver range logic from OCIGetter to ChartDownloader
Browse files Browse the repository at this point in the history
Signed-off-by: Scott Rigby <scott@r6by.com>
  • Loading branch information
scottrigby committed Jan 12, 2022
1 parent c7b2a9d commit 4d50526
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 30 deletions.
5 changes: 0 additions & 5 deletions pkg/action/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@ func (p *Pull) Run(chartRef string) (string, error) {
if registry.IsOCI(chartRef) {
c.Options = append(c.Options,
getter.WithRegistryClient(p.cfg.RegistryClient))

if p.Version != "" {
c.Options = append(c.Options,
getter.WithTagName(p.Version))
}
}

if p.Verify {
Expand Down
35 changes: 34 additions & 1 deletion pkg/downloader/chart_downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,41 @@ func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *proven
return destfile, ver, nil
}

func (c *ChartDownloader) getOciUri(ref, version string, u *url.URL) (*url.URL, error) {
// Retrieve list of repository tags
tags, err := c.RegistryClient.Tags(ref)
if err != nil {
return nil, err
}
if len(tags) == 0 {
return nil, errors.Errorf("Unable to locate any tags in provided repository: %s", ref)
}

// Determine if version provided
// If empty, try to get the highest available tag
// If exact version, try to find it
// If semver constraint string, try to find a match
providedVersion := version

tag, err := registry.GetTagMatchingVersionOrConstraint(tags, providedVersion)
if err != nil {
return nil, err
}

// TODO Find a net/url equivalent of this
//ref = fmt.Sprintf("%s:%s", ref, tag)
u.Path = fmt.Sprintf("%s:%s", u.Path, tag)

return u, err
}

// ResolveChartVersion resolves a chart reference to a URL.
//
// It returns the URL and sets the ChartDownloader's Options that can fetch
// the URL using the appropriate Getter.
//
// A reference may be an HTTP URL, a 'reponame/chartname' reference, or a local path.
// A reference may be an HTTP URL, an oci reference URL, a 'reponame/chartname'
// reference, or a local path.
//
// A version is a SemVer string (1.2.3-beta.1+f334a6789).
//
Expand All @@ -159,6 +188,10 @@ func (c *ChartDownloader) ResolveChartVersion(ref, version string) (*url.URL, er
return nil, errors.Errorf("invalid chart URL format: %s", ref)
}

if registry.IsOCI(u.Path) {
return c.getOciUri(ref, version, u)
}

rf, err := loadRepoConfig(c.RepositoryConfig)
if err != nil {
return u, err
Expand Down
24 changes: 0 additions & 24 deletions pkg/getter/ocigetter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"fmt"
"strings"

"github.com/pkg/errors"

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

Expand Down Expand Up @@ -52,28 +50,6 @@ func (g *OCIGetter) get(href string) (*bytes.Buffer, error) {
registry.PullOptWithProv(true))
}

// Retrieve list of repository tags
tags, err := client.Tags(ref)
if err != nil {
return nil, err
}
if len(tags) == 0 {
return nil, errors.Errorf("Unable to locate any tags in provided repository: %s", ref)
}

// Determine if version provided
// If empty, try to get the highest available tag
// If exact version, try to find it
// If semver constraint string, try to find a match
providedVersion := g.opts.version

tag, err := registry.GetTagMatchingVersionOrConstraint(tags, providedVersion)
if err != nil {
return nil, err
}

ref = fmt.Sprintf("%s:%s", ref, tag)

result, err := client.Pull(ref, pullOpts...)
if err != nil {
return nil, err
Expand Down

0 comments on commit 4d50526

Please sign in to comment.