Skip to content

Commit

Permalink
Support more types of image names (#119)
Browse files Browse the repository at this point in the history
* Support more types of image names

Now works with many cases of different image names including ports in registry or containers in folders.

Verified with the following image formats:
host:port/dir/image:tag
host:port/image:tag
host:port/image
host/image:tag
image
image:tag

fixes #31 #90 

* improve `parseRegistry`

add a check for `.` or `:` in the first part, if it is there, it must be registry URL
if not this can be image tag

Co-authored-by: Taras <9948629+Trane9991@users.noreply.github.com>
  • Loading branch information
jgough and taraspos authored Oct 8, 2020
1 parent 2a06bda commit 9c278ca
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
31 changes: 19 additions & 12 deletions core/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,27 +240,34 @@ func buildFindLocalImageOptions(image string) docker.ListImagesOptions {
}

func buildPullOptions(image string) (docker.PullImageOptions, docker.AuthConfiguration) {
tag := "latest"
registry := ""
repository, tag := docker.ParseRepositoryTag(image)

parts := strings.Split(image, ":")
if len(parts) == 2 {
tag = parts[1]
}
registry := parseRegistry(repository)

name := parts[0]
parts = strings.Split(name, "/")
if len(parts) > 2 {
registry = parts[0]
if tag == "" {
tag = "latest"
}

return docker.PullImageOptions{
Repository: name,
Repository: repository,
Registry: registry,
Tag: tag,
Tag: tag,
}, buildAuthConfiguration(registry)
}

func parseRegistry(repository string) string {
parts := strings.Split(repository, "/")
if len(parts) < 2 {
return ""
}

if strings.ContainsAny(parts[0], ".:") || len(parts) > 2 {
return parts[0]
}

return ""
}

func buildAuthConfiguration(registry string) docker.AuthConfiguration {
var auth docker.AuthConfiguration
if dockercfg == nil {
Expand Down
7 changes: 7 additions & 0 deletions core/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,10 @@ func (*TestLogger) Debugf(format string, args ...interface{}) {}
func (*TestLogger) Errorf(format string, args ...interface{}) {}
func (*TestLogger) Noticef(format string, args ...interface{}) {}
func (*TestLogger) Warningf(format string, args ...interface{}) {}

func (s *SuiteCommon) TestParseRegistry(c *C) {
c.Assert(parseRegistry("example.com:port/dir/image"), Equals, "example.com:port")
c.Assert(parseRegistry("example.com:port/image"), Equals, "example.com:port")
c.Assert(parseRegistry("dir/image"), Equals, "")
c.Assert(parseRegistry("image"), Equals, "")
}

0 comments on commit 9c278ca

Please sign in to comment.