Skip to content

Commit

Permalink
Merge pull request #57 from BenTheElder/library
Browse files Browse the repository at this point in the history
fix ParseImageName
  • Loading branch information
k8s-ci-robot authored Mar 23, 2023
2 parents 221f5a3 + ee97115 commit b95e737
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
18 changes: 15 additions & 3 deletions pkg/community_images/image_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ var (
dockerImageNameRegex = regexp.MustCompile("(?:([^\\/]+)\\/)?(?:([^\\/]+)\\/)?([^@:\\/]+)(?:[@:](.+))")
)

const defaultHost = "index.docker.io"
const defaultHostAlias = "docker.io"

func ParseImageName(imageName string) (string, string, string, error) {
matches := dockerImageNameRegex.FindStringSubmatch(imageName)

Expand All @@ -45,13 +48,22 @@ func ParseImageName(imageName string) (string, string, string, error) {
}
}

// docker defaulting
if hostname == "" {
hostname = "index.docker.io"
hostname = defaultHostAlias
}

if namespace == "" {
if namespace == "" && hasImplicitNamespace(hostname) {
namespace = "library"
}

return hostname, fmt.Sprintf("%s/%s", namespace, image), tag, nil
if namespace != "" {
image = namespace + "/" + image
}

return hostname, image, tag, nil
}

func hasImplicitNamespace(hostname string) bool {
return hostname == defaultHost || hostname == defaultHostAlias
}
10 changes: 5 additions & 5 deletions pkg/community_images/image_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ import (
func TestParseImageName(t *testing.T) {
url, image, tag, err := ParseImageName("redis:4")
require.NoError(t, err)
assert.Equal(t, "index.docker.io", url)
assert.Equal(t, "docker.io", url)
assert.Equal(t, "library/redis", image)
assert.Equal(t, "4", tag)

url, image, tag, err = ParseImageName("k8s.gcr.io/cluster-proportional-autoscaler-amd64:1.1.2-r2")
require.NoError(t, err)
assert.Equal(t, "k8s.gcr.io", url)
assert.Equal(t, "library/cluster-proportional-autoscaler-amd64", image)
assert.Equal(t, "cluster-proportional-autoscaler-amd64", image)
assert.Equal(t, "1.1.2-r2", tag)

url, image, tag, err = ParseImageName("quay.io/coreos/grafana-watcher:v0.0.8")
Expand All @@ -44,19 +44,19 @@ func TestParseImageName(t *testing.T) {

url, image, tag, err = ParseImageName("grafana/grafana:5.0.1")
require.NoError(t, err)
assert.Equal(t, "index.docker.io", url)
assert.Equal(t, "docker.io", url)
assert.Equal(t, "grafana/grafana", image)
assert.Equal(t, "5.0.1", tag)

url, image, tag, err = ParseImageName("postgres:10.0")
require.NoError(t, err)
assert.Equal(t, "index.docker.io", url)
assert.Equal(t, "docker.io", url)
assert.Equal(t, "library/postgres", image)
assert.Equal(t, "10.0", tag)

url, image, tag, err = ParseImageName("localhost:32000/postgres:10.0")
require.NoError(t, err)
assert.Equal(t, "localhost:32000", url)
assert.Equal(t, "library/postgres", image)
assert.Equal(t, "postgres", image)
assert.Equal(t, "10.0", tag)
}

0 comments on commit b95e737

Please sign in to comment.