Skip to content

Commit 9e23e0b

Browse files
Merge pull request #9810 from jmguzik/fix-impages-filter-http-api
Fix filters list/prune in image http compat/libpod api endpoints
2 parents 8eb3632 + 429a655 commit 9e23e0b

File tree

5 files changed

+80
-27
lines changed

5 files changed

+80
-27
lines changed

libpod/image/prune.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package image
22

33
import (
44
"context"
5+
"strconv"
56
"strings"
67

78
"github.com/containers/podman/v3/libpod/events"
@@ -34,6 +35,12 @@ func generatePruneFilterFuncs(filter, filterValue string) (ImageFilter, error) {
3435
}
3536
return false
3637
}, nil
38+
case "dangling":
39+
danglingImages, err := strconv.ParseBool(filterValue)
40+
if err != nil {
41+
return nil, errors.Wrapf(err, "invalid filter dangling=%s", filterValue)
42+
}
43+
return ImageFilter(DanglingFilter(danglingImages)), nil
3744
}
3845
return nil, nil
3946
}

pkg/api/handlers/compat/images_prune.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,29 @@ import (
88
"github.com/containers/podman/v3/libpod"
99
"github.com/containers/podman/v3/pkg/api/handlers"
1010
"github.com/containers/podman/v3/pkg/api/handlers/utils"
11+
"github.com/containers/podman/v3/pkg/util"
1112
"github.com/docker/docker/api/types"
12-
"github.com/gorilla/schema"
1313
"github.com/pkg/errors"
1414
)
1515

1616
func PruneImages(w http.ResponseWriter, r *http.Request) {
1717
var (
1818
filters []string
1919
)
20-
decoder := r.Context().Value("decoder").(*schema.Decoder)
2120
runtime := r.Context().Value("runtime").(*libpod.Runtime)
2221

23-
query := struct {
24-
All bool
25-
Filters map[string][]string `schema:"filters"`
26-
}{
27-
// This is where you can override the golang default value for one of fields
28-
}
29-
30-
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
31-
utils.Error(w, "Something went wrong.", http.StatusBadRequest, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
22+
filterMap, err := util.PrepareFilters(r)
23+
if err != nil {
24+
utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
3225
return
3326
}
3427

35-
for k, v := range query.Filters {
28+
for k, v := range *filterMap {
3629
for _, val := range v {
3730
filters = append(filters, fmt.Sprintf("%s=%s", k, val))
3831
}
3932
}
40-
imagePruneReports, err := runtime.ImageRuntime().PruneImages(r.Context(), query.All, filters)
33+
imagePruneReports, err := runtime.ImageRuntime().PruneImages(r.Context(), false, filters)
4134
if err != nil {
4235
utils.InternalServerError(w, err)
4336
return

pkg/api/handlers/libpod/images.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/containers/podman/v3/pkg/domain/entities"
2323
"github.com/containers/podman/v3/pkg/domain/infra/abi"
2424
"github.com/containers/podman/v3/pkg/errorhandling"
25+
"github.com/containers/podman/v3/pkg/util"
2526
utils2 "github.com/containers/podman/v3/utils"
2627
"github.com/gorilla/schema"
2728
"github.com/pkg/errors"
@@ -125,31 +126,32 @@ func PruneImages(w http.ResponseWriter, r *http.Request) {
125126
runtime := r.Context().Value("runtime").(*libpod.Runtime)
126127
decoder := r.Context().Value("decoder").(*schema.Decoder)
127128
query := struct {
128-
All bool `schema:"all"`
129-
Filters map[string][]string `schema:"filters"`
129+
All bool `schema:"all"`
130130
}{
131131
// override any golang type defaults
132132
}
133133

134-
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
135-
utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest,
134+
filterMap, err := util.PrepareFilters(r)
135+
136+
if dErr := decoder.Decode(&query, r.URL.Query()); dErr != nil || err != nil {
137+
utils.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError,
136138
errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
137139
return
138140
}
139141

140142
var libpodFilters = []string{}
141143
if _, found := r.URL.Query()["filters"]; found {
142-
dangling := query.Filters["all"]
144+
dangling := (*filterMap)["all"]
143145
if len(dangling) > 0 {
144-
query.All, err = strconv.ParseBool(query.Filters["all"][0])
146+
query.All, err = strconv.ParseBool((*filterMap)["all"][0])
145147
if err != nil {
146148
utils.InternalServerError(w, err)
147149
return
148150
}
149151
}
150152
// dangling is special and not implemented in the libpod side of things
151-
delete(query.Filters, "dangling")
152-
for k, v := range query.Filters {
153+
delete(*filterMap, "dangling")
154+
for k, v := range *filterMap {
153155
libpodFilters = append(libpodFilters, fmt.Sprintf("%s=%s", k, v[0]))
154156
}
155157
}

pkg/api/handlers/utils/images.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/containers/image/v5/types"
1212
"github.com/containers/podman/v3/libpod"
1313
"github.com/containers/podman/v3/libpod/image"
14+
"github.com/containers/podman/v3/pkg/util"
1415
"github.com/gorilla/schema"
1516
"github.com/pkg/errors"
1617
)
@@ -58,26 +59,27 @@ func GetImages(w http.ResponseWriter, r *http.Request) ([]*image.Image, error) {
5859
runtime := r.Context().Value("runtime").(*libpod.Runtime)
5960
query := struct {
6061
All bool
61-
Filters map[string][]string `schema:"filters"`
6262
Digests bool
6363
Filter string // Docker 1.24 compatibility
6464
}{
6565
// This is where you can override the golang default value for one of fields
6666
}
6767

68+
filterMap, err := util.PrepareFilters(r)
69+
if err != nil {
70+
return nil, err
71+
}
72+
6873
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
6974
return nil, err
7075
}
7176
var filters = []string{}
7277
if _, found := r.URL.Query()["digests"]; found && query.Digests {
7378
UnSupportedParameter("digests")
7479
}
75-
var (
76-
images []*image.Image
77-
err error
78-
)
80+
var images []*image.Image
7981

80-
queryFilters := query.Filters
82+
queryFilters := *filterMap
8183
if !IsLibpodRequest(r) && len(query.Filter) > 0 { // Docker 1.24 compatibility
8284
if queryFilters == nil {
8385
queryFilters = make(map[string][]string)

test/apiv2/10-images.at

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,55 @@ for i in $iid ${iid:0:12} $PODMAN_TEST_IMAGE_NAME; do
7777
t GET "libpod/images/$i/get?compress=false" 200 '[POSIX tar archive]'
7878
done
7979

80+
#compat api list images sanity checks
81+
t GET images/json?filters='garb1age}' 500 \
82+
.cause="invalid character 'g' looking for beginning of value"
83+
t GET images/json?filters='{"label":["testl' 500 \
84+
.cause="unexpected end of JSON input"
85+
86+
#libpod api list images sanity checks
87+
t GET libpod/images/json?filters='garb1age}' 500 \
88+
.cause="invalid character 'g' looking for beginning of value"
89+
t GET libpod/images/json?filters='{"label":["testl' 500 \
90+
.cause="unexpected end of JSON input"
91+
92+
# Prune images - bad filter input
93+
t POST images/prune?filters='garb1age}' 500 \
94+
.cause="invalid character 'g' looking for beginning of value"
95+
t POST libpod/images/prune?filters='garb1age}' 500 \
96+
.cause="invalid character 'g' looking for beginning of value"
97+
98+
## Prune images with illformed label
99+
t POST images/prune?filters='{"label":["tes' 500 \
100+
.cause="unexpected end of JSON input"
101+
t POST libpod/images/prune?filters='{"label":["tes' 500 \
102+
.cause="unexpected end of JSON input"
103+
104+
105+
#create, list and remove dangling image
106+
podman image build -t test:test -<<EOF
107+
from alpine
108+
RUN >file1
109+
EOF
110+
111+
podman image build -t test:test --label xyz -<<EOF
112+
from alpine
113+
RUN >file2
114+
EOF
115+
116+
t GET images/json?filters='{"dangling":["true"]}' 200 length=1
117+
t POST images/prune?filters='{"dangling":["true"]}' 200
118+
t GET images/json?filters='{"dangling":["true"]}' 200 length=0
119+
120+
#label filter check in libpod and compat
121+
t GET images/json?filters='{"label":["xyz"]}' 200 length=1
122+
t GET libpod/images/json?filters='{"label":["xyz"]}' 200 length=1
123+
124+
t DELETE libpod/images/test:test 200
125+
126+
t GET images/json?filters='{"label":["xyz"]}' 200 length=0
127+
t GET libpod/images/json?filters='{"label":["xyz"]}' 200 length=0
128+
80129
# Export more than one image
81130
# FIXME FIXME FIXME, this doesn't work:
82131
# not ok 64 [10-images] GET images/get?names=alpine,busybox : status

0 commit comments

Comments
 (0)