Skip to content

Commit

Permalink
Only support containers stats using cgroups v2
Browse files Browse the repository at this point in the history
  • Loading branch information
jwhonce authored and mheon committed Aug 11, 2021
1 parent c836ffe commit a820061
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/api/handlers/libpod/containers_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (

"github.com/containers/podman/v3/libpod"
"github.com/containers/podman/v3/pkg/api/handlers/utils"
"github.com/containers/podman/v3/pkg/cgroups"
"github.com/containers/podman/v3/pkg/domain/entities"
"github.com/containers/podman/v3/pkg/domain/infra/abi"
"github.com/containers/podman/v3/pkg/rootless"
"github.com/gorilla/schema"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand All @@ -20,6 +22,16 @@ func StatsContainer(w http.ResponseWriter, r *http.Request) {
runtime := r.Context().Value("runtime").(*libpod.Runtime)
decoder := r.Context().Value("decoder").(*schema.Decoder)

// Check if service is running rootless (cheap check)
if rootless.IsRootless() {
// if so, then verify cgroup v2 available (more expensive check)
if isV2, _ := cgroups.IsCgroup2UnifiedMode(); !isV2 {
msg := "Container stats resource only available for cgroup v2"
utils.Error(w, msg, http.StatusConflict, errors.New(msg))
return
}
}

query := struct {
Containers []string `schema:"containers"`
Stream bool `schema:"stream"`
Expand Down
4 changes: 4 additions & 0 deletions pkg/api/server/register_containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,8 @@ func (s *APIServer) registerContainersHandlers(r *mux.Router) error {
// description: no error
// 404:
// $ref: "#/responses/NoSuchContainer"
// 409:
// $ref: "#/responses/ConflictError"
// 500:
// $ref: "#/responses/InternalError"
r.HandleFunc(VersionedPath("/libpod/containers/{name}/stats"), s.APIHandler(compat.StatsContainer)).Methods(http.MethodGet)
Expand Down Expand Up @@ -1113,6 +1115,8 @@ func (s *APIServer) registerContainersHandlers(r *mux.Router) error {
// description: no error
// 404:
// $ref: "#/responses/NoSuchContainer"
// 409:
// $ref: "#/responses/ConflictError"
// 500:
// $ref: "#/responses/InternalError"
r.HandleFunc(VersionedPath("/libpod/containers/stats"), s.APIHandler(libpod.StatsContainer)).Methods(http.MethodGet)
Expand Down
10 changes: 10 additions & 0 deletions test/apiv2/20-containers.at
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ t GET libpod/containers/json?last=1 200 \

cid=$(jq -r '.[0].Id' <<<"$output")

if root; then
t GET libpod/containers/stats?containers='[$cid]' 200
else
if have_cgroupsv2; then
t GET libpod/containers/stats?containers='[$cid]' 200
else
t GET libpod/containers/stats?containers='[$cid]' 409
fi
fi

t DELETE libpod/containers/$cid 204

# Issue #6799: it should be possible to start a container, even w/o args.
Expand Down

0 comments on commit a820061

Please sign in to comment.