remove image podman no prune

Signed-off-by: Karthik Elango <kelango@redhat.com>
This commit is contained in:
Karthik Elango 2022-07-20 16:23:13 -04:00
parent b0ef621ebf
commit cc8e4d5fec
9 changed files with 106 additions and 3 deletions

View File

@ -61,6 +61,7 @@ func imageRemoveFlagSet(flags *pflag.FlagSet) {
flags.BoolVarP(&imageOpts.All, "all", "a", false, "Remove all images")
flags.BoolVarP(&imageOpts.Ignore, "ignore", "i", false, "Ignore errors if a specified image does not exist")
flags.BoolVarP(&imageOpts.Force, "force", "f", false, "Force Removal of the image")
flags.BoolVar(&imageOpts.NoPrune, "no-prune", false, "Do not remove dangling images")
}
func rm(cmd *cobra.Command, args []string) error {

View File

@ -28,6 +28,9 @@ This option will cause podman to remove all containers that are using the image
If a specified image does not exist in the local storage, ignore it and do not throw an error.
#### **--no-prune**
This options will not remove dangling parents of specified image
Remove an image by its short ID
```

View File

@ -547,6 +547,7 @@ func ImagesBatchRemove(w http.ResponseWriter, r *http.Request) {
Ignore bool `schema:"ignore"`
LookupManifest bool `schema:"lookupManifest"`
Images []string `schema:"images"`
NoPrune bool `schema:"noprune"`
}{}
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
@ -554,7 +555,7 @@ func ImagesBatchRemove(w http.ResponseWriter, r *http.Request) {
return
}
opts := entities.ImageRemoveOptions{All: query.All, Force: query.Force, Ignore: query.Ignore, LookupManifest: query.LookupManifest}
opts := entities.ImageRemoveOptions{All: query.All, Force: query.Force, Ignore: query.Ignore, LookupManifest: query.LookupManifest, NoPrune: query.NoPrune}
imageEngine := abi.ImageEngine{Libpod: runtime}
rmReport, rmErrors := imageEngine.Remove(r.Context(), query.Images, opts)
strErrs := errorhandling.ErrorsToStrings(rmErrors)

View File

@ -15,6 +15,8 @@ type RemoveOptions struct {
Ignore *bool
// Confirms if given name is a manifest list and removes it, otherwise returns error.
LookupManifest *bool
// Does not remove dangling parent images
NoPrune *bool
}
//go:generate go run ../generator/generator.go DiffOptions

View File

@ -76,3 +76,18 @@ func (o *RemoveOptions) GetLookupManifest() bool {
}
return *o.LookupManifest
}
// WithNoPrune set field NoPrune to given value
func (o *RemoveOptions) WithNoPrune(value bool) *RemoveOptions {
o.NoPrune = &value
return o
}
// GetNoPrune returns value of field NoPrune
func (o *RemoveOptions) GetNoPrune() bool {
if o.NoPrune == nil {
var z bool
return z
}
return *o.NoPrune
}

View File

@ -94,6 +94,8 @@ type ImageRemoveOptions struct {
Ignore bool
// Confirms if given name is a manifest list and removes it, otherwise returns error.
LookupManifest bool
// NoPrune will not remove dangling images
NoPrune bool
}
// ImageRemoveReport is the response for removing one or more image(s) from storage

View File

@ -565,6 +565,7 @@ func (ir *ImageEngine) Remove(ctx context.Context, images []string, opts entitie
libimageOptions.Force = opts.Force
libimageOptions.Ignore = opts.Ignore
libimageOptions.LookupManifest = opts.LookupManifest
libimageOptions.NoPrune = opts.NoPrune
if !opts.All {
libimageOptions.Filters = append(libimageOptions.Filters, "intermediate=false")
}
@ -581,7 +582,7 @@ func (ir *ImageEngine) Remove(ctx context.Context, images []string, opts entitie
rmErrors = libimageErrors
return
return report, rmErrors
}
// Shutdown Libpod engine

View File

@ -28,7 +28,7 @@ func (ir *ImageEngine) Exists(_ context.Context, nameOrID string) (*entities.Boo
}
func (ir *ImageEngine) Remove(ctx context.Context, imagesArg []string, opts entities.ImageRemoveOptions) (*entities.ImageRemoveReport, []error) {
options := new(images.RemoveOptions).WithForce(opts.Force).WithIgnore(opts.Ignore).WithAll(opts.All).WithLookupManifest(opts.LookupManifest)
options := new(images.RemoveOptions).WithForce(opts.Force).WithIgnore(opts.Ignore).WithAll(opts.All).WithLookupManifest(opts.LookupManifest).WithNoPrune(opts.NoPrune)
return images.Remove(ir.ClientCtx, imagesArg, options)
}

View File

@ -307,4 +307,82 @@ RUN touch %s`, CIRROS_IMAGE, imageName)
}
wg.Wait()
})
It("podman rmi --no-prune with dangling parents", func() {
podmanTest.AddImageToRWStore(ALPINE)
session := podmanTest.Podman([]string{"create", "--name", "c_test1", ALPINE, "true"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
session = podmanTest.Podman([]string{"commit", "-q", "c_test1", "test1"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
session = podmanTest.Podman([]string{"create", "--name", "c_test2", "test1", "true"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
session = podmanTest.Podman([]string{"commit", "-q", "c_test2", "test2"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
imageID2 := session.OutputToString()
session = podmanTest.Podman([]string{"create", "--name", "c_test3", "test2", "true"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
session = podmanTest.Podman([]string{"commit", "-q", "c_test3", "test3"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
imageID3 := session.OutputToString()
session = podmanTest.Podman([]string{"untag", "test2"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
session = podmanTest.Podman([]string{"untag", "test1"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
session = podmanTest.Podman([]string{"rmi", "-f", "--no-prune", "test3"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
Expect(session.OutputToString()).To(ContainSubstring(imageID3))
Expect(session.OutputToString()).NotTo(ContainSubstring(imageID2))
})
It("podman rmi --no-prune with undangling parents", func() {
podmanTest.AddImageToRWStore(ALPINE)
session := podmanTest.Podman([]string{"create", "--name", "c_test1", ALPINE, "true"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
session = podmanTest.Podman([]string{"commit", "-q", "c_test1", "test1"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
session = podmanTest.Podman([]string{"create", "--name", "c_test2", "test1", "true"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
session = podmanTest.Podman([]string{"commit", "-q", "c_test2", "test2"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
imageID2 := session.OutputToString()
session = podmanTest.Podman([]string{"create", "--name", "c_test3", "test2", "true"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
session = podmanTest.Podman([]string{"commit", "-q", "c_test3", "test3"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
imageID3 := session.OutputToString()
session = podmanTest.Podman([]string{"rmi", "-f", "--no-prune", "test3"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
Expect(session.OutputToString()).To(ContainSubstring(imageID3))
Expect(session.OutputToString()).NotTo(ContainSubstring(imageID2))
})
})