From df98e18eb73cd08feee1c9e7ddf0e629ad510ee7 Mon Sep 17 00:00:00 2001 From: Andrew Block Date: Sun, 9 Jan 2022 18:47:31 -0600 Subject: [PATCH] Working oci code without providing versions Signed-off-by: Andrew Block --- internal/experimental/registry/client.go | 9 +++++---- internal/experimental/registry/util.go | 10 ++++++++++ internal/resolver/resolver.go | 4 ++-- pkg/action/install.go | 5 ++--- pkg/getter/ocigetter.go | 25 ++++++++++++++++++++++-- 5 files changed, 42 insertions(+), 11 deletions(-) diff --git a/internal/experimental/registry/client.go b/internal/experimental/registry/client.go index a9badb9d5..b3bc49612 100644 --- a/internal/experimental/registry/client.go +++ b/internal/experimental/registry/client.go @@ -567,20 +567,21 @@ func (c *Client) Tags(ref string) ([]string, error) { Client: c.registryAuthorizer, } - registrtyTags, err := registry.Tags(ctx(c.out, c.debug), &repository) + registryTags, err := registry.Tags(ctx(c.out, c.debug), &repository) if err != nil { return nil, err } var tagVersions []*semver.Version - for _, tag := range registrtyTags { + for _, tag := range registryTags { tagVersion, err := semver.StrictNewVersion(tag) - if err != nil { + if err == nil { tagVersions = append(tagVersions, tagVersion) } } - sort.Sort(semver.Collection(tagVersions)) + // Sort the collection + sort.Sort(sort.Reverse(semver.Collection(tagVersions))) tags := make([]string, len(tagVersions)) diff --git a/internal/experimental/registry/util.go b/internal/experimental/registry/util.go index c421e5639..909a803a3 100644 --- a/internal/experimental/registry/util.go +++ b/internal/experimental/registry/util.go @@ -36,6 +36,16 @@ func IsOCI(url string) bool { return strings.HasPrefix(url, fmt.Sprintf("%s://", OCIScheme)) } +// ContainsTag determines whether a tag is found in a provided list of tags +func ContainsTag(tags []string, tag string) bool { + for _, t := range tags { + if tag == t { + return true + } + } + return false +} + // 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/resolver/resolver.go b/internal/resolver/resolver.go index 27ca9929d..36a4803e1 100644 --- a/internal/resolver/resolver.go +++ b/internal/resolver/resolver.go @@ -144,10 +144,10 @@ func (r *Resolver) Resolve(reqs []*chart.Dependency, repoNames map[string]string "repository %s is an OCI registry", d.Repository) } - // Retrive list of tags for repository + // Retrieve list of tags for repository tags, err := r.registryClient.Tags(d.Repository) if err != nil { - return nil, errors.Wrapf(err, "could not retrieve list of tags for repository", d.Repository) + return nil, errors.Wrapf(err, "could not retrieve list of tags for repository %s", d.Repository) } vs = make(repo.ChartVersions, len(tags)) diff --git a/pkg/action/install.go b/pkg/action/install.go index 922f728b0..250dfae95 100644 --- a/pkg/action/install.go +++ b/pkg/action/install.go @@ -695,10 +695,9 @@ func (c *ChartPathOptions) LocateChart(name string, settings *cli.EnvSettings) ( } if registry.IsOCI(name) { - if version == "" { - return "", errors.New("version is explicitly required for OCI registries") + if version != "" { + dl.Options = append(dl.Options, getter.WithTagName(version)) } - dl.Options = append(dl.Options, getter.WithTagName(version)) } if c.Verify { diff --git a/pkg/getter/ocigetter.go b/pkg/getter/ocigetter.go index 45c92749c..fbfcf7133 100644 --- a/pkg/getter/ocigetter.go +++ b/pkg/getter/ocigetter.go @@ -20,6 +20,8 @@ import ( "fmt" "strings" + "github.com/pkg/errors" + "helm.sh/helm/v3/internal/experimental/registry" ) @@ -50,10 +52,29 @@ func (g *OCIGetter) get(href string) (*bytes.Buffer, error) { registry.PullOptWithProv(true)) } - if version := g.opts.version; version != "" { - ref = fmt.Sprintf("%s:%s", ref, version) + // Retrieve list of repository tags + tags, err := client.Tags(ref) + if err != nil { + return nil, err + } + + //Determine if version provided. If not + providedVersion := g.opts.version + if g.opts.version == "" { + if len(tags) > 0 { + providedVersion = tags[0] + } else { + return nil, errors.Errorf("Unable to locate any tags in provided repository: %s", ref) + } + + } else { + if !registry.ContainsTag(tags, providedVersion) { + return nil, errors.Errorf("Could not located provided version %s in repository %s", providedVersion, ref) + } } + ref = fmt.Sprintf("%s:%s", ref, providedVersion) + result, err := client.Pull(ref, pullOpts...) if err != nil { return nil, err