mirror of
https://github.com/containers/podman.git
synced 2025-06-23 02:18:13 +08:00
Fix filters in image http compat/libpod api endpoints
Signed-off-by: Jakub Guzik <jakubmguzik@gmail.com>
This commit is contained in:
@ -2,6 +2,7 @@ package image
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/containers/podman/v3/libpod/events"
|
||||
@ -34,6 +35,12 @@ func generatePruneFilterFuncs(filter, filterValue string) (ImageFilter, error) {
|
||||
}
|
||||
return false
|
||||
}, nil
|
||||
case "dangling":
|
||||
danglingImages, err := strconv.ParseBool(filterValue)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "invalid filter dangling=%s", filterValue)
|
||||
}
|
||||
return ImageFilter(DanglingFilter(danglingImages)), nil
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
@ -8,8 +8,8 @@ import (
|
||||
"github.com/containers/podman/v3/libpod"
|
||||
"github.com/containers/podman/v3/pkg/api/handlers"
|
||||
"github.com/containers/podman/v3/pkg/api/handlers/utils"
|
||||
"github.com/containers/podman/v3/pkg/util"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/gorilla/schema"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
@ -17,27 +17,20 @@ func PruneImages(w http.ResponseWriter, r *http.Request) {
|
||||
var (
|
||||
filters []string
|
||||
)
|
||||
decoder := r.Context().Value("decoder").(*schema.Decoder)
|
||||
runtime := r.Context().Value("runtime").(*libpod.Runtime)
|
||||
|
||||
query := struct {
|
||||
All bool
|
||||
Filters map[string][]string `schema:"filters"`
|
||||
}{
|
||||
// This is where you can override the golang default value for one of fields
|
||||
}
|
||||
|
||||
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
|
||||
utils.Error(w, "Something went wrong.", http.StatusBadRequest, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
|
||||
filterMap, err := util.PrepareFilters(r)
|
||||
if err != nil {
|
||||
utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
|
||||
return
|
||||
}
|
||||
|
||||
for k, v := range query.Filters {
|
||||
for k, v := range *filterMap {
|
||||
for _, val := range v {
|
||||
filters = append(filters, fmt.Sprintf("%s=%s", k, val))
|
||||
}
|
||||
}
|
||||
imagePruneReports, err := runtime.ImageRuntime().PruneImages(r.Context(), query.All, filters)
|
||||
imagePruneReports, err := runtime.ImageRuntime().PruneImages(r.Context(), false, filters)
|
||||
if err != nil {
|
||||
utils.InternalServerError(w, err)
|
||||
return
|
||||
|
@ -22,6 +22,7 @@ import (
|
||||
"github.com/containers/podman/v3/pkg/domain/entities"
|
||||
"github.com/containers/podman/v3/pkg/domain/infra/abi"
|
||||
"github.com/containers/podman/v3/pkg/errorhandling"
|
||||
"github.com/containers/podman/v3/pkg/util"
|
||||
utils2 "github.com/containers/podman/v3/utils"
|
||||
"github.com/gorilla/schema"
|
||||
"github.com/pkg/errors"
|
||||
@ -125,31 +126,32 @@ func PruneImages(w http.ResponseWriter, r *http.Request) {
|
||||
runtime := r.Context().Value("runtime").(*libpod.Runtime)
|
||||
decoder := r.Context().Value("decoder").(*schema.Decoder)
|
||||
query := struct {
|
||||
All bool `schema:"all"`
|
||||
Filters map[string][]string `schema:"filters"`
|
||||
All bool `schema:"all"`
|
||||
}{
|
||||
// override any golang type defaults
|
||||
}
|
||||
|
||||
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
|
||||
utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest,
|
||||
filterMap, err := util.PrepareFilters(r)
|
||||
|
||||
if dErr := decoder.Decode(&query, r.URL.Query()); dErr != nil || err != nil {
|
||||
utils.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError,
|
||||
errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
|
||||
return
|
||||
}
|
||||
|
||||
var libpodFilters = []string{}
|
||||
if _, found := r.URL.Query()["filters"]; found {
|
||||
dangling := query.Filters["all"]
|
||||
dangling := (*filterMap)["all"]
|
||||
if len(dangling) > 0 {
|
||||
query.All, err = strconv.ParseBool(query.Filters["all"][0])
|
||||
query.All, err = strconv.ParseBool((*filterMap)["all"][0])
|
||||
if err != nil {
|
||||
utils.InternalServerError(w, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
// dangling is special and not implemented in the libpod side of things
|
||||
delete(query.Filters, "dangling")
|
||||
for k, v := range query.Filters {
|
||||
delete(*filterMap, "dangling")
|
||||
for k, v := range *filterMap {
|
||||
libpodFilters = append(libpodFilters, fmt.Sprintf("%s=%s", k, v[0]))
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ import (
|
||||
"github.com/containers/image/v5/types"
|
||||
"github.com/containers/podman/v3/libpod"
|
||||
"github.com/containers/podman/v3/libpod/image"
|
||||
"github.com/containers/podman/v3/pkg/util"
|
||||
"github.com/gorilla/schema"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
@ -58,13 +59,17 @@ func GetImages(w http.ResponseWriter, r *http.Request) ([]*image.Image, error) {
|
||||
runtime := r.Context().Value("runtime").(*libpod.Runtime)
|
||||
query := struct {
|
||||
All bool
|
||||
Filters map[string][]string `schema:"filters"`
|
||||
Digests bool
|
||||
Filter string // Docker 1.24 compatibility
|
||||
}{
|
||||
// This is where you can override the golang default value for one of fields
|
||||
}
|
||||
|
||||
filterMap, err := util.PrepareFilters(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -72,12 +77,9 @@ func GetImages(w http.ResponseWriter, r *http.Request) ([]*image.Image, error) {
|
||||
if _, found := r.URL.Query()["digests"]; found && query.Digests {
|
||||
UnSupportedParameter("digests")
|
||||
}
|
||||
var (
|
||||
images []*image.Image
|
||||
err error
|
||||
)
|
||||
var images []*image.Image
|
||||
|
||||
queryFilters := query.Filters
|
||||
queryFilters := *filterMap
|
||||
if !IsLibpodRequest(r) && len(query.Filter) > 0 { // Docker 1.24 compatibility
|
||||
if queryFilters == nil {
|
||||
queryFilters = make(map[string][]string)
|
||||
|
@ -77,6 +77,55 @@ for i in $iid ${iid:0:12} $PODMAN_TEST_IMAGE_NAME; do
|
||||
t GET "libpod/images/$i/get?compress=false" 200 '[POSIX tar archive]'
|
||||
done
|
||||
|
||||
#compat api list images sanity checks
|
||||
t GET images/json?filters='garb1age}' 500 \
|
||||
.cause="invalid character 'g' looking for beginning of value"
|
||||
t GET images/json?filters='{"label":["testl' 500 \
|
||||
.cause="unexpected end of JSON input"
|
||||
|
||||
#libpod api list images sanity checks
|
||||
t GET libpod/images/json?filters='garb1age}' 500 \
|
||||
.cause="invalid character 'g' looking for beginning of value"
|
||||
t GET libpod/images/json?filters='{"label":["testl' 500 \
|
||||
.cause="unexpected end of JSON input"
|
||||
|
||||
# Prune images - bad filter input
|
||||
t POST images/prune?filters='garb1age}' 500 \
|
||||
.cause="invalid character 'g' looking for beginning of value"
|
||||
t POST libpod/images/prune?filters='garb1age}' 500 \
|
||||
.cause="invalid character 'g' looking for beginning of value"
|
||||
|
||||
## Prune images with illformed label
|
||||
t POST images/prune?filters='{"label":["tes' 500 \
|
||||
.cause="unexpected end of JSON input"
|
||||
t POST libpod/images/prune?filters='{"label":["tes' 500 \
|
||||
.cause="unexpected end of JSON input"
|
||||
|
||||
|
||||
#create, list and remove dangling image
|
||||
podman image build -t test:test -<<EOF
|
||||
from alpine
|
||||
RUN >file1
|
||||
EOF
|
||||
|
||||
podman image build -t test:test --label xyz -<<EOF
|
||||
from alpine
|
||||
RUN >file2
|
||||
EOF
|
||||
|
||||
t GET images/json?filters='{"dangling":["true"]}' 200 length=1
|
||||
t POST images/prune?filters='{"dangling":["true"]}' 200
|
||||
t GET images/json?filters='{"dangling":["true"]}' 200 length=0
|
||||
|
||||
#label filter check in libpod and compat
|
||||
t GET images/json?filters='{"label":["xyz"]}' 200 length=1
|
||||
t GET libpod/images/json?filters='{"label":["xyz"]}' 200 length=1
|
||||
|
||||
t DELETE libpod/images/test:test 200
|
||||
|
||||
t GET images/json?filters='{"label":["xyz"]}' 200 length=0
|
||||
t GET libpod/images/json?filters='{"label":["xyz"]}' 200 length=0
|
||||
|
||||
# Export more than one image
|
||||
# FIXME FIXME FIXME, this doesn't work:
|
||||
# not ok 64 [10-images] GET images/get?names=alpine,busybox : status
|
||||
|
Reference in New Issue
Block a user