Skip to content

Commit

Permalink
Merge pull request #9810 from jmguzik/fix-impages-filter-http-api
Browse files Browse the repository at this point in the history
Fix filters list/prune in image http compat/libpod api endpoints
  • Loading branch information
openshift-merge-robot authored Mar 25, 2021
2 parents 8eb3632 + 429a655 commit 9e23e0b
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 27 deletions.
7 changes: 7 additions & 0 deletions libpod/image/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package image

import (
"context"
"strconv"
"strings"

"github.com/containers/podman/v3/libpod/events"
Expand Down Expand Up @@ -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
}
Expand Down
19 changes: 6 additions & 13 deletions pkg/api/handlers/compat/images_prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,29 @@ 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"
)

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
Expand Down
18 changes: 10 additions & 8 deletions pkg/api/handlers/libpod/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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]))
}
}
Expand Down
14 changes: 8 additions & 6 deletions pkg/api/handlers/utils/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -58,26 +59,27 @@ 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
}
var filters = []string{}
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)
Expand Down
49 changes: 49 additions & 0 deletions test/apiv2/10-images.at
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9e23e0b

Please sign in to comment.