mirror of https://github.com/fluxcd/cli-utils.git
Merge pull request #602 from chunglu-chou/kpt-table
Support the status output in table format for other inventory types
This commit is contained in:
commit
291703aeff
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue