Merge pull request #8178 from rhatdan/exists

NewFromLocal can return multiple images
This commit is contained in:
OpenShift Merge Robot
2020-10-29 17:04:05 +01:00
committed by GitHub
4 changed files with 19 additions and 4 deletions

View File

@ -14,6 +14,9 @@ var (
// ErrNoSuchImage indicates the requested image does not exist // ErrNoSuchImage indicates the requested image does not exist
ErrNoSuchImage = errors.New("no such image") ErrNoSuchImage = errors.New("no such image")
// ErrMultipleImages found multiple name and tag matches
ErrMultipleImages = errors.New("found multiple name and tag matches")
// ErrNoSuchTag indicates the requested image tag does not exist // ErrNoSuchTag indicates the requested image tag does not exist
ErrNoSuchTag = errors.New("no such tag") ErrNoSuchTag = errors.New("no such tag")

View File

@ -11,6 +11,7 @@ import (
"github.com/containers/image/v5/docker/reference" "github.com/containers/image/v5/docker/reference"
"github.com/containers/image/v5/signature" "github.com/containers/image/v5/signature"
"github.com/containers/image/v5/types" "github.com/containers/image/v5/types"
"github.com/containers/podman/v2/libpod/define"
"github.com/containers/storage" "github.com/containers/storage"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -42,7 +43,7 @@ func findImageInRepotags(search imageParts, images []*Image) (*storage.Image, er
if len(results) == 0 { if len(results) == 0 {
return &storage.Image{}, errors.Errorf("unable to find a name and tag match for %s in repotags", searchName) return &storage.Image{}, errors.Errorf("unable to find a name and tag match for %s in repotags", searchName)
} else if len(results) > 1 { } else if len(results) > 1 {
return &storage.Image{}, errors.Errorf("found multiple name and tag matches for %s in repotags", searchName) return &storage.Image{}, errors.Wrapf(define.ErrMultipleImages, searchName)
} }
return results[0], nil return results[0], nil
} }

View File

@ -44,11 +44,16 @@ func ImageExists(w http.ResponseWriter, r *http.Request) {
runtime := r.Context().Value("runtime").(*libpod.Runtime) runtime := r.Context().Value("runtime").(*libpod.Runtime)
name := utils.GetName(r) name := utils.GetName(r)
_, err := runtime.ImageRuntime().NewFromLocal(name) ir := abi.ImageEngine{Libpod: runtime}
report, err := ir.Exists(r.Context(), name)
if err != nil { if err != nil {
utils.Error(w, "Something went wrong.", http.StatusNotFound, errors.Wrapf(err, "failed to find image %s", name)) utils.Error(w, "Something went wrong.", http.StatusNotFound, errors.Wrapf(err, "failed to find image %s", name))
return return
} }
if !report.Value {
utils.Error(w, "Something went wrong.", http.StatusNotFound, errors.Wrapf(nil, "failed to find image %s", name))
return
}
utils.WriteResponse(w, http.StatusNoContent, "") utils.WriteResponse(w, http.StatusNoContent, "")
} }

View File

@ -39,8 +39,14 @@ const SignatureStoreDir = "/var/lib/containers/sigstore"
func (ir *ImageEngine) Exists(_ context.Context, nameOrID string) (*entities.BoolReport, error) { func (ir *ImageEngine) Exists(_ context.Context, nameOrID string) (*entities.BoolReport, error) {
_, err := ir.Libpod.ImageRuntime().NewFromLocal(nameOrID) _, err := ir.Libpod.ImageRuntime().NewFromLocal(nameOrID)
if err != nil && errors.Cause(err) != define.ErrNoSuchImage { if err != nil {
return nil, err if errors.Cause(err) == define.ErrMultipleImages {
return &entities.BoolReport{Value: true}, nil
} else {
if errors.Cause(err) != define.ErrNoSuchImage {
return nil, err
}
}
} }
return &entities.BoolReport{Value: err == nil}, nil return &entities.BoolReport{Value: err == nil}, nil
} }