Browse code

c8d/delete: Extract untagReferences

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

Paweł Gronowski authored on 2025/04/17 22:30:27
Showing 1 changed files
... ...
@@ -96,20 +96,7 @@ func (i *ImageService) ImageDelete(ctx context.Context, imageRef string, force,
96 96
 			c &= ^conflictActiveReference
97 97
 		}
98 98
 		if named != nil && len(sameRef) > 0 && len(sameRef) != len(all) {
99
-			var records []imagetypes.DeleteResponse
100
-			for _, ref := range sameRef {
101
-				// TODO: Add with target
102
-				err := i.images.Delete(ctx, ref.Name)
103
-				if err != nil {
104
-					return nil, err
105
-				}
106
-				if nn, err := reference.ParseNormalizedNamed(ref.Name); err == nil {
107
-					familiarRef := reference.FamiliarString(nn)
108
-					i.logImageEvent(ref, familiarRef, events.ActionUnTag)
109
-					records = append(records, imagetypes.DeleteResponse{Untagged: familiarRef})
110
-				}
111
-			}
112
-			return records, nil
99
+			return i.untagReferences(ctx, sameRef)
113 100
 		}
114 101
 	} else {
115 102
 		imgID = image.ID(img.Target.Digest)
... ...
@@ -127,20 +114,7 @@ func (i *ImageService) ImageDelete(ctx context.Context, imageRef string, force,
127 127
 			return nil, err
128 128
 		}
129 129
 		if len(sameRef) != len(all) {
130
-			var records []imagetypes.DeleteResponse
131
-			for _, ref := range sameRef {
132
-				// TODO: Add with target
133
-				err := i.images.Delete(ctx, ref.Name)
134
-				if err != nil {
135
-					return nil, err
136
-				}
137
-				if nn, err := reference.ParseNormalizedNamed(ref.Name); err == nil {
138
-					familiarRef := reference.FamiliarString(nn)
139
-					i.logImageEvent(ref, familiarRef, events.ActionUnTag)
140
-					records = append(records, imagetypes.DeleteResponse{Untagged: familiarRef})
141
-				}
142
-			}
143
-			return records, nil
130
+			return i.untagReferences(ctx, sameRef)
144 131
 		} else if len(all) > 1 && !force {
145 132
 			// Since only a single used reference, remove all active
146 133
 			// TODO: Consider keeping the conflict and changing active
... ...
@@ -411,6 +385,25 @@ func (*imageDeleteConflict) Conflict() {}
411 411
 // nil if there are none. It takes a bitmask representing a
412 412
 // filter for which conflict types the caller cares about,
413 413
 // and will only check for these conflict types.
414
+
415
+// untagReferences deletes the given image references and returns the appropriate response records
416
+func (i *ImageService) untagReferences(ctx context.Context, refs []c8dimages.Image) ([]imagetypes.DeleteResponse, error) {
417
+	var records []imagetypes.DeleteResponse
418
+	for _, ref := range refs {
419
+		// TODO: Add with target
420
+		err := i.images.Delete(ctx, ref.Name)
421
+		if err != nil {
422
+			return nil, err
423
+		}
424
+		if nn, err := reference.ParseNormalizedNamed(ref.Name); err == nil {
425
+			familiarRef := reference.FamiliarString(nn)
426
+			i.logImageEvent(ref, familiarRef, events.ActionUnTag)
427
+			records = append(records, imagetypes.DeleteResponse{Untagged: familiarRef})
428
+		}
429
+	}
430
+	return records, nil
431
+}
432
+
414 433
 func (i *ImageService) checkImageDeleteConflict(ctx context.Context, imgID image.ID, all []c8dimages.Image, mask conflictType) error {
415 434
 	if mask&conflictRunningContainer != 0 {
416 435
 		running := func(c *container.Container) bool {