mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-14 10:30:59 +00:00
Add support for setting new tag while preserving the current name
- As well as setting new name while keeping the tag Issue: #3487
This commit is contained in:
@@ -20,7 +20,9 @@ type setImageOptions struct {
|
|||||||
imageMap map[string]types.Image
|
imageMap map[string]types.Image
|
||||||
}
|
}
|
||||||
|
|
||||||
var pattern = regexp.MustCompile("^(.*):([a-zA-Z0-9._-]*)$")
|
var pattern = regexp.MustCompile("^(.*):([a-zA-Z0-9._-]*|\\*)$")
|
||||||
|
|
||||||
|
var preserveSeparator = "*"
|
||||||
|
|
||||||
// errors
|
// errors
|
||||||
|
|
||||||
@@ -74,7 +76,8 @@ 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 '-'.
|
The image tag can only contain alphanumeric, '.', '_' and '-'. Passing * (asterisk) either in the new name
|
||||||
|
or in new tag 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 {
|
||||||
err := o.Validate(args)
|
err := o.Validate(args)
|
||||||
@@ -125,7 +128,20 @@ func (o *setImageOptions) RunSetImage(fSys filesys.FileSystem) error {
|
|||||||
|
|
||||||
// append only new images from kustomize file
|
// append only new images from kustomize file
|
||||||
for _, im := range m.Images {
|
for _, im := range m.Images {
|
||||||
if _, ok := o.imageMap[im.Name]; ok {
|
if argIm, ok := o.imageMap[im.Name]; ok {
|
||||||
|
|
||||||
|
// Reuse the existing new name when asterisk new name is passed
|
||||||
|
if argIm.NewName == preserveSeparator {
|
||||||
|
argIm = replaceNewName(argIm, im.NewName)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reuse the existing new tag when asterisk new tag is passed
|
||||||
|
if argIm.NewTag == preserveSeparator {
|
||||||
|
argIm = replaceNewTag(argIm, im.NewTag)
|
||||||
|
}
|
||||||
|
|
||||||
|
o.imageMap[im.Name] = argIm
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,6 +150,15 @@ func (o *setImageOptions) RunSetImage(fSys filesys.FileSystem) error {
|
|||||||
|
|
||||||
var images []types.Image
|
var images []types.Image
|
||||||
for _, v := range o.imageMap {
|
for _, v := range o.imageMap {
|
||||||
|
|
||||||
|
if v.NewName == preserveSeparator {
|
||||||
|
v = replaceNewName(v, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
if v.NewTag == preserveSeparator {
|
||||||
|
v = replaceNewTag(v, "")
|
||||||
|
}
|
||||||
|
|
||||||
images = append(images, v)
|
images = append(images, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,6 +170,24 @@ func (o *setImageOptions) RunSetImage(fSys filesys.FileSystem) error {
|
|||||||
return mf.Write(m)
|
return mf.Write(m)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func replaceNewName(image types.Image, newName string) types.Image {
|
||||||
|
return types.Image{
|
||||||
|
Name: image.Name,
|
||||||
|
NewName: newName,
|
||||||
|
NewTag: image.NewTag,
|
||||||
|
Digest: image.Digest,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func replaceNewTag(image types.Image, newTag string) types.Image {
|
||||||
|
return types.Image{
|
||||||
|
Name: image.Name,
|
||||||
|
NewName: image.NewName,
|
||||||
|
NewTag: newTag,
|
||||||
|
Digest: image.Digest,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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 to overwrite
|
||||||
|
|||||||
@@ -251,6 +251,104 @@ func TestSetImage(t *testing.T) {
|
|||||||
err: errImageInvalidArgs,
|
err: errImageInvalidArgs,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
description: "override new tag but keep new name",
|
||||||
|
given: given{
|
||||||
|
args: []string{"image1=*:v1"},
|
||||||
|
infileImages: []string{
|
||||||
|
"images:",
|
||||||
|
"- name: image1",
|
||||||
|
" newName: foo.bar.foo:8800/foo/image1",
|
||||||
|
" newTag: my-tag",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: expected{
|
||||||
|
fileOutput: []string{
|
||||||
|
"images:",
|
||||||
|
"- name: image1",
|
||||||
|
" newName: foo.bar.foo:8800/foo/image1",
|
||||||
|
" newTag: v1",
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "override new name but keep new tag",
|
||||||
|
given: given{
|
||||||
|
args: []string{"image1=my-image1:*"},
|
||||||
|
infileImages: []string{
|
||||||
|
"images:",
|
||||||
|
"- name: image1",
|
||||||
|
" newName: foo.bar.foo:8800/foo/image1",
|
||||||
|
" newTag: my-tag",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: expected{
|
||||||
|
fileOutput: []string{
|
||||||
|
"images:",
|
||||||
|
"- name: image1",
|
||||||
|
" newName: my-image1",
|
||||||
|
" newTag: my-tag",
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "keep new name and new tag (rare case)",
|
||||||
|
given: given{
|
||||||
|
args: []string{"image1=*:*"},
|
||||||
|
infileImages: []string{
|
||||||
|
"images:",
|
||||||
|
"- name: image1",
|
||||||
|
" newName: my-image1",
|
||||||
|
" newTag: my-tag",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: expected{
|
||||||
|
fileOutput: []string{
|
||||||
|
"images:",
|
||||||
|
"- name: image1",
|
||||||
|
" newName: my-image1",
|
||||||
|
" newTag: my-tag",
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "do not set asterisk as new name for existing image",
|
||||||
|
given: given{
|
||||||
|
args: []string{"image1=*:v1"},
|
||||||
|
infileImages: []string{
|
||||||
|
"images:",
|
||||||
|
"- name: image1",
|
||||||
|
" newTag: my-tag",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: expected{
|
||||||
|
fileOutput: []string{
|
||||||
|
"images:",
|
||||||
|
"- name: image1",
|
||||||
|
" newTag: v1",
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "do not set asterisk as new name",
|
||||||
|
given: given{
|
||||||
|
args: []string{"image1=*:v1"},
|
||||||
|
},
|
||||||
|
expected: expected{
|
||||||
|
fileOutput: []string{
|
||||||
|
"images:",
|
||||||
|
"- name: image1",
|
||||||
|
" newTag: v1",
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
description: "do not set asterisk as new tag",
|
||||||
|
given: given{
|
||||||
|
args: []string{"image1=my-image1:*"},
|
||||||
|
},
|
||||||
|
expected: expected{
|
||||||
|
fileOutput: []string{
|
||||||
|
"images:",
|
||||||
|
"- name: image1",
|
||||||
|
" newName: my-image1",
|
||||||
|
}},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
|
|||||||
Reference in New Issue
Block a user