Skip to content

Commit d66fa11

Browse files
committed
fix: Allow to delete images when names of images are short digest ids of another images.
"nerdctl rmi <REPOSITORY>" can be run to delete the target images. However, at the current implementation, the deletion fails when images names are the short digest ids of another images. The specific behavior is described below, which is how it works in the current implementation. First, suppose there are alpine and busybox images. ``` > nerdctl images REPOSITORY TAG IMAGE ID CREATED PLATFORM SIZE BLOB SIZE busybox latest 768e5c6f5cb6 3 seconds ago linux/arm64 4.092MB 1.845MB alpine latest beefdbd8a1da 11 seconds ago linux/arm64 10.46MB 4.09MB ``` Then, we tag the alpine image using digest id of the busybox image. ``` > nerdctl tag alpine $(dn inspect busybox | jq -rc .[0].RepoDigests[0] | awk -F':' '{print substr($2, 1, 8)}') > nerdctl images REPOSITORY TAG IMAGE ID CREATED PLATFORM SIZE BLOB SIZE 768e5c6f latest beefdbd8a1da 4 seconds ago linux/arm64 10.46MB 4.09MB busybox latest 768e5c6f5cb6 22 hours ago linux/arm64 4.092MB 1.845MB alpine latest beefdbd8a1da 22 hours ago linux/arm64 10.46MB 4.09MB ``` In this situation, running 'nerdctl rmi "$(dn inspect busybox | jq -rc .[0].RepoDigests[0] | awk -F':' '{print substr($2, 1, 8)}')"' will fail to remove the image. The details of the error are as follows. ``` > nerdctl rmi "$(dn inspect busybox | jq -rc .[0].RepoDigests[0] | awk -F':' '{print substr($2, 1, 8)}')" FATA[0000] 1 errors: multiple IDs found with provided prefix: 768e5c6f ``` This issue is reported in the following issue. - containerd#3016 Therefore, this pull request modifies this so that images can be deleted with "nerdctl rmi <short digest id of another image>" when images names are the short digest ids of another images. Signed-off-by: Hayato Kiwata <[email protected]>
1 parent 427f1cb commit d66fa11

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

pkg/idutil/imagewalker/imagewalker.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,26 @@ func (w *ImageWalker) Walk(ctx context.Context, req string) (int, error) {
6464
return -1, err
6565
}
6666

67-
matchCount := len(images)
6867
// to handle the `rmi -f` case where returned images are different but
6968
// have the same short prefix.
7069
uniqueImages := make(map[digest.Digest]bool)
7170
for _, image := range images {
7271
uniqueImages[image.Target.Digest] = true
7372
}
7473

74+
// Allow to nerdctl rmi <short digest ids of another images> to remove images.
75+
if len(uniqueImages) > 1 {
76+
imageIDPrefix := fmt.Sprintf("sha256:%s", regexp.QuoteMeta(req))
77+
for i := len(images) - 1; i >= 0; i-- {
78+
if strings.HasPrefix(images[i].Target.Digest.String(), imageIDPrefix) {
79+
delete(uniqueImages, images[i].Target.Digest)
80+
images = append(images[:i], images[i+1:]...)
81+
}
82+
}
83+
}
84+
85+
matchCount := len(images)
86+
7587
for i, img := range images {
7688
f := Found{
7789
Image: img,

0 commit comments

Comments
 (0)