Skip to content

Add ctr image prune command#7730

Merged
dmcgowan merged 1 commit intocontainerd:mainfrom
djdongjin:ctr-prune
Nov 29, 2022
Merged

Add ctr image prune command#7730
dmcgowan merged 1 commit intocontainerd:mainfrom
djdongjin:ctr-prune

Conversation

@djdongjin
Copy link
Copy Markdown
Member

Fix #5844.

The implementation is based on nerdctl image prune.

It deletes unused images and logs deleted images and their digests.

Although nerdctl is recommended for daily usage, ctr image prune might still helpful for debugging to easily clean up leftover images.

Signed-off-by: Jin Dong [email protected]

@k8s-ci-robot
Copy link
Copy Markdown

Hi @djdongjin. Thanks for your PR.

I'm waiting for a containerd member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Copy link
Copy Markdown
Member

@mikebrow mikebrow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a couple comments...

Comment thread cmd/ctr/commands/images/images.go Outdated
Comment thread cmd/ctr/commands/images/images.go Outdated
Comment thread cmd/ctr/commands/images/images.go Outdated
@mikebrow
Copy link
Copy Markdown
Member

/ok-to-test

@djdongjin
Copy link
Copy Markdown
Member Author

Thanks for review @mikebrow. Updated based on your comments.

Comment thread cmd/ctr/commands/images/images.go Outdated
Comment thread cmd/ctr/commands/images/images.go
Copy link
Copy Markdown
Member

@dcantah dcantah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change looks fine, curious on one aspect

@djdongjin djdongjin requested a review from mikebrow November 28, 2022 23:20
Comment thread cmd/ctr/commands/images/images.go Outdated
for image, digests := range removedImages {
log.G(ctx).Infof("untagged: %s\n", image)
for _, digest := range digests {
log.G(ctx).Infof("deleted: %s\n", digest)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this can be assumed and not something that is surfaced to the client. With ctr I think its best to error on accuracy or skip the functionality. The containerd logs would accurately show what got removed during GC. I don't think it is worth it here to check each layer to see if it still exists.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for review @dmcgowan. I removed the layer check/log here.

Comment thread cmd/ctr/commands/images/images.go Outdated
if err != nil {
log.G(ctx).WithError(err).Warnf("failed to enumerate rootfs of image %s", image.Name)
}
if err := imageStore.Delete(ctx, image.Name, delOpts...); err != nil {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the delete could be deferred to the next loop, SynchronousDelete option could be reserved for the last run. Synchronous delete can also be run for a lease as well. It is not a good pattern to do synchronous deletes in a loop though.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Member Author

@djdongjin djdongjin Nov 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Derek and Mike :) Yes the code is helpful! I've updated the logic to match the CRI logic.

I used []string to store removed image names which assumes image.Name returned from imageStore.List(ctx) is unique. If it doesn't hold I can change to use map[string]interface{}.

Another thing is I notice the CRI code has the Update if Delete succeeds logic: https://github.com/containerd/containerd/blame/main/pkg/cri/server/image_remove.go#L58-L64

Do we need the same logic here? Thanks

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the same logic here? Thanks

There is no equivalent image store cache outside of CRI and we are working on removing that image store cache so that we don't have to do that sort of synchronization and operations outside of CRI are reflected better through CRI.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Derek, thanks for the explanation! I've updated the logic to using SynchronousDelete only for the last run (and ignore IsNotFound err).

@djdongjin djdongjin force-pushed the ctr-prune branch 3 times, most recently from 08e4312 to 66595da Compare November 29, 2022 16:21
if i == len(removedImages)-1 {
delOpts = []images.DeleteOpt{images.SynchronousDelete()}
}
if err := imageStore.Delete(ctx, imageName, delOpts...); err != nil {
Copy link
Copy Markdown
Member

@mikebrow mikebrow Nov 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

errdefs.IsNotFound(err) should be ignored here... because async deletes.. though a simple warning probably doesn't hurt...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the reminder Mike. Added errdefs.IsNotFound(err) check :)

Signed-off-by: Jin Dong <[email protected]>
@dmcgowan dmcgowan merged commit 763d4e1 into containerd:main Nov 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

ctr should support "ctr image prune" command

6 participants