podmanv2: implement pod top

Implement `podman pod top` for podmanV2.

Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
This commit is contained in:
Valentin Rothberg 2020-03-27 11:46:33 +01:00
parent cc129d13c5
commit 9812804f75
9 changed files with 217 additions and 7 deletions

90
cmd/podmanV2/pods/top.go Normal file
View File

@ -0,0 +1,90 @@
package pods
import (
"context"
"fmt"
"os"
"strings"
"text/tabwriter"
"github.com/containers/libpod/cmd/podmanV2/registry"
"github.com/containers/libpod/pkg/domain/entities"
"github.com/containers/psgo"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
topDescription = fmt.Sprintf(`Specify format descriptors to alter the output.
You may run "podman pod top -l pid pcpu seccomp" to print the process ID, the CPU percentage and the seccomp mode of each process of the latest pod.
Format Descriptors:
%s`, strings.Join(psgo.ListDescriptors(), ","))
topOptions = entities.PodTopOptions{}
topCommand = &cobra.Command{
Use: "top [flags] POD [FORMAT-DESCRIPTORS|ARGS]",
Short: "Display the running processes in a pod",
Long: topDescription,
PersistentPreRunE: preRunE,
RunE: top,
Args: cobra.ArbitraryArgs,
Example: `podman pod top podID
podman pod top --latest
podman pod top podID pid seccomp args %C
podman pod top podID -eo user,pid,comm`,
}
)
func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
Command: topCommand,
Parent: podCmd,
})
topCommand.SetHelpTemplate(registry.HelpTemplate())
topCommand.SetUsageTemplate(registry.UsageTemplate())
flags := topCommand.Flags()
flags.SetInterspersed(false)
flags.BoolVar(&topOptions.ListDescriptors, "list-descriptors", false, "")
flags.BoolVarP(&topOptions.Latest, "latest", "l", false, "Act on the latest container podman is aware of")
_ = flags.MarkHidden("list-descriptors") // meant only for bash completion
if registry.IsRemote() {
_ = flags.MarkHidden("latest")
}
}
func top(cmd *cobra.Command, args []string) error {
if topOptions.ListDescriptors {
fmt.Println(strings.Join(psgo.ListDescriptors(), "\n"))
return nil
}
if len(args) < 1 && !topOptions.Latest {
return errors.Errorf("you must provide the name or id of a running pod")
}
if topOptions.Latest {
topOptions.Descriptors = args
} else {
topOptions.NameOrID = args[0]
topOptions.Descriptors = args[1:]
}
topResponse, err := registry.ContainerEngine().PodTop(context.Background(), topOptions)
if err != nil {
return err
}
w := tabwriter.NewWriter(os.Stdout, 5, 1, 3, ' ', 0)
for _, proc := range topResponse.Value {
if _, err := fmt.Fprintln(w, proc); err != nil {
return err
}
}
return w.Flush()
}

View File

@ -41,12 +41,23 @@ func (p *Pod) GetPodPidInformation(descriptors []string) ([]string, error) {
}
c.lock.Unlock()
}
// Also support comma-separated input.
psgoDescriptors := []string{}
for _, d := range descriptors {
for _, s := range strings.Split(d, ",") {
if s != "" {
psgoDescriptors = append(psgoDescriptors, s)
}
}
}
// TODO: psgo returns a [][]string to give users the ability to apply
// filters on the data. We need to change the API here and the
// varlink API to return a [][]string if we want to make use of
// filtering.
opts := psgo.JoinNamespaceOpts{FillMappings: rootless.IsRootless()}
output, err := psgo.JoinNamespaceAndProcessInfoByPidsWithOptions(pids, descriptors, &opts)
output, err := psgo.JoinNamespaceAndProcessInfoByPidsWithOptions(pids, psgoDescriptors, &opts)
if err != nil {
return nil, err
}

View File

@ -8,6 +8,7 @@ import (
"strings"
"github.com/containers/libpod/libpod"
"github.com/containers/libpod/pkg/api/handlers"
"github.com/containers/libpod/pkg/bindings"
"github.com/containers/libpod/pkg/domain/entities"
"github.com/containers/libpod/pkg/specgen"
@ -213,9 +214,38 @@ func Stop(ctx context.Context, nameOrID string, timeout *int) (*entities.PodStop
return &report, response.Process(&report)
}
func Top() error {
// TODO
return bindings.ErrNotImplemented // nolint:typecheck
// Top gathers statistics about the running processes in a pod. The nameOrID can be a pod name
// or a partial/full ID. The descriptors allow for specifying which data to collect from each process.
func Top(ctx context.Context, nameOrID string, descriptors []string) ([]string, error) {
conn, err := bindings.GetClient(ctx)
if err != nil {
return nil, err
}
params := url.Values{}
if len(descriptors) > 0 {
// flatten the slice into one string
params.Set("ps_args", strings.Join(descriptors, ","))
}
response, err := conn.DoRequest(nil, http.MethodGet, "/pods/%s/top", params, nameOrID)
if err != nil {
return nil, err
}
body := handlers.PodTopOKBody{}
if err = response.Process(&body); err != nil {
return nil, err
}
// handlers.PodTopOKBody{} returns a slice of slices where each cell in the top table is an item.
// In libpod land, we're just using a slice with cells being split by tabs, which allows for an idiomatic
// usage of the tabwriter.
topOutput := []string{strings.Join(body.Titles, "\t")}
for _, out := range body.Processes {
topOutput = append(topOutput, strings.Join(out, "\t"))
}
return topOutput, err
}
// Unpause unpauses all paused containers in a Pod.

View File

@ -387,15 +387,15 @@ var _ = Describe("Podman containers ", func() {
Expect(err).To(BeNil())
// By name
output, err := containers.Top(bt.conn, name, nil)
_, err = containers.Top(bt.conn, name, nil)
Expect(err).To(BeNil())
// By id
output, err = containers.Top(bt.conn, cid, nil)
_, err = containers.Top(bt.conn, cid, nil)
Expect(err).To(BeNil())
// With descriptors
output, err = containers.Top(bt.conn, cid, []string{"user,pid,hpid"})
output, err := containers.Top(bt.conn, cid, []string{"user,pid,hpid"})
Expect(err).To(BeNil())
header := strings.Split(output[0], "\t")
for _, d := range []string{"USER", "PID", "HPID"} {

View File

@ -2,6 +2,7 @@ package test_bindings
import (
"net/http"
"strings"
"time"
"github.com/containers/libpod/libpod/define"
@ -319,4 +320,33 @@ var _ = Describe("Podman pods", func() {
Expect(err).To(BeNil())
Expect(exists).To(BeTrue())
})
// Test validates the pod top bindings
It("pod top", func() {
var name string = "podA"
bt.Podcreate(&name)
_, err := pods.Start(bt.conn, name)
Expect(err).To(BeNil())
// By name
_, err = pods.Top(bt.conn, name, nil)
Expect(err).To(BeNil())
// With descriptors
output, err := pods.Top(bt.conn, name, []string{"user,pid,hpid"})
Expect(err).To(BeNil())
header := strings.Split(output[0], "\t")
for _, d := range []string{"USER", "PID", "HPID"} {
Expect(d).To(BeElementOf(header))
}
// With bogus ID
_, err = pods.Top(bt.conn, "IdoNotExist", nil)
Expect(err).ToNot(BeNil())
// With bogus descriptors
_, err = pods.Top(bt.conn, name, []string{"Me,Neither"})
Expect(err).ToNot(BeNil())
})
})

View File

@ -24,6 +24,7 @@ type ContainerEngine interface {
PodStop(ctx context.Context, namesOrIds []string, options PodStopOptions) ([]*PodStopReport, error)
PodRm(ctx context.Context, namesOrIds []string, options PodRmOptions) ([]*PodRmReport, error)
PodUnpause(ctx context.Context, namesOrIds []string, options PodunpauseOptions) ([]*PodUnpauseReport, error)
PodTop(ctx context.Context, options PodTopOptions) (*StringSliceReport, error)
VolumeCreate(ctx context.Context, opts VolumeCreateOptions) (*IdOrNameResponse, error)
VolumeInspect(ctx context.Context, namesOrIds []string, opts VolumeInspectOptions) ([]*VolumeInspectReport, error)

View File

@ -141,3 +141,13 @@ func (p PodCreateOptions) ToPodSpecGen(s *specgen.PodSpecGenerator) {
// Cgroup
s.CgroupParent = p.CGroupParent
}
type PodTopOptions struct {
// CLI flags.
ListDescriptors bool
Latest bool
// Options for the API.
Descriptors []string
NameOrID string
}

View File

@ -250,3 +250,25 @@ func (ic *ContainerEngine) PodCreate(ctx context.Context, opts entities.PodCreat
}
return &entities.PodCreateReport{Id: pod.ID()}, nil
}
func (ic *ContainerEngine) PodTop(ctx context.Context, options entities.PodTopOptions) (*entities.StringSliceReport, error) {
var (
pod *libpod.Pod
err error
)
// Look up the pod.
if options.Latest {
pod, err = ic.Libpod.GetLatestPod()
} else {
pod, err = ic.Libpod.LookupPod(options.NameOrID)
}
if err != nil {
return nil, errors.Wrap(err, "unable to lookup requested container")
}
// Run Top.
report := &entities.StringSliceReport{}
report.Value, err = pod.GetPodPidInformation(options.Descriptors)
return report, err
}

View File

@ -6,6 +6,7 @@ import (
"github.com/containers/libpod/pkg/bindings/pods"
"github.com/containers/libpod/pkg/domain/entities"
"github.com/containers/libpod/pkg/specgen"
"github.com/pkg/errors"
)
func (ic *ContainerEngine) PodExists(ctx context.Context, nameOrId string) (*entities.BoolReport, error) {
@ -177,3 +178,18 @@ func (ic *ContainerEngine) PodCreate(ctx context.Context, opts entities.PodCreat
opts.ToPodSpecGen(podSpec)
return pods.CreatePodFromSpec(ic.ClientCxt, podSpec)
}
func (ic *ContainerEngine) PodTop(ctx context.Context, options entities.PodTopOptions) (*entities.StringSliceReport, error) {
switch {
case options.Latest:
return nil, errors.New("latest is not supported")
case options.NameOrID == "":
return nil, errors.New("NameOrID must be specified")
}
topOutput, err := pods.Top(ic.ClientCxt, options.NameOrID, options.Descriptors)
if err != nil {
return nil, err
}
return &entities.StringSliceReport{Value: topOutput}, nil
}