Skip to content

Commit 88c6393

Browse files
authored
Merge pull request #4107 from apostasie/fix-not-found
[FIX]: fix issues in rmi code (including #4104 content digest not found)
2 parents 0cc1f0c + 086cef0 commit 88c6393

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

pkg/cmd/image/remove.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,22 @@ func Remove(ctx context.Context, client *containerd.Client, args []string, optio
7979

8080
if cid, ok := runningImages[found.Image.Name]; ok {
8181
if options.Force {
82-
// FIXME: this is suspicious, but passing the opt seem to break some tests
83-
// if err = is.Delete(ctx, found.Image.Name, delOpts...); err != nil {
84-
if err = is.Delete(ctx, found.Image.Name); err != nil {
82+
// This is a running image, so, we need to keep a ref on it so that containerd does not GC the layers
83+
// First create the new image with an empty name
84+
originalName := found.Image.Name
85+
found.Image.Name = ":"
86+
if _, err = is.Create(ctx, found.Image); err != nil {
8587
return err
8688
}
87-
fmt.Fprintf(options.Stdout, "Untagged: %s\n", found.Image.Name)
88-
fmt.Fprintf(options.Stdout, "Untagged: %s\n", found.Image.Target.Digest.String())
8989

90-
found.Image.Name = ":"
91-
if _, err = is.Create(ctx, found.Image); err != nil {
90+
// Now, delete the original
91+
if err = is.Delete(ctx, originalName, delOpts...); err != nil {
9292
return err
9393
}
94+
95+
fmt.Fprintf(options.Stdout, "Untagged: %s\n", originalName)
96+
fmt.Fprintf(options.Stdout, "Untagged: %s@%s\n", originalName, found.Image.Target.Digest.String())
97+
9498
return nil
9599
}
96100
return fmt.Errorf("conflict: unable to delete %s (cannot be forced) - image is being used by running container %s", found.Req, cid)
@@ -128,18 +132,22 @@ func Remove(ctx context.Context, client *containerd.Client, args []string, optio
128132

129133
if cid, ok := runningImages[found.Image.Name]; ok {
130134
if options.Force {
131-
// FIXME: this is suspicious, but passing the opt seem to break some tests
132-
// if err = is.Delete(ctx, found.Image.Name, delOpts...); err != nil {
133-
if err = is.Delete(ctx, found.Image.Name); err != nil {
135+
// This is a running image, so, we need to keep a ref on it so that containerd does not GC the layers
136+
// First create the new image with an empty name
137+
originalName := found.Image.Name
138+
found.Image.Name = ":"
139+
if _, err = is.Create(ctx, found.Image); err != nil {
134140
return false, err
135141
}
136-
fmt.Fprintf(options.Stdout, "Untagged: %s\n", found.Image.Name)
137-
fmt.Fprintf(options.Stdout, "Untagged: %s\n", found.Image.Target.Digest.String())
138142

139-
found.Image.Name = ":"
140-
if _, err = is.Create(ctx, found.Image); err != nil {
143+
// Now, delete the original
144+
if err = is.Delete(ctx, originalName, delOpts...); err != nil {
141145
return false, err
142146
}
147+
148+
fmt.Fprintf(options.Stdout, "Untagged: %s\n", originalName)
149+
fmt.Fprintf(options.Stdout, "Untagged: %s@%s\n", originalName, found.Image.Target.Digest.String())
150+
143151
return false, nil
144152
}
145153
return false, fmt.Errorf("conflict: unable to delete %s (cannot be forced) - image is being used by running container %s", found.Req, cid)

0 commit comments

Comments
 (0)