Skip to content

Commit

Permalink
Namespace not required
Browse files Browse the repository at this point in the history
  • Loading branch information
emosbaugh committed Sep 3, 2020
1 parent 2af55f1 commit fb572c7
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 20 deletions.
17 changes: 12 additions & 5 deletions pkg/image/image.go
Expand Up @@ -23,7 +23,10 @@ func ImageInfoFromFile(registry registry.RegistryOptions, nameParts []string) (k
return image, errors.Errorf("not enough parts in image name: %v", nameParts)
}

newImageNameParts := []string{registry.Endpoint, registry.Namespace}
newImageNameParts := []string{registry.Endpoint}
if registry.Namespace != "" {
newImageNameParts = append(newImageNameParts, registry.Namespace)
}
var originalName, tag, separator string
if nameParts[len(nameParts)-2] == "sha256" {
newImageNameParts = append(newImageNameParts, nameParts[len(nameParts)-3])
Expand All @@ -50,8 +53,10 @@ func DestRef(registry registry.RegistryOptions, srcImage string) string {
imageParts := strings.Split(srcImage, "/")
lastPart := imageParts[len(imageParts)-1]

image := fmt.Sprintf("%s/%s/%s", registry.Endpoint, registry.Namespace, lastPart)
return image
if registry.Namespace == "" {
return fmt.Sprintf("%s/%s", registry.Endpoint, lastPart)
}
return fmt.Sprintf("%s/%s/%s", registry.Endpoint, registry.Namespace, lastPart)
}

// stripImageTag removes the tag or digest from an image
Expand All @@ -76,8 +81,10 @@ func destImageName(registry registry.RegistryOptions, srcImage string) string {
lastPart := imageParts[len(imageParts)-1]
lastPart = stripImageTag(lastPart)

image := fmt.Sprintf("%s/%s/%s", registry.Endpoint, registry.Namespace, lastPart)
return image
if registry.Namespace == "" {
return fmt.Sprintf("%s/%s", registry.Endpoint, lastPart)
}
return fmt.Sprintf("%s/%s/%s", registry.Endpoint, registry.Namespace, lastPart)
}

func buildImageAlts(destRegistry registry.RegistryOptions, image string) ([]kustomizeimage.Image, error) {
Expand Down
81 changes: 66 additions & 15 deletions pkg/image/image_test.go
Expand Up @@ -12,29 +12,33 @@ import (
)

func Test_ImageNameFromNameParts(t *testing.T) {
registryOps := registry.RegistryOptions{
Endpoint: "localhost:5000",
Namespace: "somebigbank",
}

tests := []struct {
name string
parts []string
expected kustomizetypes.Image
isError bool
name string
parts []string
registryOps registry.RegistryOptions
expected kustomizetypes.Image
isError bool
}{
{
name: "bad name format",
parts: []string{"quay.io", "latest"},
name: "bad name format",
parts: []string{"quay.io", "latest"},
registryOps: registry.RegistryOptions{
Endpoint: "localhost:5000",
Namespace: "somebigbank",
},
expected: kustomizetypes.Image{},
isError: true,
},
{
name: "ECR style image",
parts: []string{"411111111111.dkr.ecr.us-west-1.amazonaws.com", "myrepo", "v0.0.1"},
registryOps: registry.RegistryOptions{
Endpoint: "localhost:5000",
Namespace: "somebigbank",
},
expected: kustomizetypes.Image{
Name: "411111111111.dkr.ecr.us-west-1.amazonaws.com/myrepo:v0.0.1",
NewName: fmt.Sprintf("%s/%s/myrepo", registryOps.Endpoint, registryOps.Namespace),
NewName: "localhost:5000/somebigbank/myrepo",
NewTag: "v0.0.1",
Digest: "",
},
Expand All @@ -43,9 +47,13 @@ func Test_ImageNameFromNameParts(t *testing.T) {
{
name: "four parts with tag",
parts: []string{"quay.io", "someorg", "debian", "0.1"},
registryOps: registry.RegistryOptions{
Endpoint: "localhost:5000",
Namespace: "somebigbank",
},
expected: kustomizetypes.Image{
Name: "quay.io/someorg/debian:0.1",
NewName: fmt.Sprintf("%s/%s/debian", registryOps.Endpoint, registryOps.Namespace),
NewName: "localhost:5000/somebigbank/debian",
NewTag: "0.1",
Digest: "",
},
Expand All @@ -54,21 +62,39 @@ func Test_ImageNameFromNameParts(t *testing.T) {
{
name: "five parts with sha",
parts: []string{"quay.io", "someorg", "debian", "sha256", "1234567890abcdef"},
registryOps: registry.RegistryOptions{
Endpoint: "localhost:5000",
Namespace: "somebigbank",
},
expected: kustomizetypes.Image{
Name: "quay.io/someorg/debian@sha256:1234567890abcdef",
NewName: fmt.Sprintf("%s/%s/debian", registryOps.Endpoint, registryOps.Namespace),
NewName: "localhost:5000/somebigbank/debian",
NewTag: "",
Digest: "1234567890abcdef",
},
isError: false,
},
{
name: "no namespace",
parts: []string{"quay.io", "someorg", "debian", "0.1"},
registryOps: registry.RegistryOptions{
Endpoint: "localhost:5000",
},
expected: kustomizetypes.Image{
Name: "quay.io/someorg/debian:0.1",
NewName: "localhost:5000/debian",
NewTag: "0.1",
Digest: "",
},
isError: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
scopetest := scopeagent.StartTest(t)
defer scopetest.End()
image, err := ImageInfoFromFile(registryOps, test.parts)
image, err := ImageInfoFromFile(test.registryOps, test.parts)
if test.isError {
assert.Error(t, err)
} else {
Expand Down Expand Up @@ -118,6 +144,16 @@ func TestDestRef(t *testing.T) {
},
want: fmt.Sprintf("%s/%s/debian@sha256:mytestdigest", registryOps.Endpoint, registryOps.Namespace),
},
{
name: "No Namespace",
args: args{
registry: registry.RegistryOptions{
Endpoint: "localhost:5000",
},
srcImage: "quay.io/someorg/debian:0.1",
},
want: fmt.Sprintf("%s/debian:0.1", registryOps.Endpoint),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -265,6 +301,21 @@ func Test_buildImageAlts(t *testing.T) {
},
},
},
{
name: "no namespace",
destRegistry: registry.RegistryOptions{
Endpoint: "localhost:5000",
},
image: "library/redis:v1",
want: []kustomizetypes.Image{
{
Name: "library/redis",
NewName: "localhost:5000/redis",
NewTag: "v1",
Digest: "",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit fb572c7

Please sign in to comment.