From 5e42d050a7e44af75601b06f9f19b12a07a6809c Mon Sep 17 00:00:00 2001 From: Jameel Al-Aziz Date: Tue, 18 Jun 2024 21:40:37 -0700 Subject: [PATCH 1/2] Improve support for third-party registry images Update `ReferenceExpand` to support image references from remote registries. This fixes local image lookup and pulling with newer versions of Docker. fixes #4045 Signed-off-by: Jameel Al-Aziz --- src/cmd/linuxkit/util/reference.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cmd/linuxkit/util/reference.go b/src/cmd/linuxkit/util/reference.go index 7ed89c349..04139b54a 100644 --- a/src/cmd/linuxkit/util/reference.go +++ b/src/cmd/linuxkit/util/reference.go @@ -32,7 +32,12 @@ func ReferenceExpand(ref string, options ...ReferenceOption) string { case 1: ret = "docker.io/library/" + ref case 2: - ret = "docker.io/" + ref + // If the first part is not a domain, assume it is a DockerHub user/org. + // This logic is copied from moby: + // https://github.com/moby/moby/blob/e7347f8a8c2fd3d2abd34b638d6fc8c18b0278d1/registry/search.go#L148C29-L149C71 + if !strings.Contains(parts[0], ".") && !strings.Contains(parts[0], ":") && parts[0] != "localhost" { + ret = "docker.io/" + ref + } } if opts.withTag && !strings.Contains(ret, ":") { From 21e9f0fa1c1a809ea1a364302ab6a46fcc964c7a Mon Sep 17 00:00:00 2001 From: Jameel Al-Aziz Date: Sat, 22 Jun 2024 01:58:37 -0400 Subject: [PATCH 2/2] add tests Signed-off-by: Jameel Al-Aziz --- src/cmd/linuxkit/util/reference_test.go | 59 +++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/cmd/linuxkit/util/reference_test.go diff --git a/src/cmd/linuxkit/util/reference_test.go b/src/cmd/linuxkit/util/reference_test.go new file mode 100644 index 000000000..b96232e49 --- /dev/null +++ b/src/cmd/linuxkit/util/reference_test.go @@ -0,0 +1,59 @@ +package util + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestReferenceExpand(t *testing.T) { + tests := []struct { + name string + ref string + options []ReferenceOption + want string + }{ + { + "basic image name should expand to docker.io/library image", + "redis", + nil, + "docker.io/library/redis", + }, + { + "image name with user/org should expand to docker.io image", + "foo/bar", + nil, + "docker.io/foo/bar", + }, + { + "custom registry image name should not expand", + "myregistry.io/foo", + nil, + "myregistry.io/foo", + }, + { + "image name with more than three parts should not expand", + "foo/bar/baz", + nil, + "foo/bar/baz", + }, + { + "with tag should add latest if image does not have tag", + "redis", + []ReferenceOption{ReferenceWithTag()}, + "docker.io/library/redis:latest", + }, + { + "with tag should not add latest if image already has tag", + "redis:alpine", + []ReferenceOption{ReferenceWithTag()}, + "docker.io/library/redis:alpine", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := ReferenceExpand(tt.ref, tt.options...) + assert.Equal(t, tt.want, got) + }) + } +}