Skip to content

Commit

Permalink
Merge pull request #27 from rumstead/master
Browse files Browse the repository at this point in the history
Fixes image url with ports
  • Loading branch information
etiennetremel authored Jun 17, 2020
2 parents 33ff93f + d9e766c commit efadb0e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 30 deletions.
4 changes: 0 additions & 4 deletions pkg/transformers/configmap.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package transformers

import (
"regexp"

"github.com/ContainerSolutions/helm-convert/pkg/types"
"github.com/golang/glog"
ktypes "sigs.k8s.io/kustomize/pkg/types"
)

type configMapTransformer struct{}

var regexpMultiline = regexp.MustCompile("\n")

var _ Transformer = &configMapTransformer{}

// NewConfigMapTransformer constructs a configMapTransformer.
Expand Down
50 changes: 27 additions & 23 deletions pkg/transformers/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

// imageTransformer replace images
type imageTransformer struct {
images []kimage.Image
}

var _ Transformer = &imageTransformer{}
Expand Down Expand Up @@ -68,40 +67,45 @@ LOOP_CONTAINERS:
}

imagePathStr := imagePath.(string)
image := createKImage(imagePathStr)

hasDigest := strings.Contains(imagePathStr, "@")
separator := ":"

if hasDigest {
separator = "@"
}

s := strings.Split(imagePathStr, separator)

image := kimage.Image{
Name: s[0],
}

// doesn't add image if already in the list
// don't add image if already in the list
for _, v := range config.Images {
if v.Name == image.Name {
continue LOOP_CONTAINERS
}
}

if len(s) > 1 {
if hasDigest {
image.Digest = s[1]
} else {
image.NewTag = s[1]
}
}

config.Images = append(config.Images, image)
}
return nil
}

func createKImage(imagePathStr string) kimage.Image {
hasDigest := strings.Contains(imagePathStr, "@")
separator := ":"

if hasDigest {
separator = "@"
}

s := strings.Split(imagePathStr, separator)
image := kimage.Image{
Name: s[0],
}
if len(s) > 1 {
// combine everything but the last element in the string
// fixes if image URL has a port
image.Name = strings.Join(s[:len(s)-1], separator)
if hasDigest {
image.Digest = s[len(s)-1]
} else {
image.NewTag = s[len(s)-1]
}
}
return image
}

func (pt *imageTransformer) findContainers(config *ktypes.Kustomization, obj map[string]interface{}) error {
for key := range obj {
switch typedV := obj[key].(type) {
Expand Down
43 changes: 40 additions & 3 deletions pkg/transformers/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func TestImageRun(t *testing.T) {
"name": "alpine",
"image": "alpine@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3",
},
map[string]interface{}{
"name": "centos",
"image": "myregistry:5000/namespace/centos:1.2.3",
},
},
},
},
Expand Down Expand Up @@ -91,9 +95,10 @@ func TestImageRun(t *testing.T) {
expected: &imageTransformerArgs{
config: &ktypes.Kustomization{
Images: []kimage.Image{
kimage.Image{Name: "alpine", Digest: "sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3"},
kimage.Image{Name: "busybox"},
kimage.Image{Name: "nginx", NewTag: "1.7.9"},
{Name: "alpine", Digest: "sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3"},
{Name: "busybox"},
{Name: "myregistry:5000/namespace/centos", NewTag: "1.2.3"},
{Name: "nginx", NewTag: "1.7.9"},
},
},
resources: &types.Resources{
Expand Down Expand Up @@ -122,6 +127,10 @@ func TestImageRun(t *testing.T) {
"name": "alpine",
"image": "alpine@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3",
},
map[string]interface{}{
"name": "centos",
"image": "myregistry:5000/namespace/centos:1.2.3",
},
},
},
},
Expand Down Expand Up @@ -171,3 +180,31 @@ func TestImageRun(t *testing.T) {
})
}
}

func TestCreateKImage(t *testing.T) {
imagePath := "myregistry:5000/namespace/busybox:1.2.3"
image := createKImage(imagePath)
if image.Name != "myregistry:5000/namespace/busybox" || image.NewTag != "1.2.3" {
t.Fatalf("Parsed imageName: %s newTag %s from %s", image.Name, image.NewTag, imagePath)
}

imagePath = "myregistry:5000/namespace/busybox@sha256" +
":24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3"
image = createKImage(imagePath)
if image.Name != "myregistry:5000/namespace/busybox" || image.Digest != "sha256:"+
"24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3" {
t.Fatalf("Parsed imageName: %s digest %s from %s", image.Name, image.Digest, imagePath)
}

imagePath = "busybox"
image = createKImage(imagePath)
if image.Name != "busybox" {
t.Fatalf("Parsed imageName: %s newTag %s from %s", image.Name, image.NewTag, imagePath)
}

imagePath = "busybox:1.2.3"
image = createKImage(imagePath)
if image.Name != "busybox" || image.NewTag != "1.2.3" {
t.Fatalf("Parsed imageName: %s newTag %s from %s", image.Name, image.NewTag, imagePath)
}
}

0 comments on commit efadb0e

Please sign in to comment.