Skip to content

Commit

Permalink
fix edit set image to parse both tag and digest
Browse files Browse the repository at this point in the history
  • Loading branch information
blackjid committed Jul 5, 2023
1 parent da4e881 commit 716a102
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 41 deletions.
70 changes: 29 additions & 41 deletions kustomize/commands/edit/set/setimage.go
Expand Up @@ -9,6 +9,7 @@ import (
"sort"
"strings"

"sigs.k8s.io/kustomize/api/image"
"sigs.k8s.io/kustomize/api/types"

"github.com/spf13/cobra"
Expand All @@ -31,8 +32,10 @@ var (
errImageInvalidArgs = errors.New(`invalid format of image, use one of the following options:
- <image>=<newimage>:<newtag>
- <image>=<newimage>@<digest>
- <image>=<newimage>:<newtag>@<digest>
- <image>=<newimage>
- <image>:<newtag>
- <image>:<newtag>@<digest>
- <image>@<digest>`)
)

Expand Down Expand Up @@ -76,7 +79,7 @@ images:
to the kustomization file if it doesn't exist,
and overwrite the previous ones if the image name exists.
The image tag can only contain alphanumeric, '.', '_' and '-'. Passing * (asterisk) either as the new name,
The image tag can only contain alphanumeric, '.', '_' and '-'. Passing * (asterisk) either as the new name,
the new tag, or the digest will preserve the appropriate values from the kustomization file.
`,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -204,52 +207,37 @@ func replaceDigest(image types.Image, digest string) types.Image {
}

func parse(arg string) (types.Image, error) {
// matches if there is an image name to overwrite
// <image>=<new-image><:|@><new-tag>
if s := strings.Split(arg, separator); len(s) == 2 {
p, err := parseOverwrite(s[1], true)
return types.Image{
Name: s[0],
NewName: p.name,
NewTag: p.tag,
Digest: p.digest,
}, err
key, value, err := imageArgParse(arg)
if err != nil {
return types.Image{}, err
}

// matches only for <tag|digest> overwrites
// <image><:|@><new-tag>
p, err := parseOverwrite(arg, false)
return types.Image{
Name: p.name,
NewTag: p.tag,
Digest: p.digest,
}, err
}
name, tag, digest := image.Split(value)
if name == arg {
return types.Image{}, errImageInvalidArgs
}

// parseOverwrite parses the overwrite parameters
// from the given arg into a struct
func parseOverwrite(arg string, overwriteImage bool) (overwrite, error) {
// match <image>@<digest>
if d := strings.Split(arg, "@"); len(d) > 1 {
return overwrite{
name: d[0],
digest: d[1],
}, nil
newImage := types.Image{
NewTag: tag,
Digest: digest,
}

// match <image>:<tag>
if t := pattern.FindStringSubmatch(arg); len(t) == 3 {
return overwrite{
name: t[1],
tag: t[2],
}, nil
if key == "" {
newImage.Name = name
} else {
newImage.Name = key
newImage.NewName = name
}

// match <image>
if len(arg) > 0 && overwriteImage {
return overwrite{
name: arg,
}, nil
return newImage, nil
}

func imageArgParse(arg string) (key string, value string, err error) {
s := strings.SplitN(arg, separator, 2)
if len(s) == 2 {
return s[0], s[1], nil
} else if len(s) == 1 {
return "", s[0], nil
}
return overwrite{}, errImageInvalidArgs
return "", "", errImageInvalidArgs
}
25 changes: 25 additions & 0 deletions kustomize/commands/edit/set/setimage_test.go
Expand Up @@ -96,6 +96,31 @@ func TestSetImage(t *testing.T) {
" name: image1",
}},
},
{
given: given{
args: []string{"my-image1:my-tag@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3"},
},
expected: expected{
fileOutput: []string{
"images:",
"- digest: sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3",
" name: my-image1",
" newTag: my-tag",
}},
},
{
given: given{
args: []string{"image1=my-image1:my-tag@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3"},
},
expected: expected{
fileOutput: []string{
"images:",
"- digest: sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3",
" name: image1",
" newName: my-image1",
" newTag: my-tag",
}},
},
{
description: "<image>=<image>",
given: given{
Expand Down

0 comments on commit 716a102

Please sign in to comment.