Skip to content

Commit f9e5bc6

Browse files
committed
containerd handle localhost and 127.0.0.1 registry hosts
Signed-off-by: Chris Plock <[email protected]>
1 parent 3984f6f commit f9e5bc6

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

pkg/image/containerd/daemon_provider.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ func (p *daemonImageProvider) fetchPlatformFromConfig(ctx context.Context, clien
319319
}
320320

321321
func (p *daemonImageProvider) pullImageIfMissing(ctx context.Context, client *containerd.Client) (string, *platforms.Platform, error) {
322-
p.imageStr = checkRegistryHostMissing(p.imageStr)
322+
p.imageStr = ensureRegistryHostPrefix(p.imageStr)
323323

324324
// try to get the image first before pulling
325325
resolvedImage, resolvedPlatform, err := p.resolveImage(ctx, client, p.imageStr)
@@ -504,13 +504,21 @@ func withMetadata(platform *platforms.Platform, ref string) (metadata []image.Ad
504504
return metadata
505505
}
506506

507-
// if image doesn't have host set, add docker hub by default
508-
func checkRegistryHostMissing(imageName string) string {
507+
// if imageName doesn't have an identifiable hostname prefix set,
508+
// add docker hub by default
509+
func ensureRegistryHostPrefix(imageName string) string {
509510
parts := strings.Split(imageName, "/")
511+
if len(parts) > 1 && isRegistryHostname(parts[0]) {
512+
return imageName
513+
}
510514
if len(parts) == 1 {
511515
return fmt.Sprintf("docker.io/library/%s", imageName)
512-
} else if len(parts) > 1 && !strings.Contains(parts[0], ".") {
513-
return fmt.Sprintf("docker.io/%s", imageName)
514516
}
515-
return imageName
517+
return fmt.Sprintf("docker.io/%s", imageName)
518+
}
519+
520+
// isRegistryHostname returns true if the string passed in can be interpreted
521+
// as a container registry hostname
522+
func isRegistryHostname(s string) bool {
523+
return s == "localhost" || strings.Contains(s, ".") || strings.Contains(s, ":")
516524
}

pkg/image/containerd/daemon_provider_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/anchore/stereoscope/pkg/image"
1212
)
1313

14-
func Test_checkRegistryHostMissing(t *testing.T) {
14+
func Test_ensureRegistryHostPrefix(t *testing.T) {
1515
tests := []struct {
1616
image string
1717
want string
@@ -32,6 +32,22 @@ func Test_checkRegistryHostMissing(t *testing.T) {
3232
image: "registry.place.io/thing:version",
3333
want: "registry.place.io/thing:version",
3434
},
35+
{
36+
image: "127.0.0.1/thing:version",
37+
want: "127.0.0.1/thing:version",
38+
},
39+
{
40+
image: "127.0.0.1:1234/thing:version",
41+
want: "127.0.0.1:1234/thing:version",
42+
},
43+
{
44+
image: "localhost/thing:version",
45+
want: "localhost/thing:version",
46+
},
47+
{
48+
image: "localhost:1234/thing:version",
49+
want: "localhost:1234/thing:version",
50+
},
3551
{
3652
image: "alpine@sha256:95cf004f559831017cdf4628aaf1bb30133677be8702a8c5f2994629f637a209",
3753
want: "docker.io/library/alpine@sha256:95cf004f559831017cdf4628aaf1bb30133677be8702a8c5f2994629f637a209",
@@ -43,7 +59,7 @@ func Test_checkRegistryHostMissing(t *testing.T) {
4359
}
4460
for _, tt := range tests {
4561
t.Run(tt.image, func(t *testing.T) {
46-
got := checkRegistryHostMissing(tt.image)
62+
got := ensureRegistryHostPrefix(tt.image)
4763
require.NotNil(t, got)
4864
assert.Equal(t, tt.want, got)
4965
})

0 commit comments

Comments
 (0)