-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Tag implementation to omit default docker registry and namespace,
- New tag implementation is used to determine imageName passed to lifecycle phases Signed-off-by: dwillist <[email protected]>
- Loading branch information
Showing
6 changed files
with
215 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package pack | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/google/go-containerregistry/pkg/name" | ||
) | ||
|
||
// Default Namespace used by docker | ||
const defaultNamespace = "library" | ||
|
||
// Tag stores a docker tag name with the following form: | ||
// <registry>/<namespace>/<name>:<tag> | ||
type Tag struct { | ||
name.Tag | ||
dockerPrefixSpecified bool | ||
} | ||
|
||
// NewTag creates a new for the given tagName. | ||
// This tag will be validated against strictness defined by the opts. | ||
func NewTag(tagName string, opts ...name.Option) (Tag, error) { | ||
minPrefix := fmt.Sprintf("%s/", name.DefaultRegistry) | ||
tag, err := name.NewTag(tagName, opts...) | ||
if err != nil { | ||
return Tag{}, fmt.Errorf("error creating tag: %q", err) | ||
} | ||
return Tag{ | ||
Tag: tag, | ||
dockerPrefixSpecified: strings.HasPrefix(tagName, minPrefix), | ||
}, nil | ||
} | ||
|
||
// Context accesses the Repository context of the reference. | ||
func (pr Tag) Context() name.Repository { | ||
return pr.Tag.Context() | ||
} | ||
|
||
// Identifier accesses the type-specific portion of the reference. | ||
// This corresponds to the <tag> section. | ||
func (pr Tag) Identifier() string { | ||
return pr.Tag.Identifier() | ||
} | ||
|
||
// Name returns the fully-qualified reference name, this is the entire | ||
// <registry>/<namespace>/<name>:<tag> string. | ||
// If <registry> or <namespace> were omitted during the creation of this Tag, they will be missing from | ||
// the string returned by this function. | ||
func (pr Tag) Name() string { | ||
minPrefix := fmt.Sprintf("%s/", name.DefaultRegistry) | ||
maxPrefix := fmt.Sprintf("%s%s/", minPrefix, defaultNamespace) | ||
|
||
result := pr.Tag.Name() | ||
|
||
if pr.Registry.RegistryStr() == name.DefaultRegistry && !pr.dockerPrefixSpecified { | ||
result = strings.TrimPrefix(result, maxPrefix) | ||
result = strings.TrimPrefix(result, minPrefix) | ||
} | ||
return result | ||
} | ||
|
||
// Scope is the scope needed to access this reference. | ||
func (pr Tag) Scope(action string) string { | ||
return pr.Tag.Scope(action) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package pack_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/heroku/color" | ||
"github.com/sclevine/spec" | ||
"github.com/sclevine/spec/report" | ||
|
||
"github.com/buildpacks/pack" | ||
h "github.com/buildpacks/pack/testhelpers" | ||
) | ||
|
||
func TestTag(t *testing.T) { | ||
color.Disable(true) | ||
defer color.Disable(false) | ||
spec.Run(t, "build", testTag, spec.Report(report.Terminal{})) | ||
} | ||
|
||
func testTag(t *testing.T, when spec.G, it spec.S) { | ||
var ( | ||
tag pack.Tag | ||
err error | ||
assert = h.NewAssertionManager(t) | ||
) | ||
when("Tag", func() { | ||
it.Before(func() { | ||
tag, err = pack.NewTag("gcr.io/repo/image-name:tag-name") | ||
assert.Nil(err) | ||
}) | ||
|
||
when("#Identifier", func() { | ||
it("returns specified tag", func() { | ||
assert.Equal(tag.Identifier(), "tag-name") | ||
}) | ||
it("defaults to latest if no tag is specified", func() { | ||
tag, err = pack.NewTag("gcr.io/repo/image-name") | ||
assert.Nil(err) | ||
assert.Equal(tag.Identifier(), "latest") | ||
}) | ||
}) | ||
|
||
when("#Scope", func() { | ||
it("returns scope required to access this reference", func() { | ||
h.AssertEq( | ||
t, | ||
tag.Scope("some-action"), | ||
"repository:repo/image-name:some-action", | ||
) | ||
}) | ||
}) | ||
|
||
when("#Context", func() { | ||
when("registry is omitted", func() { | ||
it.Before(func() { | ||
tag, err = pack.NewTag("image-name:tag-name") | ||
assert.Nil(err) | ||
}) | ||
it("keeps index.docker.io/library prefix", func() { | ||
assert.Equal(tag.Context().String(), "index.docker.io/library/image-name") | ||
}) | ||
}) | ||
when("registry is not omitted", func() { | ||
it("keeps registry context", func() { | ||
assert.Equal(tag.Context().String(), "gcr.io/repo/image-name") | ||
}) | ||
}) | ||
}) | ||
|
||
when("#Name", func() { | ||
when("no registry prefix", func() { | ||
it.Before(func() { | ||
tag, err = pack.NewTag("image-name:tag-name") | ||
assert.Nil(err) | ||
}) | ||
it("omits index.docker.io/library/ prefix", func() { | ||
assert.Equal(tag.Name(), "image-name:tag-name") | ||
}) | ||
}) | ||
|
||
when("registry prefix is provided", func() { | ||
it("keeps specified prefix", func() { | ||
assert.Equal(tag.Name(), "gcr.io/repo/image-name:tag-name") | ||
}) | ||
|
||
when("keeps index.docker.io/*/ prefix is specified", func() { | ||
it("keeps the index.docker.io/library/ prefix", func() { | ||
tag, err = pack.NewTag("index.docker.io/library/image-name:tag-name") | ||
assert.Nil(err) | ||
assert.Equal(tag.Name(), "index.docker.io/library/image-name:tag-name") | ||
}) | ||
|
||
it("keeps the index.docker.io/* prefix", func() { | ||
tag, err = pack.NewTag("index.docker.io/other/image-name:tag-name") | ||
assert.Nil(err) | ||
assert.Equal(tag.Name(), "index.docker.io/other/image-name:tag-name") | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
when("Error cases", func() { | ||
it("passed malformed tagName", func() { | ||
_, err := pack.NewTag((":::")) | ||
assert.ErrorContains(err, "error creating tag: ") | ||
}) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters