From 5185e7e39eea1882b8b6ca7dc7b98d5dd5fe3c5d Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Wed, 22 Mar 2023 14:09:44 +0100 Subject: [PATCH] stats compat API: return "id" lowercase We use the same endpoint for libpod and docker compat API. However as reported docker returns "id" lowercase. Because we cannot break the libpod API right now keep the output for the libpod endpoint and only change the docker one. To do so simply use two types that we can cast with different JSON tags. Fixes #17869 Signed-off-by: Paul Holzinger --- pkg/api/handlers/compat/containers_stats.go | 9 ++++++++- pkg/api/handlers/compat/types.go | 13 +++++++++++++ test/apiv2/20-containers.at | 8 +++++++- 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/pkg/api/handlers/compat/containers_stats.go b/pkg/api/handlers/compat/containers_stats.go index 848252a766..223f8f2ac8 100644 --- a/pkg/api/handlers/compat/containers_stats.go +++ b/pkg/api/handlers/compat/containers_stats.go @@ -202,7 +202,14 @@ streamLabel: // A label to flatten the scope Networks: net, } - if err := coder.Encode(s); err != nil { + var jsonOut interface{} + if utils.IsLibpodRequest(r) { + jsonOut = s + } else { + jsonOut = DockerStatsJSON(s) + } + + if err := coder.Encode(jsonOut); err != nil { logrus.Errorf("Unable to encode stats: %v", err) return } diff --git a/pkg/api/handlers/compat/types.go b/pkg/api/handlers/compat/types.go index 6d47ede649..f422784d4b 100644 --- a/pkg/api/handlers/compat/types.go +++ b/pkg/api/handlers/compat/types.go @@ -53,3 +53,16 @@ type StatsJSON struct { // Networks request version >=1.21 Networks map[string]docker.NetworkStats `json:"networks,omitempty"` } + +// DockerStatsJSON is the same as StatsJSON except for the lowercase +// "id" in the JSON tag. This is needed for docker compat but we should +// not change the libpod API output for backwards compat reasons. +type DockerStatsJSON struct { + Stats + + Name string `json:"name,omitempty"` + ID string `json:"id,omitempty"` + + // Networks request version >=1.21 + Networks map[string]docker.NetworkStats `json:"networks,omitempty"` +} diff --git a/test/apiv2/20-containers.at b/test/apiv2/20-containers.at index e77a8b09ac..650c67cfd1 100644 --- a/test/apiv2/20-containers.at +++ b/test/apiv2/20-containers.at @@ -113,7 +113,13 @@ if root; then podman run --name $CTRNAME -d -m 512m -v /tmp:/tmp $IMAGE top t GET libpod/containers/$CTRNAME/stats?stream=false 200 \ - .memory_stats.limit=536870912 + .memory_stats.limit=536870912 \ + .Id~[0-9a-f]\\{64\\} + + # Make sure docker compat endpoint shows "id" lowercase + t GET containers/$CTRNAME/stats?stream=false 200 \ + .memory_stats.limit=536870912 \ + .id~[0-9a-f]\\{64\\} podman rm -f $CTRNAME fi