summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorhaircommander <pehunt@redhat.com>2018-08-02 10:48:56 -0400
committerAtomic Bot <atomic-devel@projectatomic.io>2018-08-02 20:39:06 +0000
commit26b9b17d275d3b740a471e70cc8116f75de99a3d (patch)
tree05febb1fbf6b61e1217f56f25c2b00af8419194f /cmd
parent20155657ecf688568c4712a8a125ff95a5dd29a3 (diff)
downloadpodman-26b9b17d275d3b740a471e70cc8116f75de99a3d.tar.gz
podman-26b9b17d275d3b740a471e70cc8116f75de99a3d.tar.bz2
podman-26b9b17d275d3b740a471e70cc8116f75de99a3d.zip
Added ps --pod option
Signed-off-by: haircommander <pehunt@redhat.com> Closes: #1205 Approved by: rhatdan
Diffstat (limited to 'cmd')
-rw-r--r--cmd/podman/batchcontainer/container.go1
-rw-r--r--cmd/podman/ps.go39
2 files changed, 34 insertions, 6 deletions
diff --git a/cmd/podman/batchcontainer/container.go b/cmd/podman/batchcontainer/container.go
index 017837a96..de127c64d 100644
--- a/cmd/podman/batchcontainer/container.go
+++ b/cmd/podman/batchcontainer/container.go
@@ -25,6 +25,7 @@ type PsOptions struct {
Last int
Latest bool
NoTrunc bool
+ Pod bool
Quiet bool
Size bool
Sort string
diff --git a/cmd/podman/ps.go b/cmd/podman/ps.go
index f795ee278..a0fb9dabc 100644
--- a/cmd/podman/ps.go
+++ b/cmd/podman/ps.go
@@ -44,6 +44,7 @@ type psTemplateParams struct {
PIDNS string
User string
UTS string
+ Pod string
}
// psJSONParams is used as a base structure for the psParams
@@ -69,6 +70,7 @@ type psJSONParams struct {
Mounts []string `json:"mounts"`
ContainerRunning bool `json:"ctrRunning"`
Namespaces *batchcontainer.Namespace `json:"namespace,omitempty"`
+ Pod string `json:"pod,omitempty"`
}
// Type declaration and functions for sorting the PS output
@@ -101,6 +103,10 @@ type psSortedNames struct{ psSorted }
func (a psSortedNames) Less(i, j int) bool { return a.psSorted[i].Names < a.psSorted[j].Names }
+type psSortedPod struct{ psSorted }
+
+func (a psSortedPod) Less(i, j int) bool { return a.psSorted[i].Pod < a.psSorted[j].Pod }
+
type psSortedRunningFor struct{ psSorted }
func (a psSortedRunningFor) Less(i, j int) bool {
@@ -152,6 +158,10 @@ var (
Usage: "Display the extended information",
},
cli.BoolFlag{
+ Name: "pod",
+ Usage: "Print the ID and name of the pod the containers are associated with",
+ },
+ cli.BoolFlag{
Name: "quiet, q",
Usage: "Print the numeric IDs of the containers only",
},
@@ -206,7 +216,7 @@ func psCmd(c *cli.Context) error {
return errors.Errorf("too many arguments, ps takes no arguments")
}
- format := genPsFormat(c.String("format"), c.Bool("quiet"), c.Bool("size"), c.Bool("namespace"))
+ format := genPsFormat(c.String("format"), c.Bool("quiet"), c.Bool("size"), c.Bool("namespace"), c.Bool("pod"))
opts := batchcontainer.PsOptions{
All: c.Bool("all"),
@@ -215,6 +225,7 @@ func psCmd(c *cli.Context) error {
Last: c.Int("last"),
Latest: c.Bool("latest"),
NoTrunc: c.Bool("no-trunc"),
+ Pod: c.Bool("pod"),
Quiet: c.Bool("quiet"),
Size: c.Bool("size"),
Namespace: c.Bool("namespace"),
@@ -406,7 +417,7 @@ func generateContainerFilterFuncs(filter, filterValue string, runtime *libpod.Ru
}
// generate the template based on conditions given
-func genPsFormat(format string, quiet, size, namespace bool) string {
+func genPsFormat(format string, quiet, size, namespace, pod bool) string {
if format != "" {
// "\t" from the command line is not being recognized as a tab
// replacing the string "\t" to a tab character if the user passes in "\t"
@@ -415,10 +426,15 @@ func genPsFormat(format string, quiet, size, namespace bool) string {
if quiet {
return formats.IDString
}
+ podappend := ""
+ if pod {
+ podappend = "{{.Pod}}\t"
+ }
if namespace {
- return "table {{.ID}}\t{{.Names}}\t{{.PID}}\t{{.Cgroup}}\t{{.IPC}}\t{{.MNT}}\t{{.NET}}\t{{.PIDNS}}\t{{.User}}\t{{.UTS}}\t"
+ return fmt.Sprintf("table {{.ID}}\t{{.Names}}\t%s{{.PID}}\t{{.Cgroup}}\t{{.IPC}}\t{{.MNT}}\t{{.NET}}\t{{.PIDNS}}\t{{.User}}\t{{.UTS}}\t", podappend)
}
format = "table {{.ID}}\t{{.Image}}\t{{.Command}}\t{{.Created}}\t{{.Status}}\t{{.Ports}}\t{{.Names}}\t"
+ format += podappend
if size {
format += "{{.Size}}\t"
}
@@ -472,6 +488,8 @@ func sortPsOutput(sortBy string, psOutput psSorted) (psSorted, error) {
sort.Sort(psSortedNames{psOutput})
case "created":
sort.Sort(psSortedCreated{psOutput})
+ case "pod":
+ sort.Sort(psSortedPod{psOutput})
default:
return nil, errors.Errorf("invalid option for --sort, options are: command, created, id, image, names, runningfor, size, or status")
}
@@ -481,15 +499,18 @@ func sortPsOutput(sortBy string, psOutput psSorted) (psSorted, error) {
// getTemplateOutput returns the modified container information
func getTemplateOutput(psParams []psJSONParams, opts batchcontainer.PsOptions) ([]psTemplateParams, error) {
var (
- psOutput []psTemplateParams
- status, size string
- ns *batchcontainer.Namespace
+ psOutput []psTemplateParams
+ pod, status, size string
+ ns *batchcontainer.Namespace
)
// If the user is trying to filter based on size, or opted to sort on size
// the size bool must be set.
if strings.Contains(opts.Format, ".Size") || opts.Sort == "size" {
opts.Size = true
}
+ if strings.Contains(opts.Format, ".Pod") || opts.Sort == "pod" {
+ opts.Pod = true
+ }
for _, psParam := range psParams {
// do we need this?
@@ -505,6 +526,9 @@ func getTemplateOutput(psParams []psJSONParams, opts batchcontainer.PsOptions) (
}
size = units.HumanSizeWithPrecision(float64(psParam.Size.RwSize), 3) + " (virtual " + units.HumanSizeWithPrecision(float64(psParam.Size.RootFsSize), 3) + ")"
}
+ if opts.Pod {
+ pod = psParam.Pod
+ }
runningFor := units.HumanDuration(psParam.RunningFor)
command := strings.Join(psParam.Command, " ")
@@ -531,6 +555,7 @@ func getTemplateOutput(psParams []psJSONParams, opts batchcontainer.PsOptions) (
if !opts.NoTrunc {
ctrID = shortID(psParam.ID)
+ pod = shortID(psParam.Pod)
}
params := psTemplateParams{
ID: ctrID,
@@ -546,6 +571,7 @@ func getTemplateOutput(psParams []psJSONParams, opts batchcontainer.PsOptions) (
Labels: labels,
Mounts: getMounts(psParam.Mounts, opts.NoTrunc),
PID: psParam.PID,
+ Pod: pod,
}
if opts.Namespace {
@@ -599,6 +625,7 @@ func getAndSortJSONParams(containers []*libpod.Container, opts batchcontainer.Ps
Mounts: batchInfo.ConConfig.UserVolumes,
ContainerRunning: batchInfo.ConState == libpod.ContainerStateRunning,
Namespaces: ns,
+ Pod: ctr.PodID(),
}
if !batchInfo.StartedTime.IsZero() && batchInfo.ConState == libpod.ContainerStateRunning {