aboutsummaryrefslogtreecommitdiff
path: root/cmd/podman
diff options
context:
space:
mode:
authorOpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com>2020-09-24 15:32:36 +0000
committerGitHub <noreply@github.com>2020-09-24 15:32:36 +0000
commit3957058f298f03bf57b0bac17f5c4d7f274e9ecb (patch)
tree6fd129d525e79eac10febb9207172e090e3da944 /cmd/podman
parent08cc91926db1cd17509f8578e2ff00a94747dbd4 (diff)
parent19b955f0999f7fe9e187e94d60327e4d6ee891c0 (diff)
downloadpodman-3957058f298f03bf57b0bac17f5c4d7f274e9ecb.tar.gz
podman-3957058f298f03bf57b0bac17f5c4d7f274e9ecb.tar.bz2
podman-3957058f298f03bf57b0bac17f5c4d7f274e9ecb.zip
Merge pull request #7753 from vrothberg/fix-7689
remote stats
Diffstat (limited to 'cmd/podman')
-rw-r--r--cmd/podman/containers/stats.go56
1 files changed, 35 insertions, 21 deletions
diff --git a/cmd/podman/containers/stats.go b/cmd/podman/containers/stats.go
index ddb5f32ef..bbd389bbf 100644
--- a/cmd/podman/containers/stats.go
+++ b/cmd/podman/containers/stats.go
@@ -4,7 +4,6 @@ import (
"fmt"
"os"
"strings"
- "sync"
"text/tabwriter"
"text/template"
@@ -48,8 +47,18 @@ var (
}
)
+// statsOptionsCLI is used for storing CLI arguments. Some fields are later
+// used in the backend.
+type statsOptionsCLI struct {
+ All bool
+ Format string
+ Latest bool
+ NoReset bool
+ NoStream bool
+}
+
var (
- statsOptions entities.ContainerStatsOptions
+ statsOptions statsOptionsCLI
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"
)
@@ -107,32 +116,37 @@ func stats(cmd *cobra.Command, args []string) error {
return errors.New("stats is not supported in rootless mode without cgroups v2")
}
}
- statsOptions.StatChan = make(chan []*define.ContainerStats, 1)
- wg := sync.WaitGroup{}
- wg.Add(1)
- go func() {
- for reports := range statsOptions.StatChan {
- if err := outputStats(reports); err != nil {
- logrus.Error(err)
- }
- }
- wg.Done()
- }()
- err := registry.ContainerEngine().ContainerStats(registry.Context(), args, statsOptions)
- wg.Wait()
- return err
+ // Convert to the entities options. We should not leak CLI-only
+ // options into the backend and separate concerns.
+ opts := entities.ContainerStatsOptions{
+ Latest: statsOptions.Latest,
+ Stream: !statsOptions.NoStream,
+ }
+ statsChan, err := registry.ContainerEngine().ContainerStats(registry.Context(), args, opts)
+ if err != nil {
+ return err
+ }
+ for report := range statsChan {
+ if report.Error != nil {
+ return report.Error
+ }
+ if err := outputStats(report.Stats); err != nil {
+ logrus.Error(err)
+ }
+ }
+ return nil
}
-func outputStats(reports []*define.ContainerStats) error {
+func outputStats(reports []define.ContainerStats) error {
if len(statsOptions.Format) < 1 && !statsOptions.NoReset {
tm.Clear()
tm.MoveCursor(1, 1)
tm.Flush()
}
- stats := make([]*containerStats, 0, len(reports))
+ stats := make([]containerStats, 0, len(reports))
for _, r := range reports {
- stats = append(stats, &containerStats{r})
+ stats = append(stats, containerStats{r})
}
if statsOptions.Format == "json" {
return outputJSON(stats)
@@ -163,7 +177,7 @@ func outputStats(reports []*define.ContainerStats) error {
}
type containerStats struct {
- *define.ContainerStats
+ define.ContainerStats
}
func (s *containerStats) ID() string {
@@ -213,7 +227,7 @@ func combineHumanValues(a, b uint64) string {
return fmt.Sprintf("%s / %s", units.HumanSize(float64(a)), units.HumanSize(float64(b)))
}
-func outputJSON(stats []*containerStats) error {
+func outputJSON(stats []containerStats) error {
type jstat struct {
Id string `json:"id"` //nolint
Name string `json:"name"`