Skip to content

Commit

Permalink
Volumes prune endpoint should use only prune filters
Browse files Browse the repository at this point in the history
Volumes endpoints for HTTP compat and libpod APIs allowed
usage of list HTTP endpoint filter funcs. Documentation in
case of compat API does not allow that. This commit aligns
code with the documentation and also ligns libpod with compat API.

Signed-off-by: Jakub Guzik <[email protected]>
  • Loading branch information
jmguzik authored and mheon committed Apr 16, 2021
1 parent 850ead4 commit 3338901
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/api/handlers/compat/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func PruneVolumes(w http.ResponseWriter, r *http.Request) {
}

f := (url.Values)(*filterMap)
filterFuncs, err := filters.GenerateVolumeFilters(f)
filterFuncs, err := filters.GeneratePruneVolumeFilters(f)
if err != nil {
utils.Error(w, "Something when wrong.", http.StatusInternalServerError, errors.Wrapf(err, "failed to parse filters for %s", f.Encode()))
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/handlers/libpod/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func pruneVolumesHelper(r *http.Request) ([]*reports.PruneReport, error) {
}

f := (url.Values)(*filterMap)
filterFuncs, err := filters.GenerateVolumeFilters(f)
filterFuncs, err := filters.GeneratePruneVolumeFilters(f)
if err != nil {
return nil, err
}
Expand Down
20 changes: 19 additions & 1 deletion pkg/domain/filters/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,25 @@ func GenerateVolumeFilters(filters url.Values) ([]libpod.VolumeFilter, error) {
return dangling
})
default:
return nil, errors.Errorf("%q is in an invalid volume filter", filter)
return nil, errors.Errorf("%q is an invalid volume filter", filter)
}
}
}
return vf, nil
}

func GeneratePruneVolumeFilters(filters url.Values) ([]libpod.VolumeFilter, error) {
var vf []libpod.VolumeFilter
for filter, v := range filters {
for _, val := range v {
switch filter {
case "label":
filter := val
vf = append(vf, func(v *libpod.Volume) bool {
return util.MatchLabelFilters([]string{filter}, v.Labels())
})
default:
return nil, errors.Errorf("%q is an invalid volume filter", filter)
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions test/apiv2/30-volumes.at
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,20 @@ t POST libpod/volumes/prune 200
#After prune volumes, there should be no volume existing
t GET libpod/volumes/json 200 length=0

# libpod api: do not use list filters for prune
t POST libpod/volumes/prune?filters='{"name":["anyname"]}' 500 \
.cause="\"name\" is an invalid volume filter"
t POST libpod/volumes/prune?filters='{"driver":["anydriver"]}' 500 \
.cause="\"driver\" is an invalid volume filter"
t POST libpod/volumes/prune?filters='{"scope":["anyscope"]}' 500 \
.cause="\"scope\" is an invalid volume filter"

# compat api: do not use list filters for prune
t POST volumes/prune?filters='{"name":["anyname"]}' 500 \
.cause="\"name\" is an invalid volume filter"
t POST volumes/prune?filters='{"driver":["anydriver"]}' 500 \
.cause="\"driver\" is an invalid volume filter"
t POST volumes/prune?filters='{"scope":["anyscope"]}' 500 \
.cause="\"scope\" is an invalid volume filter"

# vim: filetype=sh

0 comments on commit 3338901

Please sign in to comment.