From 854aed09b90ffd558e229179a4905cc96a5d550b Mon Sep 17 00:00:00 2001 From: Johnnie Chou Date: Tue, 9 Aug 2022 15:27:53 +0000 Subject: [PATCH] add inventory_name column to include this information in table output format --- cmd/status/printers/printers.go | 2 +- cmd/status/printers/table/adapter.go | 17 +++++++++++++---- cmd/status/printers/table/printer.go | 26 ++++++++++++++++++++++++-- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/cmd/status/printers/printers.go b/cmd/status/printers/printers.go index 2751dc75..6fb81caf 100644 --- a/cmd/status/printers/printers.go +++ b/cmd/status/printers/printers.go @@ -15,7 +15,7 @@ import ( func CreatePrinter(printerType string, ioStreams genericclioptions.IOStreams, printData *printer.PrintData) (printer.Printer, error) { switch printerType { case "table": - return table.NewPrinter(ioStreams), nil + return table.NewPrinter(ioStreams, printData), nil default: return event.NewPrinter(ioStreams, printData), nil } diff --git a/cmd/status/printers/table/adapter.go b/cmd/status/printers/table/adapter.go index 40e37ba4..35207504 100644 --- a/cmd/status/printers/table/adapter.go +++ b/cmd/status/printers/table/adapter.go @@ -4,6 +4,8 @@ package table import ( + "strings" + "sigs.k8s.io/cli-utils/pkg/kstatus/polling/collector" pe "sigs.k8s.io/cli-utils/pkg/kstatus/polling/event" "sigs.k8s.io/cli-utils/pkg/object" @@ -14,11 +16,14 @@ import ( // provides a set of functions that matches the interfaces // needed by the BaseTablePrinter. type CollectorAdapter struct { - collector *collector.ResourceStatusCollector + collector *collector.ResourceStatusCollector + invNameMap map[object.ObjMetadata]string + statusSet map[string]bool } type ResourceInfo struct { resourceStatus *pe.ResourceStatus + invName string } func (r *ResourceInfo) Identifier() object.ObjMetadata { @@ -34,6 +39,7 @@ func (r *ResourceInfo) SubResources() []table.Resource { for _, rs := range r.resourceStatus.GeneratedResources { subResources = append(subResources, &ResourceInfo{ resourceStatus: rs, + invName: r.invName, }) } return subResources @@ -56,9 +62,12 @@ func (ca *CollectorAdapter) LatestStatus() *ResourceState { observation := ca.collector.LatestObservation() var resources []table.Resource for _, resourceStatus := range observation.ResourceStatuses { - resources = append(resources, &ResourceInfo{ - resourceStatus: resourceStatus, - }) + if _, ok := ca.statusSet[strings.ToLower(resourceStatus.Status.String())]; len(ca.statusSet) == 0 || ok { + resources = append(resources, &ResourceInfo{ + resourceStatus: resourceStatus, + invName: ca.invNameMap[resourceStatus.Identifier], + }) + } } return &ResourceState{ resources: resources, diff --git a/cmd/status/printers/table/printer.go b/cmd/status/printers/table/printer.go index 9bb93aaa..511e9d87 100644 --- a/cmd/status/printers/table/printer.go +++ b/cmd/status/printers/table/printer.go @@ -4,9 +4,12 @@ package table import ( + "fmt" + "io" "time" "k8s.io/cli-runtime/pkg/genericclioptions" + "sigs.k8s.io/cli-utils/cmd/status/printers/printer" "sigs.k8s.io/cli-utils/pkg/kstatus/polling/collector" "sigs.k8s.io/cli-utils/pkg/kstatus/polling/event" "sigs.k8s.io/cli-utils/pkg/object" @@ -22,12 +25,14 @@ const ( // status information about resources in a table format with in-place updates. type Printer struct { IOStreams genericclioptions.IOStreams + PrintData *printer.PrintData } // NewPrinter returns a new instance of the tablePrinter. -func NewPrinter(ioStreams genericclioptions.IOStreams) *Printer { +func NewPrinter(ioStreams genericclioptions.IOStreams, printData *printer.PrintData) *Printer { return &Printer{ IOStreams: ioStreams, + PrintData: printData, } } @@ -41,7 +46,9 @@ func (t *Printer) Print(ch <-chan event.Event, identifiers object.ObjMetadataSet // Start the goroutine that is responsible for // printing the latest state on a regular cadence. printCompleted := t.runPrintLoop(&CollectorAdapter{ - collector: coll, + collector: coll, + invNameMap: t.PrintData.InvNameMap, + statusSet: t.PrintData.StatusSet, }, stop) // Make the collector start listening on the eventChannel. @@ -65,6 +72,20 @@ func (t *Printer) Print(ch <-chan event.Event, identifiers object.ObjMetadataSet return err } +var invNameColumn = table.ColumnDef{ + ColumnName: "inventory_name", + ColumnHeader: "INVENTORY_NAME", + ColumnWidth: 30, + PrintResourceFunc: func(w io.Writer, width int, r table.Resource) (int, error) { + group := r.(*ResourceInfo).invName + if len(group) > width { + group = group[:width] + } + _, err := fmt.Fprint(w, group) + return len(group), err + }, +} + var columns = []table.ColumnDefinition{ table.MustColumn("namespace"), table.MustColumn("resource"), @@ -72,6 +93,7 @@ var columns = []table.ColumnDefinition{ table.MustColumn("conditions"), table.MustColumn("age"), table.MustColumn("message"), + invNameColumn, } // Print prints the table of resources with their statuses until the