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 <rothberg@redhat.com>
This commit is contained in:
Valentin Rothberg
2020-01-14 13:58:03 +01:00
parent e025b43060
commit 63d8ba64e4
4 changed files with 42 additions and 22 deletions

View File

@ -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() {

View File

@ -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
}

View File

@ -135,7 +135,6 @@ type Stats struct {
type ContainerTopOKBody struct {
dockerContainer.ContainerTopOKBody
ID string `json:"Id"`
}
type PodCreateConfig struct {

View File

@ -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
// ---