Skip to content

Commit a89797e

Browse files
authored
fix: containerd localhost and 127.0.0.1 registry hosts (#334)
Signed-off-by: Chris Plock <[email protected]>
1 parent e394cf5 commit a89797e

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)
@@ -511,13 +511,21 @@ func withMetadata(platform *platforms.Platform, ref string) (metadata []image.Ad
511511
return metadata
512512
}
513513

514-
// if image doesn't have host set, add docker hub by default
515-
func checkRegistryHostMissing(imageName string) string {
514+
// if imageName doesn't have an identifiable hostname prefix set,
515+
// add docker hub by default
516+
func ensureRegistryHostPrefix(imageName string) string {
516517
parts := strings.Split(imageName, "/")
517518
if len(parts) == 1 {
518519
return fmt.Sprintf("docker.io/library/%s", imageName)
519-
} else if len(parts) > 1 && !strings.Contains(parts[0], ".") {
520-
return fmt.Sprintf("docker.io/%s", imageName)
521520
}
522-
return imageName
521+
if isRegistryHostname(parts[0]) {
522+
return imageName
523+
}
524+
return fmt.Sprintf("docker.io/%s", imageName)
525+
}
526+
527+
// isRegistryHostname returns true if the string passed in can be interpreted
528+
// as a container registry hostname
529+
func isRegistryHostname(s string) bool {
530+
return s == "localhost" || strings.Contains(s, ".") || strings.Contains(s, ":")
523531
}

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)