2023-02-02 20:57:33 +00:00
|
|
|
package containerd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
cerrdefs "github.com/containerd/containerd/errdefs"
|
|
|
|
containerdimages "github.com/containerd/containerd/images"
|
|
|
|
"github.com/docker/docker/errdefs"
|
2023-10-04 11:17:51 +00:00
|
|
|
"github.com/docker/docker/internal/compatcontext"
|
2023-02-02 20:57:33 +00:00
|
|
|
"github.com/opencontainers/go-digest"
|
2023-05-24 23:38:24 +00:00
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
2023-02-02 20:57:33 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2023-10-06 08:14:31 +00:00
|
|
|
const imageNameDanglingPrefix = "moby-dangling@"
|
|
|
|
|
2023-02-02 20:57:33 +00:00
|
|
|
// softImageDelete deletes the image, making sure that there are other images
|
|
|
|
// that reference the content of the deleted image.
|
|
|
|
// If no other image exists, a dangling one is created.
|
2023-11-17 05:55:54 +00:00
|
|
|
func (i *ImageService) softImageDelete(ctx context.Context, img containerdimages.Image, imgs []containerdimages.Image) error {
|
2023-02-02 20:57:33 +00:00
|
|
|
// From this point explicitly ignore the passed context
|
|
|
|
// and don't allow to interrupt operation in the middle.
|
|
|
|
|
|
|
|
// Create dangling image if this is the last image pointing to this target.
|
|
|
|
if len(imgs) == 1 {
|
2023-11-17 05:55:54 +00:00
|
|
|
err := i.ensureDanglingImage(compatcontext.WithoutCancel(ctx), img)
|
2023-02-02 20:57:33 +00:00
|
|
|
|
|
|
|
// Error out in case we couldn't persist the old image.
|
2023-06-29 12:36:22 +00:00
|
|
|
if err != nil {
|
2023-02-02 20:57:33 +00:00
|
|
|
return errdefs.System(errors.Wrapf(err, "failed to create a dangling image for the replaced image %s with digest %s",
|
2023-06-29 12:36:22 +00:00
|
|
|
img.Name, img.Target.Digest.String()))
|
2023-02-02 20:57:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Free the target name.
|
2023-11-17 05:55:54 +00:00
|
|
|
// TODO: Add with target option
|
|
|
|
err := i.images.Delete(compatcontext.WithoutCancel(ctx), img.Name)
|
2023-02-02 20:57:33 +00:00
|
|
|
if err != nil {
|
|
|
|
if !cerrdefs.IsNotFound(err) {
|
|
|
|
return errdefs.System(errors.Wrapf(err, "failed to delete image %s which existed a moment before", img.Name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-29 12:36:22 +00:00
|
|
|
func (i *ImageService) ensureDanglingImage(ctx context.Context, from containerdimages.Image) error {
|
|
|
|
danglingImage := from
|
|
|
|
|
2023-06-30 07:37:45 +00:00
|
|
|
danglingImage.Labels = make(map[string]string)
|
|
|
|
for k, v := range from.Labels {
|
|
|
|
switch k {
|
|
|
|
case containerdimages.AnnotationImageName, ocispec.AnnotationRefName:
|
|
|
|
// Don't copy name labels.
|
|
|
|
default:
|
|
|
|
danglingImage.Labels[k] = v
|
|
|
|
}
|
|
|
|
}
|
2023-06-29 12:36:22 +00:00
|
|
|
danglingImage.Name = danglingImageName(from.Target.Digest)
|
|
|
|
|
2023-11-17 05:55:54 +00:00
|
|
|
_, err := i.images.Create(compatcontext.WithoutCancel(ctx), danglingImage)
|
2023-06-29 12:36:22 +00:00
|
|
|
// If it already exists, then just continue.
|
|
|
|
if cerrdefs.IsAlreadyExists(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-02-02 20:57:33 +00:00
|
|
|
func danglingImageName(digest digest.Digest) string {
|
2023-10-06 08:14:31 +00:00
|
|
|
return imageNameDanglingPrefix + digest.String()
|
2023-02-02 20:57:33 +00:00
|
|
|
}
|
2023-02-22 15:32:17 +00:00
|
|
|
|
|
|
|
func isDanglingImage(image containerdimages.Image) bool {
|
2023-11-17 05:55:54 +00:00
|
|
|
// TODO: Also check for expired
|
2023-02-22 15:32:17 +00:00
|
|
|
return image.Name == danglingImageName(image.Target.Digest)
|
|
|
|
}
|