use regexp to determin if the image matched in imagetag transformer

This commit is contained in:
Jingfang Liu
2018-07-30 11:09:32 -07:00
parent 4d9d54e2c7
commit 36115a7fa3
2 changed files with 11 additions and 8 deletions

View File

@@ -17,6 +17,7 @@ limitations under the License.
package transformers
import (
"regexp"
"strings"
"github.com/kubernetes-sigs/kustomize/pkg/resmap"
@@ -115,6 +116,7 @@ func (pt *imageTagTransformer) findContainers(obj map[string]interface{}) error
}
func isImageMatched(s, t string) bool {
imagetag := strings.Split(s, ":")
return len(imagetag) >= 1 && imagetag[0] == t
// Tag values are limited to [a-zA-Z0-9_.-].
pattern, _ := regexp.Compile("^" + t + "(:[a-zA-Z0-9_.-]*)?$")
return pattern.MatchString(s)
}

View File

@@ -66,8 +66,8 @@ func TestImageTagTransformer(t *testing.T) {
"image": "nginx",
},
map[string]interface{}{
"name": "nginx2",
"image": "my-nginx:random",
"name": "myimage",
"image": "myprivaterepohostname:1234/my/image:latest",
},
},
},
@@ -78,7 +78,7 @@ func TestImageTagTransformer(t *testing.T) {
"spec": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"name": "ngin3",
"name": "nginx3",
"image": "nginx:v1",
},
map[string]interface{}{
@@ -130,8 +130,8 @@ func TestImageTagTransformer(t *testing.T) {
"image": "nginx:v2",
},
map[string]interface{}{
"name": "nginx2",
"image": "my-nginx:previous",
"name": "myimage",
"image": "myprivaterepohostname:1234/my/image:v1.0.1",
},
},
},
@@ -142,7 +142,7 @@ func TestImageTagTransformer(t *testing.T) {
"spec": map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"name": "ngin3",
"name": "nginx3",
"image": "nginx:v2",
},
map[string]interface{}{
@@ -159,6 +159,7 @@ func TestImageTagTransformer(t *testing.T) {
it, err := NewImageTagTransformer([]types.ImageTag{
{Name: "nginx", NewTag: "v2"},
{Name: "my-nginx", NewTag: "previous"},
{Name: "myprivaterepohostname:1234/my/image", NewTag: "v1.0.1"},
})
if err != nil {
t.Fatalf("unexpected error: %v", err)