diff --git a/cmd/workload/cmd.go b/cmd/workload/cmd.go index c96915b..0534529 100644 --- a/cmd/workload/cmd.go +++ b/cmd/workload/cmd.go @@ -117,6 +117,14 @@ func Command() *cli.Command { Name: "skip-ip", Usage: "filter out IP", }, + &cli.StringSliceFlag{ + Name: "pod", + Usage: "filter by Pod", + }, + &cli.BoolFlag{ + Name: "statistics", + Usage: "Display the statistics of Workloads", + }, }, }, { diff --git a/cmd/workload/list.go b/cmd/workload/list.go index 4c6c606..2955488 100644 --- a/cmd/workload/list.go +++ b/cmd/workload/list.go @@ -22,6 +22,8 @@ type listWorkloadsOptions struct { labels map[string]string matchIPs []string skipIPs []string + podnames []string + statistics bool } func (o *listWorkloadsOptions) run(ctx context.Context) error { @@ -54,14 +56,23 @@ func (o *listWorkloadsOptions) run(ctx context.Context) error { ips: o.matchIPs, skipIPs: o.skipIPs, nodenames: []string{}, + podnames: []string{}, } if len(o.nodename) > 0 { f.nodenames = append(f.nodenames, o.nodename) } + if len(o.podnames) > 0 { + f.podnames = append(f.podnames, o.podnames...) + } workloads = f.filterIn(workloads) - describe.Workloads(workloads...) + if o.statistics { + describe.WorkloadsStatistics(workloads...) + } else { + describe.Workloads(workloads...) + } + return nil } @@ -69,6 +80,7 @@ type filter struct { ips []string skipIPs []string nodenames []string + podnames []string } func (wf filter) filterIn(workloads []*corepb.Workload) []*corepb.Workload { @@ -100,7 +112,8 @@ func (wf filter) skip(workload *corepb.Workload) bool { } return (len(wf.ips) > 0 && !wf.hasIntersection(wf.ips, ips)) || - (len(wf.skipIPs) > 0 && wf.hasIntersection(wf.skipIPs, ips)) + (len(wf.skipIPs) > 0 && wf.hasIntersection(wf.skipIPs, ips)) || + (len(wf.podnames) > 0 && !wf.hasIntersection(wf.podnames, []string{workload.Podname})) } func (wf filter) hasIntersection(a, b []string) bool { @@ -133,6 +146,8 @@ func cmdWorkloadList(c *cli.Context) error { limit: c.Int64("limit"), matchIPs: c.StringSlice("match-ip"), skipIPs: c.StringSlice("skip-ip"), + podnames: c.StringSlice("pod"), + statistics: c.Bool("statistics"), } return o.run(c.Context) } diff --git a/describe/workload.go b/describe/workload.go index 390955d..35ee6d2 100644 --- a/describe/workload.go +++ b/describe/workload.go @@ -23,6 +23,46 @@ func Workloads(workloads ...*corepb.Workload) { } } +// WorkloadsStatistics describes the statistics of the Workloads +func WorkloadsStatistics(workloads ...*corepb.Workload) { + stat := struct { + CPUs float64 + Memory int64 + Storage int64 + }{} + for _, w := range workloads { + stat.CPUs += w.Resource.CpuQuotaRequest + stat.Memory += w.Resource.MemoryRequest + stat.Storage += w.Resource.StorageRequest + } + + describeStatistics := func() { + t := table.NewWriter() + t.SetOutputMirror(os.Stdout) + t.AppendHeader(table.Row{"CPUs", "Memory", "Storage"}) + + rows := [][]string{ + {fmt.Sprintf("%f", stat.CPUs)}, + {fmt.Sprintf("%d", stat.Memory)}, + {fmt.Sprintf("%d", stat.Storage)}, + } + t.AppendRows(toTableRows(rows)) + t.AppendSeparator() + + t.SetStyle(table.StyleLight) + t.Render() + } + + switch { + case isJSON(): + describeAsJSON(stat) + case isYAML(): + describeAsYAML(stat) + default: + describeStatistics() + } +} + func describeWorkloads(workloads []*corepb.Workload) { t := table.NewWriter() t.SetOutputMirror(os.Stdout)