Drop image.DecomposeString, make image.Parts private imageParts again

Now that DecomposeString has no users, make the type private again.
Any new users of it should come with a rationale - and new users
of the "none"/"latest" handling of untagged/digested names that is
currently implemented should have an exceptionaly unusual rationale.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
This commit is contained in:
Miloslav Trmač
2019-01-14 03:58:35 +01:00
parent a6e668fac5
commit 6486e2c41b
5 changed files with 26 additions and 32 deletions

View File

@ -25,7 +25,7 @@ import (
"github.com/containers/libpod/pkg/util" "github.com/containers/libpod/pkg/util"
"github.com/containers/storage" "github.com/containers/storage"
"github.com/containers/storage/pkg/reexec" "github.com/containers/storage/pkg/reexec"
"github.com/opencontainers/go-digest" digest "github.com/opencontainers/go-digest"
ociv1 "github.com/opencontainers/image-spec/specs-go/v1" ociv1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@ -460,7 +460,7 @@ func normalizeTag(tag string) (string, error) {
} }
// If the input does not have a tag, we need to add one (latest) // If the input does not have a tag, we need to add one (latest)
if !decomposedTag.isTagged { if !decomposedTag.isTagged {
tag = fmt.Sprintf("%s:%s", tag, decomposedTag.Tag) tag = fmt.Sprintf("%s:%s", tag, decomposedTag.tag)
} }
// If the input doesn't specify a registry, set the registry to localhost // If the input doesn't specify a registry, set the registry to localhost
if !decomposedTag.hasRegistry { if !decomposedTag.hasRegistry {
@ -937,7 +937,7 @@ func (i *Image) MatchRepoTag(input string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
if dcRepoName.Registry == dcImage.Registry && dcImage.Registry != "" { if dcRepoName.registry == dcImage.registry && dcImage.registry != "" {
count++ count++
} }
if dcRepoName.name == dcImage.name && dcImage.name != "" { if dcRepoName.name == dcImage.name && dcImage.name != "" {
@ -945,7 +945,7 @@ func (i *Image) MatchRepoTag(input string) (string, error) {
} else if splitString(dcRepoName.name) == splitString(dcImage.name) { } else if splitString(dcRepoName.name) == splitString(dcImage.name) {
count++ count++
} }
if dcRepoName.Tag == dcImage.Tag { if dcRepoName.tag == dcImage.tag {
count++ count++
} }
results[count] = append(results[count], repoName) results[count] = append(results[count], repoName)

View File

@ -7,12 +7,12 @@ import (
"github.com/containers/image/docker/reference" "github.com/containers/image/docker/reference"
) )
// Parts describes the parts of an image's name // imageParts describes the parts of an image's name
type Parts struct { type imageParts struct {
transport string transport string
Registry string registry string
name string name string
Tag string tag string
isTagged bool isTagged bool
hasRegistry bool hasRegistry bool
} }
@ -34,16 +34,10 @@ func GetImageBaseName(input string) (string, error) {
return splitImageName[len(splitImageName)-1], nil return splitImageName[len(splitImageName)-1], nil
} }
// DecomposeString decomposes a string name into imageParts description. This
// is a wrapper for decompose
func DecomposeString(input string) (Parts, error) {
return decompose(input)
}
// decompose breaks an input name into an imageParts description // decompose breaks an input name into an imageParts description
func decompose(input string) (Parts, error) { func decompose(input string) (imageParts, error) {
var ( var (
parts Parts parts imageParts
hasRegistry bool hasRegistry bool
tag string tag string
) )
@ -62,7 +56,7 @@ func decompose(input string) (Parts, error) {
} }
registry := reference.Domain(imgRef.(reference.Named)) registry := reference.Domain(imgRef.(reference.Named))
imageName := reference.Path(imgRef.(reference.Named)) imageName := reference.Path(imgRef.(reference.Named))
// Is this a Registry or a repo? // Is this a registry or a repo?
if isRegistry(registry) { if isRegistry(registry) {
hasRegistry = true hasRegistry = true
} else { } else {
@ -71,27 +65,27 @@ func decompose(input string) (Parts, error) {
registry = "" registry = ""
} }
} }
return Parts{ return imageParts{
Registry: registry, registry: registry,
hasRegistry: hasRegistry, hasRegistry: hasRegistry,
name: imageName, name: imageName,
Tag: tag, tag: tag,
isTagged: isTagged, isTagged: isTagged,
transport: DefaultTransport, transport: DefaultTransport,
}, nil }, nil
} }
// assemble concatenates an image's parts into a string // assemble concatenates an image's parts into a string
func (ip *Parts) assemble() string { func (ip *imageParts) assemble() string {
spec := fmt.Sprintf("%s:%s", ip.name, ip.Tag) spec := fmt.Sprintf("%s:%s", ip.name, ip.tag)
if ip.Registry != "" { if ip.registry != "" {
spec = fmt.Sprintf("%s/%s", ip.Registry, spec) spec = fmt.Sprintf("%s/%s", ip.registry, spec)
} }
return spec return spec
} }
// assemble concatenates an image's parts with transport into a string // assemble concatenates an image's parts with transport into a string
func (ip *Parts) assembleWithTransport() string { func (ip *imageParts) assembleWithTransport() string {
return fmt.Sprintf("%s%s", ip.transport, ip.assemble()) return fmt.Sprintf("%s%s", ip.transport, ip.assemble())
} }

View File

@ -55,9 +55,9 @@ func TestDecompose(t *testing.T) {
} else { } else {
assert.NoError(t, err, c.input) assert.NoError(t, err, c.input)
assert.Equal(t, c.transport, parts.transport, c.input) assert.Equal(t, c.transport, parts.transport, c.input)
assert.Equal(t, c.registry, parts.Registry, c.input) assert.Equal(t, c.registry, parts.registry, c.input)
assert.Equal(t, c.name, parts.name, c.input) assert.Equal(t, c.name, parts.name, c.input)
assert.Equal(t, c.tag, parts.Tag, c.input) assert.Equal(t, c.tag, parts.tag, c.input)
assert.Equal(t, c.isTagged, parts.isTagged, c.input) assert.Equal(t, c.isTagged, parts.isTagged, c.input)
assert.Equal(t, c.hasRegistry, parts.hasRegistry, c.input) assert.Equal(t, c.hasRegistry, parts.hasRegistry, c.input)
assert.Equal(t, c.assembled, parts.assemble(), c.input) assert.Equal(t, c.assembled, parts.assemble(), c.input)

View File

@ -76,7 +76,7 @@ func (ir *Runtime) getPullRefPair(srcRef types.ImageReference, destName string)
decomposedDest, err := decompose(destName) decomposedDest, err := decompose(destName)
if err == nil && !decomposedDest.hasRegistry { if err == nil && !decomposedDest.hasRegistry {
// If the image doesn't have a registry, set it as the default repo // If the image doesn't have a registry, set it as the default repo
decomposedDest.Registry = DefaultLocalRegistry decomposedDest.registry = DefaultLocalRegistry
decomposedDest.hasRegistry = true decomposedDest.hasRegistry = true
destName = decomposedDest.assemble() destName = decomposedDest.assemble()
} }
@ -317,7 +317,7 @@ func (ir *Runtime) pullGoalFromPossiblyUnqualifiedName(inputName string) (*pullG
} }
var refPairs []pullRefPair var refPairs []pullRefPair
for _, registry := range searchRegistries { for _, registry := range searchRegistries {
decomposedImage.Registry = registry decomposedImage.registry = registry
imageName := decomposedImage.assembleWithTransport() imageName := decomposedImage.assembleWithTransport()
if hasShaInInputName(inputName) { if hasShaInInputName(inputName) {
imageName = fmt.Sprintf("%s%s/%s", decomposedImage.transport, registry, inputName) imageName = fmt.Sprintf("%s%s/%s", decomposedImage.transport, registry, inputName)

View File

@ -16,7 +16,7 @@ import (
// findImageInRepotags takes an imageParts struct and searches images' repotags for // findImageInRepotags takes an imageParts struct and searches images' repotags for
// a match on name:tag // a match on name:tag
func findImageInRepotags(search Parts, images []*Image) (*storage.Image, error) { func findImageInRepotags(search imageParts, images []*Image) (*storage.Image, error) {
var results []*storage.Image var results []*storage.Image
for _, image := range images { for _, image := range images {
for _, name := range image.Names() { for _, name := range image.Names() {
@ -25,12 +25,12 @@ func findImageInRepotags(search Parts, images []*Image) (*storage.Image, error)
if err != nil { if err != nil {
continue continue
} }
if d.name == search.name && d.Tag == search.Tag { if d.name == search.name && d.tag == search.tag {
results = append(results, image.image) results = append(results, image.image)
continue continue
} }
// account for registry:/somedir/image // account for registry:/somedir/image
if strings.HasSuffix(d.name, search.name) && d.Tag == search.Tag { if strings.HasSuffix(d.name, search.name) && d.tag == search.tag {
results = append(results, image.image) results = append(results, image.image)
continue continue
} }