diff --git a/pkg/registry/cache.go b/pkg/registry/cache.go index 3fd9c3654..3b0e4f866 100644 --- a/pkg/registry/cache.go +++ b/pkg/registry/cache.go @@ -137,7 +137,7 @@ func (cache *filesystemCache) ChartToLayers(ch *chart.Chart) ([]ocispec.Descript } func (cache *filesystemCache) LoadReference(ref *Reference) ([]ocispec.Descriptor, error) { - tagDir := filepath.Join(cache.rootDir, "refs", escape(ref.Locator), "tags", tagOrDefault(ref.Object)) + tagDir := filepath.Join(cache.rootDir, "refs", escape(ref.Repo), "tags", tagOrDefault(ref.Tag)) // add meta layer metaJSONRaw, err := getSymlinkDestContent(filepath.Join(tagDir, "meta")) @@ -165,8 +165,8 @@ func (cache *filesystemCache) LoadReference(ref *Reference) ([]ocispec.Descripto } func (cache *filesystemCache) StoreReference(ref *Reference, layers []ocispec.Descriptor) (bool, error) { - tag := tagOrDefault(ref.Object) - tagDir := mkdir(filepath.Join(cache.rootDir, "refs", escape(ref.Locator), "tags", tag)) + tag := tagOrDefault(ref.Tag) + tagDir := mkdir(filepath.Join(cache.rootDir, "refs", escape(ref.Repo), "tags", tag)) // Retrieve just the meta and content layers metaLayer, contentLayer, err := extractLayers(layers) @@ -239,7 +239,7 @@ func (cache *filesystemCache) StoreReference(ref *Reference, layers []ocispec.De } func (cache *filesystemCache) DeleteReference(ref *Reference) error { - tagDir := filepath.Join(cache.rootDir, "refs", escape(ref.Locator), "tags", tagOrDefault(ref.Object)) + tagDir := filepath.Join(cache.rootDir, "refs", escape(ref.Repo), "tags", tagOrDefault(ref.Tag)) if _, err := os.Stat(tagDir); os.IsNotExist(err) { return errors.New("ref not found") } @@ -427,10 +427,10 @@ func getRefsSorted(refsRootDir string) ([][]interface{}, error) { tagDir := filepath.Dir(path) // Determine the ref - locator := unescape(strings.TrimLeft( + repo := unescape(strings.TrimLeft( strings.TrimPrefix(filepath.Dir(filepath.Dir(tagDir)), refsRootDir), "/\\")) - object := filepath.Base(tagDir) - ref := fmt.Sprintf("%s:%s", locator, object) + tag := filepath.Base(tagDir) + ref := fmt.Sprintf("%s:%s", repo, tag) // Init hashmap entry if does not exist if _, ok := refsMap[ref]; !ok { diff --git a/pkg/registry/client.go b/pkg/registry/client.go index edba2faca..9c0bdf42f 100644 --- a/pkg/registry/client.go +++ b/pkg/registry/client.go @@ -60,7 +60,7 @@ func NewClient(options *ClientOptions) *Client { // PushChart uploads a chart to a registry func (c *Client) PushChart(ref *Reference) error { c.setDefaultTag(ref) - fmt.Fprintf(c.out, "The push refers to repository [%s]\n", ref.Locator) + fmt.Fprintf(c.out, "The push refers to repository [%s]\n", ref.Repo) layers, err := c.cache.LoadReference(ref) if err != nil { return err @@ -74,14 +74,14 @@ func (c *Client) PushChart(ref *Reference) error { totalSize += layer.Size } fmt.Fprintf(c.out, - "%s: pushed to remote (%d layers, %s total)\n", ref.Object, len(layers), byteCountBinary(totalSize)) + "%s: pushed to remote (%d layers, %s total)\n", ref.Tag, len(layers), byteCountBinary(totalSize)) return nil } // PullChart downloads a chart from a registry func (c *Client) PullChart(ref *Reference) error { c.setDefaultTag(ref) - fmt.Fprintf(c.out, "%s: Pulling from %s\n", ref.Object, ref.Locator) + fmt.Fprintf(c.out, "%s: Pulling from %s\n", ref.Tag, ref.Repo) layers, err := oras.Pull(context.Background(), c.resolver, ref.String(), c.cache.store, KnownMediaTypes()...) if err != nil { return err @@ -91,9 +91,9 @@ func (c *Client) PullChart(ref *Reference) error { return err } if !exists { - fmt.Fprintf(c.out, "Status: Downloaded newer chart for %s:%s\n", ref.Locator, ref.Object) + fmt.Fprintf(c.out, "Status: Downloaded newer chart for %s:%s\n", ref.Repo, ref.Tag) } else { - fmt.Fprintf(c.out, "Status: Chart is up to date for %s:%s\n", ref.Locator, ref.Object) + fmt.Fprintf(c.out, "Status: Chart is up to date for %s:%s\n", ref.Repo, ref.Tag) } return nil } @@ -109,7 +109,7 @@ func (c *Client) SaveChart(ch *chart.Chart, ref *Reference) error { if err != nil { return err } - fmt.Fprintf(c.out, "%s: saved\n", ref.Object) + fmt.Fprintf(c.out, "%s: saved\n", ref.Tag) return nil } @@ -131,7 +131,7 @@ func (c *Client) RemoveChart(ref *Reference) error { if err != nil { return err } - fmt.Fprintf(c.out, "%s: removed\n", ref.Object) + fmt.Fprintf(c.out, "%s: removed\n", ref.Tag) return err } @@ -152,8 +152,8 @@ func (c *Client) PrintChartTable() error { } func (c *Client) setDefaultTag(ref *Reference) { - if ref.Object == "" { - ref.Object = HelmChartDefaultTag + if ref.Tag == "" { + ref.Tag = HelmChartDefaultTag fmt.Fprintf(c.out, "Using default tag: %s\n", HelmChartDefaultTag) } } diff --git a/pkg/registry/reference.go b/pkg/registry/reference.go index 87bf9fffa..cf88d8590 100644 --- a/pkg/registry/reference.go +++ b/pkg/registry/reference.go @@ -33,6 +33,8 @@ type ( // Reference defines the main components of a reference specification Reference struct { *reference.Spec + Tag string + Repo string } ) @@ -44,8 +46,8 @@ func ParseReference(s string) (*Reference, error) { } // convert to our custom type and make necessary mods - ref := Reference{&spec} - ref.fix() + ref := Reference{Spec: &spec} + ref.setExtraFields() // ensure the reference is valid err = ref.validate() @@ -56,33 +58,35 @@ func ParseReference(s string) (*Reference, error) { return &ref, nil } -// fix modifies and augments a ref that may not have been parsed properly -func (ref *Reference) fix() { +// setExtraFields adds the Rpeo and Tag fields to a Reference +func (ref *Reference) setExtraFields() { + ref.Tag = ref.Object + ref.Repo = ref.Locator ref.fixNoTag() - ref.fixNoLocator() + ref.fixNoRepo() } // fixNoTag is a fix for ref strings such as "mychart:1.0.0", which result in missing tag func (ref *Reference) fixNoTag() { - if ref.Object == "" { - parts := strings.Split(ref.Locator, ":") + if ref.Tag == "" { + parts := strings.Split(ref.Repo, ":") numParts := len(parts) if 0 < numParts { lastIndex := numParts - 1 lastPart := parts[lastIndex] if !strings.Contains(lastPart, "/") { - ref.Locator = strings.Join(parts[:lastIndex], ":") - ref.Object = lastPart + ref.Repo = strings.Join(parts[:lastIndex], ":") + ref.Tag = lastPart } } } } -// fixNoLocator is a fix for ref strings such as "mychart", which have the locator swapped with tag -func (ref *Reference) fixNoLocator() { - if ref.Locator == "" { - ref.Locator = ref.Object - ref.Object = "" +// fixNoRepo is a fix for ref strings such as "mychart", which have the repo swapped with tag +func (ref *Reference) fixNoRepo() { + if ref.Repo == "" { + ref.Repo = ref.Tag + ref.Tag = "" } } @@ -94,10 +98,10 @@ func (ref *Reference) validate() error { // validateColons verifies the ref only contains one colon max // (or two, there might be a port number specified i.e. :5000) func (ref *Reference) validateColons() error { - if strings.Contains(ref.Object, ":") { + if strings.Contains(ref.Tag, ":") { return tooManyColonsError } - locParts := strings.Split(ref.Locator, ":") + locParts := strings.Split(ref.Repo, ":") locLastIndex := len(locParts) - 1 if 1 < locLastIndex { return tooManyColonsError diff --git a/pkg/registry/reference_test.go b/pkg/registry/reference_test.go index 6c01fbd5c..e9ec024bc 100644 --- a/pkg/registry/reference_test.go +++ b/pkg/registry/reference_test.go @@ -42,48 +42,48 @@ func TestReference(t *testing.T) { s = "mychart" ref, err := ParseReference(s) is.NoError(err) - is.Equal("mychart", ref.Locator) - is.Equal("", ref.Object) + is.Equal("mychart", ref.Repo) + is.Equal("", ref.Tag) s = "mychart:1.5.0" ref, err = ParseReference(s) is.NoError(err) - is.Equal("mychart", ref.Locator) - is.Equal("1.5.0", ref.Object) + is.Equal("mychart", ref.Repo) + is.Equal("1.5.0", ref.Tag) s = "myrepo/mychart" ref, err = ParseReference(s) is.NoError(err) - is.Equal("myrepo/mychart", ref.Locator) - is.Equal("", ref.Object) + is.Equal("myrepo/mychart", ref.Repo) + is.Equal("", ref.Tag) s = "myrepo/mychart:1.5.0" ref, err = ParseReference(s) is.NoError(err) - is.Equal("myrepo/mychart", ref.Locator) - is.Equal("1.5.0", ref.Object) + is.Equal("myrepo/mychart", ref.Repo) + is.Equal("1.5.0", ref.Tag) s = "mychart:5001:1.5.0" ref, err = ParseReference(s) is.NoError(err) - is.Equal("mychart:5001", ref.Locator) - is.Equal("1.5.0", ref.Object) + is.Equal("mychart:5001", ref.Repo) + is.Equal("1.5.0", ref.Tag) s = "myrepo:5001/mychart:1.5.0" ref, err = ParseReference(s) is.NoError(err) - is.Equal("myrepo:5001/mychart", ref.Locator) - is.Equal("1.5.0", ref.Object) + is.Equal("myrepo:5001/mychart", ref.Repo) + is.Equal("1.5.0", ref.Tag) s = "localhost:5000/mychart:latest" ref, err = ParseReference(s) is.NoError(err) - is.Equal("localhost:5000/mychart", ref.Locator) - is.Equal("latest", ref.Object) + is.Equal("localhost:5000/mychart", ref.Repo) + is.Equal("latest", ref.Tag) s = "my.host.com/my/nested/repo:1.2.3" ref, err = ParseReference(s) is.NoError(err) - is.Equal("my.host.com/my/nested/repo", ref.Locator) - is.Equal("1.2.3", ref.Object) + is.Equal("my.host.com/my/nested/repo", ref.Repo) + is.Equal("1.2.3", ref.Tag) }