Skip to content

Commit

Permalink
containerd handle localhost and 127.0.0.1 registry hosts
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Plock <[email protected]>
  • Loading branch information
chrisplo committed Dec 18, 2024
1 parent 3984f6f commit f9e5bc6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
20 changes: 14 additions & 6 deletions pkg/image/containerd/daemon_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func (p *daemonImageProvider) fetchPlatformFromConfig(ctx context.Context, clien
}

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

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

// if image doesn't have host set, add docker hub by default
func checkRegistryHostMissing(imageName string) string {
// if imageName doesn't have an identifiable hostname prefix set,
// add docker hub by default
func ensureRegistryHostPrefix(imageName string) string {
parts := strings.Split(imageName, "/")
if len(parts) > 1 && isRegistryHostname(parts[0]) {
return imageName
}
if len(parts) == 1 {
return fmt.Sprintf("docker.io/library/%s", imageName)
} else if len(parts) > 1 && !strings.Contains(parts[0], ".") {
return fmt.Sprintf("docker.io/%s", imageName)
}
return imageName
return fmt.Sprintf("docker.io/%s", imageName)
}

// isRegistryHostname returns true if the string passed in can be interpreted
// as a container registry hostname
func isRegistryHostname(s string) bool {
return s == "localhost" || strings.Contains(s, ".") || strings.Contains(s, ":")
}
20 changes: 18 additions & 2 deletions pkg/image/containerd/daemon_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/anchore/stereoscope/pkg/image"
)

func Test_checkRegistryHostMissing(t *testing.T) {
func Test_ensureRegistryHostPrefix(t *testing.T) {
tests := []struct {
image string
want string
Expand All @@ -32,6 +32,22 @@ func Test_checkRegistryHostMissing(t *testing.T) {
image: "registry.place.io/thing:version",
want: "registry.place.io/thing:version",
},
{
image: "127.0.0.1/thing:version",
want: "127.0.0.1/thing:version",
},
{
image: "127.0.0.1:1234/thing:version",
want: "127.0.0.1:1234/thing:version",
},
{
image: "localhost/thing:version",
want: "localhost/thing:version",
},
{
image: "localhost:1234/thing:version",
want: "localhost:1234/thing:version",
},
{
image: "alpine@sha256:95cf004f559831017cdf4628aaf1bb30133677be8702a8c5f2994629f637a209",
want: "docker.io/library/alpine@sha256:95cf004f559831017cdf4628aaf1bb30133677be8702a8c5f2994629f637a209",
Expand All @@ -43,7 +59,7 @@ func Test_checkRegistryHostMissing(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.image, func(t *testing.T) {
got := checkRegistryHostMissing(tt.image)
got := ensureRegistryHostPrefix(tt.image)
require.NotNil(t, got)
assert.Equal(t, tt.want, got)
})
Expand Down

0 comments on commit f9e5bc6

Please sign in to comment.