Skip to content

Commit

Permalink
Add support for setting the name while keeping the digest
Browse files Browse the repository at this point in the history
- And the other way around

Issue: #3487
  • Loading branch information
dimw committed Jan 25, 2021
1 parent bb2d63a commit 9b4fdcf
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
18 changes: 18 additions & 0 deletions kustomize/internal/commands/edit/set/setimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ func (o *setImageOptions) RunSetImage(fSys filesys.FileSystem) error {
argIm = replaceNewTag(argIm, im.NewTag)
}

// Reuse the existing digest when asterisk disgest is passed
if argIm.Digest == preserveSeparator {
argIm = replaceDigest(argIm, im.Digest)
}

o.imageMap[im.Name] = argIm

continue
Expand All @@ -159,6 +164,10 @@ func (o *setImageOptions) RunSetImage(fSys filesys.FileSystem) error {
v = replaceNewTag(v, "")
}

if v.Digest == preserveSeparator {
v = replaceDigest(v, "")
}

images = append(images, v)
}

Expand Down Expand Up @@ -188,6 +197,15 @@ func replaceNewTag(image types.Image, newTag string) types.Image {
}
}

func replaceDigest(image types.Image, digest string) types.Image {
return types.Image{
Name: image.Name,
NewName: image.NewName,
NewTag: image.NewTag,
Digest: digest,
}
}

func parse(arg string) (types.Image, error) {

// matches if there is an image name to overwrite
Expand Down
57 changes: 57 additions & 0 deletions kustomize/internal/commands/edit/set/setimage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,63 @@ func TestSetImage(t *testing.T) {
" newName: my-image1",
}},
},
{
description: "keep new name and update digest",
given: given{
args: []string{"image2=*@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3"},
infileImages: []string{
"images:",
"- name: image2",
" newName: my-image2",
" digest: sha256:abcdef12345",
},
},
expected: expected{
fileOutput: []string{
"images:",
"- digest: sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3",
" name: image2",
" newName: my-image2",
}},
},
{
description: "keep new name, remove tag, and set digest",
given: given{
args: []string{"image2=*@sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3"},
infileImages: []string{
"images:",
"- name: image2",
" newName: my-image2",
" newTag: my-tag",
},
},
expected: expected{
fileOutput: []string{
"images:",
"- digest: sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3",
" name: image2",
" newName: my-image2",
}},
},
{
description: "update new name and keep the digest",
given: given{
args: []string{"image2=my-image2@*"},
infileImages: []string{
"images:",
"- digest: sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3",
" name: image2",
" newName: foo.bar.foo:8800/foo/image1",
},
},
expected: expected{
fileOutput: []string{
"images:",
"- digest: sha256:24a0c4b4a4c0eb97a1aabb8e29f18e917d05abfe1b7a7c07857230879ce7d3d3",
" name: image2",
" newName: my-image2",
}},
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 9b4fdcf

Please sign in to comment.