From 97bfea94cb14432d54c2e8b125ed23a81f806eb8 Mon Sep 17 00:00:00 2001 From: Jonathan Gough Date: Wed, 7 Oct 2020 12:14:35 +0100 Subject: [PATCH 01/11] 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 --- core/common.go | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/core/common.go b/core/common.go index 701b38ace..2e8342185 100644 --- a/core/common.go +++ b/core/common.go @@ -7,7 +7,7 @@ import ( "fmt" "io" "reflect" - "strings" + "regexp" "time" docker "github.com/fsouza/go-dockerclient" @@ -240,25 +240,22 @@ func buildFindLocalImageOptions(image string) docker.ListImagesOptions { } func buildPullOptions(image string) (docker.PullImageOptions, docker.AuthConfiguration) { - tag := "latest" - registry := "" - - parts := strings.Split(image, ":") - if len(parts) == 2 { - tag = parts[1] - } - - name := parts[0] - parts = strings.Split(name, "/") - if len(parts) > 2 { - registry = parts[0] - } - - return docker.PullImageOptions{ - Repository: name, - Registry: registry, - Tag: tag, - }, buildAuthConfiguration(registry) + re := regexp.MustCompile("((?:([^/]+)/)?[^:]+)(?::(.+))?") + matches := re.FindStringSubmatch(image) + + repository := matches[1] + registry := matches[2] + tag := matches[3] + + if tag == "" { + tag = "latest" + } + + return docker.PullImageOptions{ + Repository: repository, + Registry: registry, + Tag: tag, + }, buildAuthConfiguration(registry) } func buildAuthConfiguration(registry string) docker.AuthConfiguration { From b23ffdf4e31b866d4a1587c692c050ab7d5df8b3 Mon Sep 17 00:00:00 2001 From: Jonathan Gough Date: Wed, 7 Oct 2020 15:05:05 +0100 Subject: [PATCH 02/11] Detect registry correctly from the image name Now the host must contain at least one dot. Was failing on images of the form dir/image --- core/common.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/common.go b/core/common.go index 2e8342185..3856e8a2f 100644 --- a/core/common.go +++ b/core/common.go @@ -240,7 +240,7 @@ func buildFindLocalImageOptions(image string) docker.ListImagesOptions { } func buildPullOptions(image string) (docker.PullImageOptions, docker.AuthConfiguration) { - re := regexp.MustCompile("((?:([^/]+)/)?[^:]+)(?::(.+))?") + re := regexp.MustCompile("((?:([^/]+\\.[^/]+)/)?[^:]+)(?::(.+))?") matches := re.FindStringSubmatch(image) repository := matches[1] From 49786577b51459a7684133c55b76e1914b874478 Mon Sep 17 00:00:00 2001 From: Jonathan Gough Date: Wed, 7 Oct 2020 15:06:28 +0100 Subject: [PATCH 03/11] Add tests for buildPullOptions --- core/common_test.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/core/common_test.go b/core/common_test.go index dc0cae559..15f1abfa3 100644 --- a/core/common_test.go +++ b/core/common_test.go @@ -316,3 +316,26 @@ 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) TestBuildPullOptions(c *C) { + var cases = map[string]docker.PullImageOptions{ + "dir/image:tag": docker.PullImageOptions{Repository: "dir/image", Registry: "", Tag: "tag",}, + "dir/image": docker.PullImageOptions{Repository: "dir/image", Registry: "", Tag: "latest",}, + "image:tag": docker.PullImageOptions{Repository: "image", Registry: "", Tag: "tag",}, + "image": docker.PullImageOptions{Repository: "image", Registry: "", Tag: "latest",}, + "example.com/dir/image:tag": docker.PullImageOptions{Repository: "example.com/dir/image", Registry: "example.com", Tag: "tag",}, + "example.com/dir/image": docker.PullImageOptions{Repository: "example.com/dir/image", Registry: "example.com", Tag: "latest",}, + "example.com/image:tag": docker.PullImageOptions{Repository: "example.com/image", Registry: "example.com", Tag: "tag",}, + "example.com/image": docker.PullImageOptions{Repository: "example.com/image", Registry: "example.com", Tag: "latest",}, + "example.com:port/dir/image:tag": docker.PullImageOptions{Repository: "example.com:port/dir/image", Registry: "example.com:port", Tag: "tag",}, + "example.com:port/dir/image": docker.PullImageOptions{Repository: "example.com:port/dir/image", Registry: "example.com:port", Tag: "latest",}, + "example.com:port/image:tag": docker.PullImageOptions{Repository: "example.com:port/image", Registry: "example.com:port", Tag: "tag",}, + "example.com:port/image": docker.PullImageOptions{Repository: "example.com:port/image", Registry: "example.com:port", Tag: "latest",}, + } + + for image, expected := range cases { + o, _ := buildPullOptions(image) + fmt.Println(image) + c.Assert(o, Equals, expected) + } +} From 006e15e055be17de43cb8106b6215d0ea4fb6a27 Mon Sep 17 00:00:00 2001 From: Jonathan Gough Date: Wed, 7 Oct 2020 15:07:56 +0100 Subject: [PATCH 04/11] Add missing import --- core/common_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/core/common_test.go b/core/common_test.go index 15f1abfa3..876cc3505 100644 --- a/core/common_test.go +++ b/core/common_test.go @@ -7,6 +7,7 @@ import ( "time" . "gopkg.in/check.v1" + docker "github.com/fsouza/go-dockerclient" ) func Test(t *testing.T) { TestingT(t) } From cba65306996e961147303e47e7f7e1e4b4ff198a Mon Sep 17 00:00:00 2001 From: Jonathan Gough Date: Wed, 7 Oct 2020 22:25:51 +0100 Subject: [PATCH 05/11] New repository logic using go-dockerclient --- core/common.go | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/core/common.go b/core/common.go index 3856e8a2f..f8ff21959 100644 --- a/core/common.go +++ b/core/common.go @@ -7,7 +7,7 @@ import ( "fmt" "io" "reflect" - "regexp" + "string" "time" docker "github.com/fsouza/go-dockerclient" @@ -240,22 +240,23 @@ func buildFindLocalImageOptions(image string) docker.ListImagesOptions { } func buildPullOptions(image string) (docker.PullImageOptions, docker.AuthConfiguration) { - re := regexp.MustCompile("((?:([^/]+\\.[^/]+)/)?[^:]+)(?::(.+))?") - matches := re.FindStringSubmatch(image) - - repository := matches[1] - registry := matches[2] - tag := matches[3] - - if tag == "" { - tag = "latest" - } - - return docker.PullImageOptions{ - Repository: repository, - Registry: registry, - Tag: tag, - }, buildAuthConfiguration(registry) + repository, tag := docker.ParseRepositoryTag(image) + + registry := "" + + if strings.Contains(repository, ":") { + registry = strings.Split(repository,"/")[0] + } + + if tag == "" { + tag = "latest" + } + + return docker.PullImageOptions{ + Repository: repository, + Registry: registry, + Tag: tag, + }, buildAuthConfiguration(registry) } func buildAuthConfiguration(registry string) docker.AuthConfiguration { From 626040fbedf34d0cca9dcce37b51b471192cde79 Mon Sep 17 00:00:00 2001 From: Jonathan Gough Date: Wed, 7 Oct 2020 22:27:42 +0100 Subject: [PATCH 06/11] Updating tests for registry parsing --- core/common_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/common_test.go b/core/common_test.go index 876cc3505..266392210 100644 --- a/core/common_test.go +++ b/core/common_test.go @@ -324,10 +324,6 @@ func (s *SuiteCommon) TestBuildPullOptions(c *C) { "dir/image": docker.PullImageOptions{Repository: "dir/image", Registry: "", Tag: "latest",}, "image:tag": docker.PullImageOptions{Repository: "image", Registry: "", Tag: "tag",}, "image": docker.PullImageOptions{Repository: "image", Registry: "", Tag: "latest",}, - "example.com/dir/image:tag": docker.PullImageOptions{Repository: "example.com/dir/image", Registry: "example.com", Tag: "tag",}, - "example.com/dir/image": docker.PullImageOptions{Repository: "example.com/dir/image", Registry: "example.com", Tag: "latest",}, - "example.com/image:tag": docker.PullImageOptions{Repository: "example.com/image", Registry: "example.com", Tag: "tag",}, - "example.com/image": docker.PullImageOptions{Repository: "example.com/image", Registry: "example.com", Tag: "latest",}, "example.com:port/dir/image:tag": docker.PullImageOptions{Repository: "example.com:port/dir/image", Registry: "example.com:port", Tag: "tag",}, "example.com:port/dir/image": docker.PullImageOptions{Repository: "example.com:port/dir/image", Registry: "example.com:port", Tag: "latest",}, "example.com:port/image:tag": docker.PullImageOptions{Repository: "example.com:port/image", Registry: "example.com:port", Tag: "tag",}, From 6a69cd720e77226b39c6a7751114b4da3edf1878 Mon Sep 17 00:00:00 2001 From: Jonathan Gough Date: Wed, 7 Oct 2020 22:39:53 +0100 Subject: [PATCH 07/11] Separating registry parsing into other function --- core/common.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/core/common.go b/core/common.go index f8ff21959..0e0229758 100644 --- a/core/common.go +++ b/core/common.go @@ -242,11 +242,7 @@ func buildFindLocalImageOptions(image string) docker.ListImagesOptions { func buildPullOptions(image string) (docker.PullImageOptions, docker.AuthConfiguration) { repository, tag := docker.ParseRepositoryTag(image) - registry := "" - - if strings.Contains(repository, ":") { - registry = strings.Split(repository,"/")[0] - } + registry := parseRegistry(repository) if tag == "" { tag = "latest" @@ -259,6 +255,13 @@ func buildPullOptions(image string) (docker.PullImageOptions, docker.AuthConfigu }, buildAuthConfiguration(registry) } +func parseRegistry(repository string) (string) { + if strings.Contains(repository, ":") { + return strings.Split(repository,"/")[0] + } + return "" +} + func buildAuthConfiguration(registry string) docker.AuthConfiguration { var auth docker.AuthConfiguration if dockercfg == nil { From 3969fbf326851e473d98ffc522c459a820a5981b Mon Sep 17 00:00:00 2001 From: Jonathan Gough Date: Wed, 7 Oct 2020 22:41:08 +0100 Subject: [PATCH 08/11] Tests for new registry parsing code --- core/common_test.go | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/core/common_test.go b/core/common_test.go index 266392210..967dade9e 100644 --- a/core/common_test.go +++ b/core/common_test.go @@ -318,21 +318,9 @@ func (*TestLogger) Errorf(format string, args ...interface{}) {} func (*TestLogger) Noticef(format string, args ...interface{}) {} func (*TestLogger) Warningf(format string, args ...interface{}) {} -func (s *SuiteCommon) TestBuildPullOptions(c *C) { - var cases = map[string]docker.PullImageOptions{ - "dir/image:tag": docker.PullImageOptions{Repository: "dir/image", Registry: "", Tag: "tag",}, - "dir/image": docker.PullImageOptions{Repository: "dir/image", Registry: "", Tag: "latest",}, - "image:tag": docker.PullImageOptions{Repository: "image", Registry: "", Tag: "tag",}, - "image": docker.PullImageOptions{Repository: "image", Registry: "", Tag: "latest",}, - "example.com:port/dir/image:tag": docker.PullImageOptions{Repository: "example.com:port/dir/image", Registry: "example.com:port", Tag: "tag",}, - "example.com:port/dir/image": docker.PullImageOptions{Repository: "example.com:port/dir/image", Registry: "example.com:port", Tag: "latest",}, - "example.com:port/image:tag": docker.PullImageOptions{Repository: "example.com:port/image", Registry: "example.com:port", Tag: "tag",}, - "example.com:port/image": docker.PullImageOptions{Repository: "example.com:port/image", Registry: "example.com:port", Tag: "latest",}, - } - - for image, expected := range cases { - o, _ := buildPullOptions(image) - fmt.Println(image) - c.Assert(o, Equals, expected) - } +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, "") } From b026402914205d42f5865789209478bd97241a6b Mon Sep 17 00:00:00 2001 From: Jonathan Gough Date: Wed, 7 Oct 2020 22:43:49 +0100 Subject: [PATCH 09/11] Typo string->strings --- core/common.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/common.go b/core/common.go index 0e0229758..bff374ce7 100644 --- a/core/common.go +++ b/core/common.go @@ -7,7 +7,7 @@ import ( "fmt" "io" "reflect" - "string" + "strings" "time" docker "github.com/fsouza/go-dockerclient" From be980b4442b8d2f258d3ae663f98ff820303c6aa Mon Sep 17 00:00:00 2001 From: Jonathan Gough Date: Wed, 7 Oct 2020 22:45:13 +0100 Subject: [PATCH 10/11] Removing import no longer needed --- core/common_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/core/common_test.go b/core/common_test.go index 967dade9e..d7430e5bd 100644 --- a/core/common_test.go +++ b/core/common_test.go @@ -7,7 +7,6 @@ import ( "time" . "gopkg.in/check.v1" - docker "github.com/fsouza/go-dockerclient" ) func Test(t *testing.T) { TestingT(t) } From 29aaf4c6c49ecc110410d00be8291cc3fc2c37d3 Mon Sep 17 00:00:00 2001 From: Taras <9948629+Trane9991@users.noreply.github.com> Date: Thu, 8 Oct 2020 20:11:27 +0300 Subject: [PATCH 11/11] 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 --- core/common.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/core/common.go b/core/common.go index bff374ce7..9f4de8783 100644 --- a/core/common.go +++ b/core/common.go @@ -255,10 +255,16 @@ func buildPullOptions(image string) (docker.PullImageOptions, docker.AuthConfigu }, buildAuthConfiguration(registry) } -func parseRegistry(repository string) (string) { - if strings.Contains(repository, ":") { - return strings.Split(repository,"/")[0] +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 "" }