c8d/softDelete: Extract ensureDanglingImage

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
This commit is contained in:
Paweł Gronowski 2023-06-29 14:36:22 +02:00
parent a96e6044cc
commit 2b0655a71a
No known key found for this signature in database
GPG key ID: B85EFCFE26DEF92A

View file

@ -30,19 +30,12 @@ func (i *ImageService) softImageDelete(ctx context.Context, img containerdimages
// Create dangling image if this is the last image pointing to this target.
if len(imgs) == 1 {
danglingImage := img
danglingImage.Name = danglingImageName(img.Target.Digest)
delete(danglingImage.Labels, containerdimages.AnnotationImageName)
delete(danglingImage.Labels, ocispec.AnnotationRefName)
_, err = is.Create(context.Background(), danglingImage)
err = i.ensureDanglingImage(context.Background(), img)
// Error out in case we couldn't persist the old image.
// If it already exists, then just continue.
if err != nil && !cerrdefs.IsAlreadyExists(err) {
if err != nil {
return errdefs.System(errors.Wrapf(err, "failed to create a dangling image for the replaced image %s with digest %s",
danglingImage.Name, danglingImage.Target.Digest.String()))
img.Name, img.Target.Digest.String()))
}
}
@ -57,6 +50,22 @@ func (i *ImageService) softImageDelete(ctx context.Context, img containerdimages
return nil
}
func (i *ImageService) ensureDanglingImage(ctx context.Context, from containerdimages.Image) error {
danglingImage := from
danglingImage.Name = danglingImageName(from.Target.Digest)
delete(danglingImage.Labels, containerdimages.AnnotationImageName)
delete(danglingImage.Labels, ocispec.AnnotationRefName)
_, err := i.client.ImageService().Create(context.Background(), danglingImage)
// If it already exists, then just continue.
if cerrdefs.IsAlreadyExists(err) {
return nil
}
return err
}
func danglingImageName(digest digest.Digest) string {
return "moby-dangling@" + digest.String()
}