From 63d8ba64e4fd1ed1bef910b313029ca6535f43cf Mon Sep 17 00:00:00 2001 From: Valentin Rothberg Date: Tue, 14 Jan 2020 13:58:03 +0100 Subject: v2 api: top improvements * Use `pkg/adapter` to increase code reuse and reduce code redundancy. * Extend swagger docs to mention AIX descriptors. * Document the libpod endpoint which shares the same handler. Signed-off-by: Valentin Rothberg --- pkg/adapter/containers.go | 6 +----- pkg/api/handlers/containers_top.go | 27 ++++++++++++--------------- pkg/api/handlers/types.go | 1 - pkg/api/server/register_containers.go | 30 +++++++++++++++++++++++++++++- 4 files changed, 42 insertions(+), 22 deletions(-) (limited to 'pkg') diff --git a/pkg/adapter/containers.go b/pkg/adapter/containers.go index 8b21d6b94..df9f13944 100644 --- a/pkg/adapter/containers.go +++ b/pkg/adapter/containers.go @@ -927,9 +927,7 @@ func (r *LocalRuntime) Top(cli *cliconfig.TopValues) ([]string, error) { output, err = r.execPS(container, descriptors) if err != nil { - // Note: return psgoErr to guide users into using the AIX descriptors - // instead of using ps(1). - return nil, psgoErr + return nil, errors.Wrapf(err, "error executing ps(1) in the container") } // Trick: filter the ps command from the output instead of @@ -956,10 +954,8 @@ func (r *LocalRuntime) execPS(c *libpod.Container, args []string) ([]string, err streams := new(libpod.AttachStreams) streams.OutputStream = wPipe streams.ErrorStream = wPipe - streams.InputStream = bufio.NewReader(os.Stdin) streams.AttachOutput = true streams.AttachError = true - streams.AttachInput = true psOutput := []string{} go func() { diff --git a/pkg/api/handlers/containers_top.go b/pkg/api/handlers/containers_top.go index bab559da1..711f6858f 100644 --- a/pkg/api/handlers/containers_top.go +++ b/pkg/api/handlers/containers_top.go @@ -4,8 +4,10 @@ import ( "net/http" "strings" + "github.com/containers/libpod/cmd/podman/cliconfig" "github.com/containers/libpod/libpod" "github.com/containers/libpod/libpod/define" + "github.com/containers/libpod/pkg/adapter" "github.com/containers/libpod/pkg/api/handlers/utils" "github.com/gorilla/mux" "github.com/gorilla/schema" @@ -28,24 +30,19 @@ func TopContainer(w http.ResponseWriter, r *http.Request) { } name := mux.Vars(r)["name"] - ctnr, err := runtime.LookupContainer(name) - if err != nil { - utils.ContainerNotFound(w, name, err) - return - } - state, err := ctnr.State() - if err != nil { - utils.InternalServerError(w, err) - return - } - if state != define.ContainerStateRunning { - utils.ContainerNotRunning(w, name, errors.Errorf("Container %s must be running to perform top operation", name)) - return - } + adapterRuntime := adapter.LocalRuntime{} + adapterRuntime.Runtime = runtime - output, err := ctnr.Top([]string{}) + topValues := cliconfig.TopValues{} + topValues.InputArgs = []string{name, query.PsArgs} + + output, err := adapterRuntime.Top(&topValues) if err != nil { + if errors.Cause(err) == define.ErrNoSuchCtr { + utils.ContainerNotFound(w, name, err) + return + } utils.InternalServerError(w, err) return } diff --git a/pkg/api/handlers/types.go b/pkg/api/handlers/types.go index 9edbbdccc..2526a3317 100644 --- a/pkg/api/handlers/types.go +++ b/pkg/api/handlers/types.go @@ -135,7 +135,6 @@ type Stats struct { type ContainerTopOKBody struct { dockerContainer.ContainerTopOKBody - ID string `json:"Id"` } type PodCreateConfig struct { diff --git a/pkg/api/server/register_containers.go b/pkg/api/server/register_containers.go index 95b986170..ad7b2b0ca 100644 --- a/pkg/api/server/register_containers.go +++ b/pkg/api/server/register_containers.go @@ -345,7 +345,7 @@ func (s *APIServer) RegisterContainersHandlers(r *mux.Router) error { // - in: query // name: ps_args // type: string - // description: arguments to pass to ps such as aux + // description: arguments to pass to ps such as aux. Requires ps(1) to be installed in the container if no ps(1) compatible AIX descriptors are used. // produces: // - application/json // responses: @@ -653,6 +653,34 @@ func (s *APIServer) RegisterContainersHandlers(r *mux.Router) error { // '500': // "$ref": "#/responses/InternalError" r.HandleFunc(VersionedPath("/libpod/containers/{name:..*}/stats"), APIHandler(s.Context, generic.StatsContainer)).Methods(http.MethodGet) + // swagger:operation GET /libpod/containers/{nameOrID}/top containers topContainer + // + // List processes running inside a container. Note + // + // --- + // parameters: + // - in: path + // name: nameOrID + // required: true + // description: the name or ID of the container + // - in: query + // name: stream + // type: bool + // default: true + // description: Stream the output + // name: ps_args + // type: string + // description: arguments to pass to ps such as aux. Requires ps(1) to be installed in the container if no ps(1) compatible AIX descriptors are used. + // produces: + // - application/json + // responses: + // '200': + // description: no error + // "ref": "#/responses/DockerTopResponse" + // '404': + // "$ref": "#/responses/NoSuchContainer" + // '500': + // "$ref": "#/responses/InternalError" r.HandleFunc(VersionedPath("/libpod/containers/{name:..*}/top"), APIHandler(s.Context, handlers.TopContainer)).Methods(http.MethodGet) // swagger:operation POST /libpod/containers/{nameOrID}/unpause containers libpodUnpauseContainer // --- -- cgit v1.2.3-54-g00ecf