From 2abcd4f1de41a5e977ea7c48d5a9e9a51d410a18 Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Mon, 22 Jun 2020 16:01:47 +0200 Subject: [PATCH] libpod/containers/json: alias last -> limit Support both `last` and `limit` for in the containers listing endpoint. We intended to use `limit` which is also mentioned in the docs, but the implementation ended up using `last` as the http parameter; likely being caused by the CLI using `--last`. To avoid any regression, we decided for supporting both and aliasing `last`. Fixes: #6413 Signed-off-by: Valentin Rothberg --- pkg/api/handlers/libpod/containers.go | 17 +++++++++++++++-- pkg/bindings/containers/containers.go | 2 +- test/apiv2/20-containers.at | 21 +++++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/pkg/api/handlers/libpod/containers.go b/pkg/api/handlers/libpod/containers.go index 2556cdc2a6..5062867368 100644 --- a/pkg/api/handlers/libpod/containers.go +++ b/pkg/api/handlers/libpod/containers.go @@ -14,6 +14,7 @@ import ( "github.com/containers/libpod/pkg/ps" "github.com/gorilla/schema" "github.com/pkg/errors" + "github.com/sirupsen/logrus" ) func ContainerExists(w http.ResponseWriter, r *http.Request) { @@ -36,7 +37,8 @@ func ListContainers(w http.ResponseWriter, r *http.Request) { query := struct { All bool `schema:"all"` Filters map[string][]string `schema:"filters"` - Last int `schema:"last"` + Last int `schema:"last"` // alias for limit + Limit int `schema:"limit"` Namespace bool `schema:"namespace"` Pod bool `schema:"pod"` Size bool `schema:"size"` @@ -50,11 +52,22 @@ func ListContainers(w http.ResponseWriter, r *http.Request) { errors.Wrapf(err, "Failed to parse parameters for %s", r.URL.String())) return } + + limit := query.Limit + // Support `last` as an alias for `limit`. While Podman uses --last in + // the CLI, the API is using `limit`. As we first used `last` in the + // API as well, we decided to go with aliasing to prevent any + // regression. See github.com/containers/libpod/issues/6413. + if _, found := r.URL.Query()["last"]; found { + logrus.Info("List containers: received `last` parameter - overwriting `limit`") + limit = query.Last + } + runtime := r.Context().Value("runtime").(*libpod.Runtime) opts := entities.ContainerListOptions{ All: query.All, Filters: query.Filters, - Last: query.Last, + Last: limit, Size: query.Size, Sort: "", Namespace: query.Namespace, diff --git a/pkg/bindings/containers/containers.go b/pkg/bindings/containers/containers.go index 929b6bbd52..8c588bb404 100644 --- a/pkg/bindings/containers/containers.go +++ b/pkg/bindings/containers/containers.go @@ -35,7 +35,7 @@ func List(ctx context.Context, filters map[string][]string, all *bool, last *int params.Set("all", strconv.FormatBool(*all)) } if last != nil { - params.Set("last", strconv.Itoa(*last)) + params.Set("limit", strconv.Itoa(*last)) } if pod != nil { params.Set("pod", strconv.FormatBool(*pod)) diff --git a/test/apiv2/20-containers.at b/test/apiv2/20-containers.at index 60f6d97aa0..9efebfeb90 100644 --- a/test/apiv2/20-containers.at +++ b/test/apiv2/20-containers.at @@ -26,6 +26,27 @@ t GET libpod/containers/json?all=true 200 \ .[0].ExitCode=0 \ .[0].IsInfra=false +# Make sure `limit` works. +t GET libpod/containers/json?limit=1 200 \ + length=1 \ + .[0].Id~[0-9a-f]\\{64\\} \ + .[0].Image=$IMAGE \ + .[0].Command[0]="true" \ + .[0].State~\\\(exited\\\|stopped\\\) \ + .[0].ExitCode=0 \ + .[0].IsInfra=false + +# Make sure `last` works, which is an alias for `limit`. +# See https://github.com/containers/libpod/issues/6413. +t GET libpod/containers/json?last=1 200 \ + length=1 \ + .[0].Id~[0-9a-f]\\{64\\} \ + .[0].Image=$IMAGE \ + .[0].Command[0]="true" \ + .[0].State~\\\(exited\\\|stopped\\\) \ + .[0].ExitCode=0 \ + .[0].IsInfra=false + cid=$(jq -r '.[0].Id' <<<"$output") t DELETE libpod/containers/$cid 204