Repository: replicatedhq/kots
Pull request #1059 (2020-09-03 17:09:43)

Namespace not required in reference format


Patch:
diff --git a/pkg/image/image.go b/pkg/image/image.go
index e4babd94a..dd682b6ed 100644
--- a/pkg/image/image.go
+++ b/pkg/image/image.go
@@ -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])
@@ -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
@@ -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) {


Test patch:
diff --git a/pkg/image/image_test.go b/pkg/image/image_test.go
index 7fb8eb5ef..d466c5774 100644
--- a/pkg/image/image_test.go
+++ b/pkg/image/image_test.go
@@ -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:  "",
 			},
@@ -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:  "",
 			},
@@ -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 {
@@ -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) {
@@ -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) {