feat(osctl): improve output of stats and ps commands (#788)

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
This commit is contained in:
Andrey Smirnov
2019-06-27 01:37:54 +03:00
committed by Andrew Rynhard
parent 7a5cc7b9f3
commit 17f28d3461
5 changed files with 42 additions and 10 deletions

View File

@@ -86,15 +86,15 @@ var lsCmd = &cobra.Command{
if info.Error != "" {
fmt.Fprintf(os.Stderr, "error reading file %s: %s\n", info.Name, info.Error)
} else {
name := info.RelativeName
display := info.RelativeName
if info.Link != "" {
name += " -> " + info.Link
display += " -> " + info.Link
}
fmt.Fprintf(w, "%s\t%d\t%s\t%s\n",
os.FileMode(info.Mode).String(),
info.Size,
time.Unix(info.Modified, 0).Format("Jan 2 2006"),
name,
display,
)
}
}

View File

@@ -8,6 +8,8 @@ package cmd
import (
"fmt"
"os"
"sort"
"strings"
"text/tabwriter"
criconstants "github.com/containerd/cri/pkg/constants"
@@ -48,10 +50,20 @@ var psCmd = &cobra.Command{
}
func processesRender(reply *proto.ProcessesReply) {
sort.Slice(reply.Processes,
func(i, j int) bool {
return strings.Compare(reply.Processes[i].Id, reply.Processes[j].Id) < 0
})
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
fmt.Fprintln(w, "NAMESPACE\tID\tIMAGE\tPID\tSTATUS")
for _, p := range reply.Processes {
fmt.Fprintf(w, "%s\t%s\t%s\t%d\t%s\n", p.Namespace, p.Id, p.Image, p.Pid, p.Status)
display := p.Id
if p.Id != p.PodId {
// container in a sandbox
display = "└─ " + display
}
fmt.Fprintf(w, "%s\t%s\t%s\t%d\t%s\n", p.Namespace, display, p.Image, p.Pid, p.Status)
}
helpers.Should(w.Flush())
}

View File

@@ -8,6 +8,8 @@ package cmd
import (
"fmt"
"os"
"sort"
"strings"
"text/tabwriter"
criconstants "github.com/containerd/cri/pkg/constants"
@@ -47,10 +49,20 @@ var statsCmd = &cobra.Command{
}
func statsRender(reply *proto.StatsReply) {
sort.Slice(reply.Stats,
func(i, j int) bool {
return strings.Compare(reply.Stats[i].Id, reply.Stats[j].Id) < 0
})
w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
fmt.Fprintln(w, "NAMESPACE\tID\tMEMORY(MB)\tCPU")
for _, s := range reply.Stats {
fmt.Fprintf(w, "%s\t%s\t%.2f\t%d\n", s.Namespace, s.Id, float64(s.MemoryUsage)*1e-6, s.CpuUsage)
display := s.Id
if s.Id != s.PodId {
// container in a sandbox
display = "└─ " + display
}
fmt.Fprintf(w, "%s\t%s\t%.2f\t%d\n", s.Namespace, display, float64(s.MemoryUsage)*1e-6, s.CpuUsage)
}
helpers.Should(w.Flush())
}

View File

@@ -95,11 +95,13 @@ func (r *Registrator) Processes(ctx context.Context, in *proto.ProcessesRequest)
processes := []*proto.Process{}
for _, containers := range pods {
for _, container := range containers.Containers {
for _, pod := range pods {
for _, container := range pod.Containers {
process := &proto.Process{
Namespace: in.Namespace,
Id: container.Display,
PodId: pod.Name,
Name: container.Name,
Image: container.Image,
Pid: container.Pid,
Status: strings.ToUpper(string(container.Status.Status)),
@@ -134,8 +136,8 @@ func (r *Registrator) Stats(ctx context.Context, in *proto.StatsRequest) (reply
stats := []*proto.Stat{}
for _, containers := range pods {
for _, container := range containers.Containers {
for _, pod := range pods {
for _, container := range pod.Containers {
if container.Metrics == nil {
continue
}
@@ -163,6 +165,8 @@ func (r *Registrator) Stats(ctx context.Context, in *proto.StatsRequest) (reply
stat := &proto.Stat{
Namespace: in.Namespace,
Id: container.Display,
PodId: pod.Name,
Name: container.Name,
MemoryUsage: used,
CpuUsage: data.CPU.Usage.Total,
}

View File

@@ -35,6 +35,8 @@ message Process {
string image = 3;
uint32 pid = 4;
string status = 5;
string pod_id = 6;
string name = 7;
}
// The request message containing the containerd namespace.
@@ -49,6 +51,8 @@ message Stat {
string id = 2;
uint64 memory_usage = 4;
uint64 cpu_usage = 5;
string pod_id = 6;
string name = 7;
}
// The request message containing the process to restart.
@@ -86,7 +90,7 @@ message Route {
// Gateway is the gateway address to which traffic to this destination should be sent
string gateway = 3;
// Metric is the priority of the route, where lower metrics have higher priorities
uint32 metric = 4;