Skip to content

Commit

Permalink
fix: Allow to delete images when names of images are short digest ids…
Browse files Browse the repository at this point in the history
… 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]>
  • Loading branch information
haytok committed Oct 17, 2024
1 parent 26a2297 commit fba4344
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion pkg/idutil/imagewalker/imagewalker.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,26 @@ func (w *ImageWalker) Walk(ctx context.Context, req string) (int, error) {
return -1, err
}

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

// Allow to nerdctl rmi <short digest ids of another images> to remove images.
if len(uniqueImages) > 1 {
imageIDPrefix := fmt.Sprintf("sha256:%s", regexp.QuoteMeta(req))
for i := len(images) - 1; i >= 0; i-- {
if strings.HasPrefix(images[i].Target.Digest.String(), imageIDPrefix) {
delete(uniqueImages, images[i].Target.Digest)
images = append(images[:i], images[i+1:]...)
}
}
}

matchCount := len(images)

for i, img := range images {
f := Found{
Image: img,
Expand Down

0 comments on commit fba4344

Please sign in to comment.