mirror of
https://github.com/containers/podman.git
synced 2025-06-23 02:18:13 +08:00
Merge pull request #8007 from ashley-cui/formatting
Restore --format: container stats & pod ps
This commit is contained in:
@ -3,12 +3,13 @@ package containers
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
tm "github.com/buger/goterm"
|
tm "github.com/buger/goterm"
|
||||||
|
"github.com/containers/podman/v2/cmd/podman/parse"
|
||||||
"github.com/containers/podman/v2/cmd/podman/registry"
|
"github.com/containers/podman/v2/cmd/podman/registry"
|
||||||
|
"github.com/containers/podman/v2/cmd/podman/report"
|
||||||
"github.com/containers/podman/v2/cmd/podman/validate"
|
"github.com/containers/podman/v2/cmd/podman/validate"
|
||||||
"github.com/containers/podman/v2/libpod/define"
|
"github.com/containers/podman/v2/libpod/define"
|
||||||
"github.com/containers/podman/v2/pkg/cgroups"
|
"github.com/containers/podman/v2/pkg/cgroups"
|
||||||
@ -58,9 +59,8 @@ type statsOptionsCLI struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
statsOptions statsOptionsCLI
|
statsOptions statsOptionsCLI
|
||||||
defaultStatsRow = "{{.ID}}\t{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}\t{{.NetIO}}\t{{.BlockIO}}\t{{.PIDS}}\n"
|
defaultStatsRow = "{{.ID}}\t{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}\t{{.NetIO}}\t{{.BlockIO}}\t{{.PIDS}}\n"
|
||||||
defaultStatsHeader = "ID\tNAME\tCPU %\tMEM USAGE / LIMIT\tMEM %\tNET IO\tBLOCK IO\tPIDS\n"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func statFlags(flags *pflag.FlagSet) {
|
func statFlags(flags *pflag.FlagSet) {
|
||||||
@ -139,7 +139,16 @@ func stats(cmd *cobra.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func outputStats(reports []define.ContainerStats) error {
|
func outputStats(reports []define.ContainerStats) error {
|
||||||
if len(statsOptions.Format) < 1 && !statsOptions.NoReset {
|
headers := report.Headers(define.ContainerStats{}, map[string]string{
|
||||||
|
"ID": "ID",
|
||||||
|
"CPUPerc": "CPU %",
|
||||||
|
"MemUsage": "MEM USAGE / LIMIT",
|
||||||
|
"MemPerc": "MEM %",
|
||||||
|
"NetIO": "NET IO",
|
||||||
|
"BlockIO": "BLOCK IO",
|
||||||
|
"PIDS": "PIDS",
|
||||||
|
})
|
||||||
|
if !statsOptions.NoReset {
|
||||||
tm.Clear()
|
tm.Clear()
|
||||||
tm.MoveCursor(1, 1)
|
tm.MoveCursor(1, 1)
|
||||||
tm.Flush()
|
tm.Flush()
|
||||||
@ -148,25 +157,27 @@ func outputStats(reports []define.ContainerStats) error {
|
|||||||
for _, r := range reports {
|
for _, r := range reports {
|
||||||
stats = append(stats, containerStats{r})
|
stats = append(stats, containerStats{r})
|
||||||
}
|
}
|
||||||
if statsOptions.Format == "json" {
|
if parse.MatchesJSONFormat(statsOptions.Format) {
|
||||||
return outputJSON(stats)
|
return outputJSON(stats)
|
||||||
}
|
}
|
||||||
format := defaultStatsRow
|
format := defaultStatsRow
|
||||||
|
|
||||||
if len(statsOptions.Format) > 0 {
|
if len(statsOptions.Format) > 0 {
|
||||||
format = statsOptions.Format
|
format = report.NormalizeFormat(statsOptions.Format)
|
||||||
if !strings.HasSuffix(format, "\n") {
|
} else if len(statsOptions.Format) < 1 {
|
||||||
format += "\n"
|
format = defaultStatsRow
|
||||||
}
|
|
||||||
}
|
}
|
||||||
format = "{{range . }}" + format + "{{end}}"
|
format = "{{range . }}" + format + "{{end}}"
|
||||||
if len(statsOptions.Format) < 1 {
|
|
||||||
format = defaultStatsHeader + format
|
|
||||||
}
|
|
||||||
tmpl, err := template.New("stats").Parse(format)
|
tmpl, err := template.New("stats").Parse(format)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0)
|
w := tabwriter.NewWriter(os.Stdout, 8, 2, 2, ' ', 0)
|
||||||
|
if len(statsOptions.Format) < 1 {
|
||||||
|
if err := tmpl.Execute(w, headers); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
if err := tmpl.Execute(w, stats); err != nil {
|
if err := tmpl.Execute(w, stats); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -106,12 +106,13 @@ func pods(cmd *cobra.Command, _ []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
headers := report.Headers(ListPodReporter{}, map[string]string{
|
headers := report.Headers(ListPodReporter{}, map[string]string{
|
||||||
"ContainerIds": "IDS",
|
"Id": "POD ID",
|
||||||
"ContainerNames": "NAMES",
|
"Name": "NAME",
|
||||||
"ContainerStatuses": "STATUS",
|
"Status": "STATUS",
|
||||||
"Namespace": "NAMESPACES",
|
"Labels": "LABELS",
|
||||||
"NumberOfContainers": "# OF CONTAINERS",
|
"NumberOfContainers": "# OF CONTAINERS",
|
||||||
"InfraId": "INFRA ID",
|
"Created": "CREATED",
|
||||||
|
"InfraID": "INFRA ID",
|
||||||
})
|
})
|
||||||
row := podPsFormat()
|
row := podPsFormat()
|
||||||
if cmd.Flags().Changed("format") {
|
if cmd.Flags().Changed("format") {
|
||||||
@ -135,7 +136,7 @@ func pods(cmd *cobra.Command, _ []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func podPsFormat() string {
|
func podPsFormat() string {
|
||||||
row := []string{"{{.Id}}", "{{.Name}}", "{{.Status}}", "{{.Created}}}"}
|
row := []string{"{{.Id}}", "{{.Name}}", "{{.Status}}", "{{.Created}}", "{{.InfraID}}"}
|
||||||
|
|
||||||
if psInput.CtrIds {
|
if psInput.CtrIds {
|
||||||
row = append(row, "{{.ContainerIds}}")
|
row = append(row, "{{.ContainerIds}}")
|
||||||
|
@ -68,11 +68,10 @@ Valid placeholders for the Go template are listed below:
|
|||||||
| .Name | Name of pod |
|
| .Name | Name of pod |
|
||||||
| .Status | Status of pod |
|
| .Status | Status of pod |
|
||||||
| .Labels | All the labels assigned to the pod |
|
| .Labels | All the labels assigned to the pod |
|
||||||
| .ContainerInfo | Show the names, ids and/or statuses of containers (only shows 9 unless no-trunc is specified) |
|
|
||||||
| .NumberOfContainers | Show the number of containers attached to pod |
|
| .NumberOfContainers | Show the number of containers attached to pod |
|
||||||
| .Cgroup | Cgroup path of pod |
|
| .Cgroup | Cgroup path of pod |
|
||||||
| .UsePodCgroup | Whether containers use the Cgroup of the pod |
|
| .Created | Creation time of pod |
|
||||||
|
| .InfraID | Pod infra container ID |
|
||||||
**--sort**
|
**--sort**
|
||||||
|
|
||||||
Sort by created, ID, name, status, or number of containers
|
Sort by created, ID, name, status, or number of containers
|
||||||
|
@ -47,12 +47,11 @@ Valid placeholders for the Go template are listed below:
|
|||||||
|
|
||||||
| **Placeholder** | **Description** |
|
| **Placeholder** | **Description** |
|
||||||
| --------------- | --------------- |
|
| --------------- | --------------- |
|
||||||
| .Pod | Pod ID |
|
|
||||||
| .ID | Container ID |
|
| .ID | Container ID |
|
||||||
| .Name | Container Name |
|
| .Name | Container Name |
|
||||||
| .CPU | CPU percentage |
|
| .CPUPerc | CPU percentage |
|
||||||
| .MemUsage | Memory usage |
|
| .MemUsage | Memory usage |
|
||||||
| .Mem | Memory percentage |
|
| .MemPerc | Memory percentage |
|
||||||
| .NetIO | Network IO |
|
| .NetIO | Network IO |
|
||||||
| .BlockIO | Block IO |
|
| .BlockIO | Block IO |
|
||||||
| .PIDS | Number of PIDs |
|
| .PIDS | Number of PIDs |
|
||||||
|
Reference in New Issue
Block a user