Skip to content

Commit

Permalink
fix parse image name (#8)
Browse files Browse the repository at this point in the history
Signed-off-by: soulseen <[email protected]>
  • Loading branch information
Zhuxiaoyang authored and ks-ci-bot committed Aug 16, 2019
1 parent 87fc295 commit 5124280
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 40 deletions.
92 changes: 54 additions & 38 deletions pkg/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,15 @@ type InstallResult struct {
// DockerNetworkMode specifies the network mode setting for the docker container
type DockerNetworkMode string

// Image holds information about an image.
type ImageInfo struct {
Domain string
Path string
Tag string
Digest digest.Digest
named reference.Named
}

const (
// DockerNetworkModeHost places the container in the default (host) network namespace.
DockerNetworkModeHost DockerNetworkMode = "host"
Expand Down Expand Up @@ -694,55 +703,62 @@ func (l *VolumeList) AsBinds() []string {
}

func Parse(originalName, serverAddress string) (ref string, err error) {
matches := reference.ReferenceRegexp.FindStringSubmatch(originalName)
if matches == nil {
if originalName == "" {
return "", reference.ErrNameEmpty
}
if reference.ReferenceRegexp.FindStringSubmatch(strings.ToLower(originalName)) != nil {
return "", reference.ErrNameContainsUppercase
}
return "", reference.ErrReferenceInvalidFormat

image, err := parseImage(originalName)
if err != nil {
return "", fmt.Errorf("parsing image %q failed: %v", originalName, err)
}

if len(matches[1]) > reference.NameTotalLengthMax {
return "", reference.ErrNameTooLong
if image.Domain != serverAddress && serverAddress != "" {
ref = serverAddress + "/" + image.Path + ":" + image.Tag
} else {
ref = image.String()
}

imageName := getImageName(matches[1], serverAddress)
return ref, err
}

if matches[3] != "" {
var err error
digest, err := digest.Parse(matches[3])
if err != nil {
return "", err
}
fullImageName := strings.Join([]string{imageName, digest.String()}, "@")
return fullImageName, nil
// ParseImage returns an Image struct with all the values filled in for a given image.
// example : localhost:5000/nginx:latest, nginx:perl etc.
func parseImage(image string) (*ImageInfo, error) {
// Parse the image name and tag.
named, err := reference.ParseNormalizedNamed(image)

if err != nil {
return nil, fmt.Errorf("parsing image %q failed: %v", image, err)
}
// Add the latest lag if they did not provide one.
named = reference.TagNameOnly(named)

if matches[2] == "" {
matches[2] = DefaultTag
i := &ImageInfo{
named: named,
Domain: reference.Domain(named),
Path: reference.Path(named),
}

fullImageName := strings.Join([]string{imageName, matches[2]}, ":")
return fullImageName, nil
// Add the tag if there was one.
if tagged, ok := named.(reference.Tagged); ok {
i.Tag = tagged.Tag()
}

// Add the digest if there was one.
if canonical, ok := named.(reference.Canonical); ok {
i.Digest = canonical.Digest()
}

return i, nil
}

func getImageName(name, serverAddress string) (imageName string) {
count := strings.Count(name, "/")
if count == 1 {
if serverAddress == "" {
serverAddress = DefaultHub
}
imageName = strings.Join([]string{serverAddress, name}, "/")
// String returns the string representation of an image.
func (i *ImageInfo) String() string {
return i.named.String()
}

} else {
parts := strings.Split(name, "/")
if serverAddress == "" {
serverAddress = parts[0]
}
imageName = strings.Join([]string{serverAddress, parts[1], parts[2]}, "/")
// Reference returns either the digest if it is non-empty or the tag for the image.
func (i *ImageInfo) Reference() string {
if len(i.Digest.String()) > 1 {
return i.Digest.String()
}
return imageName

return i.Tag
}
7 changes: 5 additions & 2 deletions pkg/api/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,14 @@ func TestGetFullImageName(t *testing.T) {
{"test/image", "test-harbor.io", "test-harbor.io/test/image:latest"},
{"test/image:latest", "test-harbor.io", "test-harbor.io/test/image:latest"},
{"test-harbor.io/test/image:tag", "test-harbor.io", "test-harbor.io/test/image:tag"},
{"repository/test/image", "test-harbor.io", "test-harbor.io/test/image:latest"},
{"repository/test/image:latest", "", "repository/test/image:latest"},
{"test/image:tag", "", "docker.io/test/image:tag"},
{"test/image", "test-harbor.io:3333", "test-harbor.io:3333/test/image:latest"},
{"test-harbor.io:3333/test/image", "", "test-harbor.io:3333/test/image:latest"},
{"nginx", "docker.io", "docker.io/library/nginx:latest"},
{"nginx:perl", "", "docker.io/library/nginx:perl"},
{"127.0.0.1:5000/test:aaa", "127.0.0.1:5001", "127.0.0.1:5001/test:aaa"},
{"127.0.0.1:5000/test:aaa", "", "127.0.0.1:5000/test:aaa"},
{"nginx", "", "docker.io/library/nginx:latest"},
}

for _, tc := range tests {
Expand Down

0 comments on commit 5124280

Please sign in to comment.