summaryrefslogtreecommitdiff
path: root/libpod/runtime_img.go
diff options
context:
space:
mode:
authorTomSweeneyRedHat <tsweeney@redhat.com>2018-03-20 13:13:09 -0400
committerAtomic Bot <atomic-devel@projectatomic.io>2018-04-05 15:44:11 +0000
commit54bd0ae71671084c53bdd7e675e23c657d410280 (patch)
tree8611cc6e711f9760234669d6a3731aa345f9c18e /libpod/runtime_img.go
parentfdcf633a33bbbfbc99268965ef5da03a4770619b (diff)
downloadpodman-54bd0ae71671084c53bdd7e675e23c657d410280.tar.gz
podman-54bd0ae71671084c53bdd7e675e23c657d410280.tar.bz2
podman-54bd0ae71671084c53bdd7e675e23c657d410280.zip
Remove image via storage if a buildah container is associated
Signed-off-by: TomSweeneyRedHat <tsweeney@redhat.com> Closes: #522 Approved by: mheon
Diffstat (limited to 'libpod/runtime_img.go')
-rw-r--r--libpod/runtime_img.go56
1 files changed, 55 insertions, 1 deletions
diff --git a/libpod/runtime_img.go b/libpod/runtime_img.go
index b891fcde9..ef1791884 100644
--- a/libpod/runtime_img.go
+++ b/libpod/runtime_img.go
@@ -12,6 +12,7 @@ import (
"github.com/containers/image/pkg/sysregistries"
"github.com/containers/image/tarball"
"github.com/containers/image/types"
+ "github.com/containers/storage"
"github.com/containers/storage/pkg/archive"
ociv1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
@@ -123,8 +124,61 @@ func (r *Runtime) RemoveImage(image *image.Image, force bool) (string, error) {
// reponames and no force is applied, we error out.
return "", fmt.Errorf("unable to delete %s (must force) - image is referred to in multiple tags", image.ID())
}
+ err = image.Remove(force)
+ if err != nil && errors.Cause(err) == storage.ErrImageUsedByContainer {
+ if errStorage := r.rmStorageContainers(force, image); errStorage == nil {
+ // Containers associated with the image should be deleted now,
+ // let's try removing the image again.
+ err = image.Remove(force)
+ } else {
+ err = errStorage
+ }
+ }
+ return image.ID(), err
+}
- return image.ID(), image.Remove(force)
+// Remove containers that are in storage rather than Podman.
+func (r *Runtime) rmStorageContainers(force bool, image *image.Image) error {
+ ctrIDs, err := storageContainers(image.ID(), r.store)
+ if err != nil {
+ return errors.Wrapf(err, "error getting containers for image %q", image.ID())
+ }
+
+ if len(ctrIDs) > 0 && !force {
+ return storage.ErrImageUsedByContainer
+ }
+
+ if len(ctrIDs) > 0 && force {
+ if err = removeStorageContainers(ctrIDs, r.store); err != nil {
+ return errors.Wrapf(err, "error removing containers %v for image %q", ctrIDs, image.ID())
+ }
+ }
+ return nil
+}
+
+// Returns a list of storage containers associated with the given ImageReference
+func storageContainers(imageID string, store storage.Store) ([]string, error) {
+ ctrIDs := []string{}
+ containers, err := store.Containers()
+ if err != nil {
+ return nil, err
+ }
+ for _, ctr := range containers {
+ if ctr.ImageID == imageID {
+ ctrIDs = append(ctrIDs, ctr.ID)
+ }
+ }
+ return ctrIDs, nil
+}
+
+// Removes the containers passed in the array.
+func removeStorageContainers(ctrIDs []string, store storage.Store) error {
+ for _, ctrID := range ctrIDs {
+ if err := store.DeleteContainer(ctrID); err != nil {
+ return errors.Wrapf(err, "could not remove container %q", ctrID)
+ }
+ }
+ return nil
}
// GetRegistries gets the searchable registries from the global registration file.