Skip to content

Commit

Permalink
Merge pull request #9711 from jmguzik/volume-prune-fix-http-compat
Browse files Browse the repository at this point in the history
Fix for volumes prune in http compat api when using filters
  • Loading branch information
openshift-merge-robot authored Mar 16, 2021
2 parents 0f954e2 + f4b2d59 commit 604459b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
34 changes: 17 additions & 17 deletions pkg/api/handlers/compat/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"net/http"
"net/url"
"strings"
"time"

"github.com/containers/podman/v3/libpod"
Expand All @@ -31,7 +32,7 @@ func ListVolumes(w http.ResponseWriter, r *http.Request) {
}

if err := decoder.Decode(&query, r.URL.Query()); err != nil {
utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest,
utils.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError,
errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
return
}
Expand All @@ -40,7 +41,7 @@ func ListVolumes(w http.ResponseWriter, r *http.Request) {
// happily parse them for us.
for filter := range query.Filters {
if filter == "opts" {
utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest,
utils.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError,
errors.Errorf("unsupported libpod filters passed to docker endpoint"))
return
}
Expand Down Expand Up @@ -90,7 +91,7 @@ func CreateVolume(w http.ResponseWriter, r *http.Request) {
/* No query string data*/
query := struct{}{}
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest,
utils.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError,
errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
return
}
Expand Down Expand Up @@ -218,7 +219,7 @@ func RemoveVolume(w http.ResponseWriter, r *http.Request) {
}

if err := decoder.Decode(&query, r.URL.Query()); err != nil {
utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest,
utils.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError,
errors.Wrapf(err, "failed to parse parameters for %s", r.URL.String()))
return
}
Expand Down Expand Up @@ -263,25 +264,24 @@ func RemoveVolume(w http.ResponseWriter, r *http.Request) {
func PruneVolumes(w http.ResponseWriter, r *http.Request) {
var (
runtime = r.Context().Value("runtime").(*libpod.Runtime)
decoder = r.Context().Value("decoder").(*schema.Decoder)
)
// For some reason the prune filters are query parameters even though this
// is a POST endpoint
query := struct {
Filters map[string][]string `schema:"filters"`
}{
// override any golang type defaults
}

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()))
filtersList, err := filtersFromRequest(r)
if err != nil {
utils.Error(w, "Something went wrong.", http.StatusInternalServerError, errors.Wrap(err, "Decode()"))
return
}
filterMap := map[string][]string{}
for _, filter := range filtersList {
split := strings.SplitN(filter, "=", 2)
if len(split) > 1 {
filterMap[split[0]] = append(filterMap[split[0]], split[1])
}
}

f := (url.Values)(query.Filters)
f := (url.Values)(filterMap)
filterFuncs, err := filters.GenerateVolumeFilters(f)
if err != nil {
utils.Error(w, "Something when wrong.", http.StatusBadRequest, errors.Wrapf(err, "failed to parse filters for %s", f.Encode()))
utils.Error(w, "Something when wrong.", http.StatusInternalServerError, errors.Wrapf(err, "failed to parse filters for %s", f.Encode()))
return
}

Expand Down
4 changes: 4 additions & 0 deletions test/apiv2/30-volumes.at
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ t DELETE libpod/volumes/foo1 404 \
.message~.* \
.response=404

# Prune volumes - bad filter input
t POST volumes/prune?filters='garb1age}' 500 \
.cause="invalid character 'g' looking for beginning of value"

## Prune volumes with label matching 'testlabel1=testonly'
t POST libpod/volumes/prune?filters='{"label":["testlabel1=testonly"]}' 200
t GET libpod/volumes/json?filters='{"label":["testlabel1=testonly"]}' 200 length=0
Expand Down

0 comments on commit 604459b

Please sign in to comment.