soft_delete.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package containerd
  2. import (
  3. "context"
  4. cerrdefs "github.com/containerd/containerd/errdefs"
  5. containerdimages "github.com/containerd/containerd/images"
  6. "github.com/docker/docker/errdefs"
  7. "github.com/opencontainers/go-digest"
  8. ocispec "github.com/opencontainers/image-spec/specs-go/v1"
  9. "github.com/pkg/errors"
  10. )
  11. // softImageDelete deletes the image, making sure that there are other images
  12. // that reference the content of the deleted image.
  13. // If no other image exists, a dangling one is created.
  14. func (i *ImageService) softImageDelete(ctx context.Context, img containerdimages.Image) error {
  15. is := i.client.ImageService()
  16. // If the image already exists, persist it as dangling image
  17. // but only if no other image has the same target.
  18. dgst := img.Target.Digest.String()
  19. imgs, err := is.List(ctx, "target.digest=="+dgst)
  20. if err != nil {
  21. return errdefs.System(errors.Wrapf(err, "failed to check if there are images targeting digest %s", dgst))
  22. }
  23. // From this point explicitly ignore the passed context
  24. // and don't allow to interrupt operation in the middle.
  25. // Create dangling image if this is the last image pointing to this target.
  26. if len(imgs) == 1 {
  27. err = i.ensureDanglingImage(context.Background(), img)
  28. // Error out in case we couldn't persist the old image.
  29. if err != nil {
  30. return errdefs.System(errors.Wrapf(err, "failed to create a dangling image for the replaced image %s with digest %s",
  31. img.Name, img.Target.Digest.String()))
  32. }
  33. }
  34. // Free the target name.
  35. err = is.Delete(context.Background(), img.Name)
  36. if err != nil {
  37. if !cerrdefs.IsNotFound(err) {
  38. return errdefs.System(errors.Wrapf(err, "failed to delete image %s which existed a moment before", img.Name))
  39. }
  40. }
  41. return nil
  42. }
  43. func (i *ImageService) ensureDanglingImage(ctx context.Context, from containerdimages.Image) error {
  44. danglingImage := from
  45. danglingImage.Labels = make(map[string]string)
  46. for k, v := range from.Labels {
  47. switch k {
  48. case containerdimages.AnnotationImageName, ocispec.AnnotationRefName:
  49. // Don't copy name labels.
  50. default:
  51. danglingImage.Labels[k] = v
  52. }
  53. }
  54. danglingImage.Name = danglingImageName(from.Target.Digest)
  55. _, err := i.client.ImageService().Create(context.Background(), danglingImage)
  56. // If it already exists, then just continue.
  57. if cerrdefs.IsAlreadyExists(err) {
  58. return nil
  59. }
  60. return err
  61. }
  62. func danglingImageName(digest digest.Digest) string {
  63. return "moby-dangling@" + digest.String()
  64. }
  65. func isDanglingImage(image containerdimages.Image) bool {
  66. return image.Name == danglingImageName(image.Target.Digest)
  67. }