Browse code

c8d/softDelete: Extract ensureDanglingImage

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>

Paweł Gronowski authored on 2023/06/29 21:36:22
Showing 1 changed files
... ...
@@ -30,19 +30,12 @@ func (i *ImageService) softImageDelete(ctx context.Context, img containerdimages
30 30
 
31 31
 	// Create dangling image if this is the last image pointing to this target.
32 32
 	if len(imgs) == 1 {
33
-		danglingImage := img
34
-
35
-		danglingImage.Name = danglingImageName(img.Target.Digest)
36
-		delete(danglingImage.Labels, containerdimages.AnnotationImageName)
37
-		delete(danglingImage.Labels, ocispec.AnnotationRefName)
38
-
39
-		_, err = is.Create(context.Background(), danglingImage)
33
+		err = i.ensureDanglingImage(context.Background(), img)
40 34
 
41 35
 		// Error out in case we couldn't persist the old image.
42
-		// If it already exists, then just continue.
43
-		if err != nil && !cerrdefs.IsAlreadyExists(err) {
36
+		if err != nil {
44 37
 			return errdefs.System(errors.Wrapf(err, "failed to create a dangling image for the replaced image %s with digest %s",
45
-				danglingImage.Name, danglingImage.Target.Digest.String()))
38
+				img.Name, img.Target.Digest.String()))
46 39
 		}
47 40
 	}
48 41
 
... ...
@@ -57,6 +50,22 @@ func (i *ImageService) softImageDelete(ctx context.Context, img containerdimages
57 57
 	return nil
58 58
 }
59 59
 
60
+func (i *ImageService) ensureDanglingImage(ctx context.Context, from containerdimages.Image) error {
61
+	danglingImage := from
62
+
63
+	danglingImage.Name = danglingImageName(from.Target.Digest)
64
+	delete(danglingImage.Labels, containerdimages.AnnotationImageName)
65
+	delete(danglingImage.Labels, ocispec.AnnotationRefName)
66
+
67
+	_, err := i.client.ImageService().Create(context.Background(), danglingImage)
68
+	// If it already exists, then just continue.
69
+	if cerrdefs.IsAlreadyExists(err) {
70
+		return nil
71
+	}
72
+
73
+	return err
74
+}
75
+
60 76
 func danglingImageName(digest digest.Digest) string {
61 77
 	return "moby-dangling@" + digest.String()
62 78
 }