diff --git a/pkg/commands/setimagetag.go b/pkg/commands/setimagetag.go index 63f8eed76..12c23b55f 100644 --- a/pkg/commands/setimagetag.go +++ b/pkg/commands/setimagetag.go @@ -18,20 +18,22 @@ package commands import ( "errors" - "strings" + "regexp" + "sort" "github.com/spf13/cobra" "github.com/kubernetes-sigs/kustomize/pkg/constants" "github.com/kubernetes-sigs/kustomize/pkg/fs" "github.com/kubernetes-sigs/kustomize/pkg/types" - "sort" ) type setImageTagOptions struct { imageTagMap map[string]string } +var pattern = regexp.MustCompile("^(.*):([a-zA-Z0-9._-]*)$") + // newCmdSetImageTag sets the new tags for images in the kustomization. func newCmdSetImageTag(fsys fs.FileSystem) *cobra.Command { var o setImageTagOptions @@ -71,11 +73,11 @@ func (o *setImageTagOptions) Validate(args []string) error { } o.imageTagMap = make(map[string]string) for _, arg := range args { - imagetag := strings.Split(arg, ":") - if len(imagetag) != 2 { + imagetag := pattern.FindStringSubmatch(arg) + if len(imagetag) != 3 { return errors.New("Invalid format of imagetag, must specify it as :") } - o.imageTagMap[imagetag[0]] = imagetag[1] + o.imageTagMap[imagetag[1]] = imagetag[2] } return nil } diff --git a/pkg/commands/setimagetag_test.go b/pkg/commands/setimagetag_test.go index 7a7287fc6..597decfd4 100644 --- a/pkg/commands/setimagetag_test.go +++ b/pkg/commands/setimagetag_test.go @@ -29,7 +29,7 @@ func TestSetImageTagsHappyPath(t *testing.T) { fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent)) cmd := newCmdSetImageTag(fakeFS) - args := []string{"image1:tag1", "image2:tag2"} + args := []string{"image1:tag1", "image2:tag2", "localhost:5000/operator:1.0.0"} err := cmd.RunE(cmd, args) if err != nil { t.Errorf("unexpected cmd error: %v", err) @@ -44,6 +44,8 @@ imageTags: newTag: tag1 - name: image2 newTag: tag2 +- name: localhost:5000/operator + newTag: 1.0.0 `) if !strings.Contains(string(content), string(expected)) { t.Errorf("expected imageTags in kustomization file")