aboutsummaryrefslogtreecommitdiff
path: root/cmd/podman/pod_top.go
blob: f65d66df67cbf94513a5750628a8816064ad87d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main

import (
	"fmt"
	"github.com/containers/libpod/pkg/adapter"
	"os"
	"strings"
	"text/tabwriter"

	"github.com/containers/libpod/cmd/podman/cliconfig"
	"github.com/containers/libpod/libpod"
	"github.com/containers/libpod/pkg/rootless"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
)

var (
	podTopCommand cliconfig.PodTopValues

	podTopDescription = 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.
%s`, getDescriptorString())

	_podTopCommand = &cobra.Command{
		Use:   "top [flags] CONTAINER [FORMAT-DESCRIPTORS]",
		Short: "Display the running processes of containers in a pod",
		Long:  podTopDescription,
		RunE: func(cmd *cobra.Command, args []string) error {
			podTopCommand.InputArgs = args
			podTopCommand.GlobalFlags = MainGlobalOpts
			return podTopCmd(&podTopCommand)
		},
		Example: `podman top ctrID
  podman top --latest
  podman top --latest pid seccomp args %C`,
	}
)

func init() {
	podTopCommand.Command = _podTopCommand
	podTopCommand.SetHelpTemplate(HelpTemplate())
	podTopCommand.SetUsageTemplate(UsageTemplate())
	flags := podTopCommand.Flags()
	flags.BoolVarP(&podTopCommand.Latest, "latest,", "l", false, "Act on the latest pod podman is aware of")
	flags.BoolVar(&podTopCommand.ListDescriptors, "list-descriptors", false, "")
	flags.MarkHidden("list-descriptors")

}

func podTopCmd(c *cliconfig.PodTopValues) error {
	var (
		descriptors []string
	)
	args := c.InputArgs

	if os.Geteuid() != 0 {
		rootless.SetSkipStorageSetup(true)
	}

	if c.ListDescriptors {
		descriptors, err := libpod.GetContainerPidInformationDescriptors()
		if err != nil {
			return err
		}
		fmt.Println(strings.Join(descriptors, "\n"))
		return nil
	}

	if len(args) < 1 && !c.Latest {
		return errors.Errorf("you must provide the name or id of a running pod")
	}

	runtime, err := adapter.GetRuntime(&c.PodmanCommand)
	if err != nil {
		return errors.Wrapf(err, "error creating libpod runtime")
	}
	defer runtime.Shutdown(false)

	if c.Latest {
		descriptors = args
	} else {
		descriptors = args[1:]
	}

	if os.Geteuid() != 0 {
		var pod *adapter.Pod
		var err error
		if c.Latest {
			pod, err = runtime.GetLatestPod()
		} else {
			pod, err = runtime.LookupPod(c.InputArgs[0])
		}
		if err != nil {
			return errors.Wrapf(err, "unable to lookup requested container")
		}
		became, ret, err := runtime.JoinOrCreateRootlessPod(pod)
		if err != nil {
			return err
		}
		if became {
			os.Exit(ret)
		}
	}

	w := tabwriter.NewWriter(os.Stdout, 5, 1, 3, ' ', 0)
	psOutput, err := runtime.PodTop(c, descriptors)
	if err != nil {
		return err
	}
	for _, proc := range psOutput {
		fmt.Fprintln(w, proc)
	}
	w.Flush()
	return nil
}