v2 api: implement pods top endpoint

Note that this commit does not add tests for the pod-top endpoint.
They will be added in a later change.

Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
This commit is contained in:
Valentin Rothberg
2020-03-27 13:31:17 +01:00
parent 684b4bd2f1
commit cc129d13c5
3 changed files with 91 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"strings"
"github.com/containers/libpod/libpod" "github.com/containers/libpod/libpod"
"github.com/containers/libpod/libpod/define" "github.com/containers/libpod/libpod/define"
@ -287,6 +288,48 @@ func PodUnpause(w http.ResponseWriter, r *http.Request) {
utils.WriteResponse(w, http.StatusOK, &report) utils.WriteResponse(w, http.StatusOK, &report)
} }
func PodTop(w http.ResponseWriter, r *http.Request) {
runtime := r.Context().Value("runtime").(*libpod.Runtime)
decoder := r.Context().Value("decoder").(*schema.Decoder)
query := struct {
PsArgs string `schema:"ps_args"`
}{
PsArgs: "",
}
if err := decoder.Decode(&query, r.URL.Query()); err != nil {
utils.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest,
errors.Wrapf(err, "Failed to parse parameters for %s", r.URL.String()))
return
}
name := utils.GetName(r)
pod, err := runtime.LookupPod(name)
if err != nil {
utils.ContainerNotFound(w, name, err)
return
}
args := []string{}
if query.PsArgs != "" {
args = append(args, query.PsArgs)
}
output, err := pod.GetPodPidInformation(args)
if err != nil {
utils.InternalServerError(w, err)
return
}
var body = handlers.PodTopOKBody{}
if len(output) > 0 {
body.Titles = strings.Split(output[0], "\t")
for _, line := range output[1:] {
body.Processes = append(body.Processes, strings.Split(line, "\t"))
}
}
utils.WriteJSON(w, http.StatusOK, body)
}
func PodKill(w http.ResponseWriter, r *http.Request) { func PodKill(w http.ResponseWriter, r *http.Request) {
var ( var (
runtime = r.Context().Value("runtime").(*libpod.Runtime) runtime = r.Context().Value("runtime").(*libpod.Runtime)

View File

@ -133,6 +133,23 @@ type ContainerTopOKBody struct {
dockerContainer.ContainerTopOKBody dockerContainer.ContainerTopOKBody
} }
type PodTopOKBody struct {
dockerContainer.ContainerTopOKBody
}
// swagger:model PodCreateConfig
type PodCreateConfig struct {
Name string `json:"name"`
CGroupParent string `json:"cgroup-parent"`
Hostname string `json:"hostname"`
Infra bool `json:"infra"`
InfraCommand string `json:"infra-command"`
InfraImage string `json:"infra-image"`
Labels []string `json:"labels"`
Publish []string `json:"publish"`
Share string `json:"share"`
}
type ErrorModel struct { type ErrorModel struct {
Message string `json:"message"` Message string `json:"message"`
} }

View File

@ -263,5 +263,36 @@ func (s *APIServer) registerPodsHandlers(r *mux.Router) error {
// 500: // 500:
// $ref: "#/responses/InternalError" // $ref: "#/responses/InternalError"
r.Handle(VersionedPath("/libpod/pods/{name}/unpause"), s.APIHandler(libpod.PodUnpause)).Methods(http.MethodPost) r.Handle(VersionedPath("/libpod/pods/{name}/unpause"), s.APIHandler(libpod.PodUnpause)).Methods(http.MethodPost)
// swagger:operation POST /libpod/pods/{name}/top pods topPod
// ---
// summary: List processes
// description: List processes running inside a pod
// produces:
// - application/json
// parameters:
// - in: path
// name: name
// type: string
// required: true
// description: |
// Name of pod to query for processes
// - in: query
// name: stream
// type: boolean
// default: true
// description: Stream the output
// - in: query
// name: ps_args
// type: string
// default: -ef
// 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.
// responses:
// 200:
// $ref: "#/responses/DockerTopResponse"
// 404:
// $ref: "#/responses/NoSuchContainer"
// 500:
// $ref: "#/responses/InternalError"
r.Handle(VersionedPath("/libpod/pods/{name}/top"), s.APIHandler(libpod.PodTop)).Methods(http.MethodGet)
return nil return nil
} }