Skip to content

Commit

Permalink
Merge pull request #2074 from xiaoxubeii/fix-remove-image
Browse files Browse the repository at this point in the history
bugfix: cannot remove image reference by specified reference when other references are used
  • Loading branch information
Wei Fu authored Aug 11, 2018
2 parents fcd1653 + 4b7a938 commit aae4a16
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
23 changes: 16 additions & 7 deletions apis/server/image_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/alibaba/pouch/pkg/httputils"

"github.com/gorilla/mux"
"github.com/opencontainers/go-digest"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -99,18 +100,26 @@ func (s *Server) removeImage(ctx context.Context, rw http.ResponseWriter, req *h
return err
}

containers, err := s.ContainerMgr.List(ctx, &mgr.ContainerListOption{
All: true,
FilterFunc: func(c *mgr.Container) bool {
return c.Image == image.ID
}})
refs, err := s.ImageMgr.ListReferences(ctx, digest.Digest(image.ID))
if err != nil {
return err
}

isForce := httputils.BoolValue(req, "force")
if !isForce && len(containers) > 0 {
return fmt.Errorf("Unable to remove the image %q (must force) - container (%s, %s) is using this image", image.ID, containers[0].ID, containers[0].Name)
// We only should check the image whether used by container when there is only one primary reference.
if len(refs) == 1 {
containers, err := s.ContainerMgr.List(ctx, &mgr.ContainerListOption{
All: true,
FilterFunc: func(c *mgr.Container) bool {
return c.Image == image.ID
}})
if err != nil {
return err
}

if !isForce && len(containers) > 0 {
return fmt.Errorf("Unable to remove the image %q (must force) - container (%s, %s) is using this image", image.ID, containers[0].ID, containers[0].Name)
}
}

if err := s.ImageMgr.RemoveImage(ctx, name, isForce); err != nil {
Expand Down
11 changes: 10 additions & 1 deletion daemon/mgr/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

"github.com/containerd/containerd"
ctrdmetaimages "github.com/containerd/containerd/images"
digest "github.com/opencontainers/go-digest"
"github.com/opencontainers/go-digest"
pkgerrors "github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -47,6 +47,9 @@ type ImageMgr interface {
// CheckReference returns imageID, actual reference and primary reference.
CheckReference(ctx context.Context, idOrRef string) (digest.Digest, reference.Named, reference.Named, error)

// ListReferences returns all references
ListReferences(ctx context.Context, imageID digest.Digest) ([]reference.Named, error)

// LoadImage creates a set of images by tarstream.
LoadImage(ctx context.Context, imageName string, tarstream io.ReadCloser) error

Expand Down Expand Up @@ -340,6 +343,12 @@ func (mgr *ImageManager) CheckReference(ctx context.Context, idOrRef string) (ac
return
}

// ListReferences returns all references
func (mgr *ImageManager) ListReferences(ctx context.Context, imageID digest.Digest) ([]reference.Named, error) {
// NOTE: we just keep ctx and error for further expansion
return mgr.localStore.GetPrimaryReferences(imageID), nil
}

// updateLocalStore updates the local store.
func (mgr *ImageManager) updateLocalStore() error {
ctx, cancel := context.WithTimeout(context.Background(), deadlineLoadImagesAtBootup)
Expand Down

0 comments on commit aae4a16

Please sign in to comment.