fix edit set image to parse both tag and digest

This commit is contained in:
Juan Ignacio Donoso
2023-07-05 15:01:33 -04:00
parent e3b9afcfaa
commit a85dfd4141
3 changed files with 75 additions and 43 deletions

10
api/pkg/util/image.go Normal file
View File

@@ -0,0 +1,10 @@
package util
import (
"sigs.k8s.io/kustomize/api/internal/image"
)
// Splits image string name into name, tag and digest
func SplitImageName(imageName string) (name string, tag string, digest string) {
return image.Split(imageName)
}

View File

@@ -9,6 +9,7 @@ import (
"sort" "sort"
"strings" "strings"
"sigs.k8s.io/kustomize/api/pkg/util"
"sigs.k8s.io/kustomize/api/types" "sigs.k8s.io/kustomize/api/types"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -31,8 +32,10 @@ var (
errImageInvalidArgs = errors.New(`invalid format of image, use one of the following options: errImageInvalidArgs = errors.New(`invalid format of image, use one of the following options:
- <image>=<newimage>:<newtag> - <image>=<newimage>:<newtag>
- <image>=<newimage>@<digest> - <image>=<newimage>@<digest>
- <image>=<newimage>:<newtag>@<digest>
- <image>=<newimage> - <image>=<newimage>
- <image>:<newtag> - <image>:<newtag>
- <image>:<newtag>@<digest>
- <image>@<digest>`) - <image>@<digest>`)
) )
@@ -76,7 +79,7 @@ images:
to the kustomization file if it doesn't exist, to the kustomization file if it doesn't exist,
and overwrite the previous ones if the image name exists. 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. the new tag, or the digest will preserve the appropriate values from the kustomization file.
`, `,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
@@ -204,52 +207,46 @@ func replaceDigest(image types.Image, digest string) types.Image {
} }
func parse(arg string) (types.Image, error) { func parse(arg string) (types.Image, error) {
// matches if there is an image name to overwrite // matches if there is an image name
// <image>=<new-image><:|@><new-tag> // <image>=<new-image>:<new-tag>@<digest>
if s := strings.Split(arg, separator); len(s) == 2 { // supports digest and tag and override the image name
p, err := parseOverwrite(s[1], true) key, value, err := imageArgParse(arg)
return types.Image{ if err != nil {
Name: s[0], return types.Image{}, err
NewName: p.name,
NewTag: p.tag,
Digest: p.digest,
}, err
} }
// matches only for <tag|digest> overwrites name, tag, digest := util.SplitImageName(value)
// <image><:|@><new-tag> if name == arg {
p, err := parseOverwrite(arg, false) return types.Image{}, errImageInvalidArgs
return types.Image{ }
Name: p.name,
NewTag: p.tag, newImage := types.Image{
Digest: p.digest, NewTag: tag,
}, err Digest: digest,
}
if key == "" {
newImage.Name = name
} else {
newImage.Name = key
newImage.NewName = name
}
return newImage, nil
} }
// parseOverwrite parses the overwrite parameters func imageArgParse(arg string) (key string, value string, err error) {
// from the given arg into a struct const maxArgsSeparatorCount = 2
func parseOverwrite(arg string, overwriteImage bool) (overwrite, error) { const keyAndValueArgsCount = 2
// match <image>@<digest>
if d := strings.Split(arg, "@"); len(d) > 1 {
return overwrite{
name: d[0],
digest: d[1],
}, nil
}
// match <image>:<tag> s := strings.SplitN(arg, separator, maxArgsSeparatorCount)
if t := pattern.FindStringSubmatch(arg); len(t) == 3 { if len(s) == keyAndValueArgsCount {
return overwrite{ // If separator is found it returns the key and value
name: t[1], return s[0], s[1], nil
tag: t[2], } else if len(s) == 1 {
}, nil // If no separator is found it returns the whole string as value
// and the key is empty
return "", s[0], nil
} }
return "", "", errImageInvalidArgs
// match <image>
if len(arg) > 0 && overwriteImage {
return overwrite{
name: arg,
}, nil
}
return overwrite{}, errImageInvalidArgs
} }

View File

@@ -96,6 +96,31 @@ func TestSetImage(t *testing.T) {
" name: image1", " 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>", description: "<image>=<image>",
given: given{ given: given{