Skip to content

Commit

Permalink
Merge pull request #602 from chunglu-chou/kpt-table
Browse files Browse the repository at this point in the history
Support the status output in table format for other inventory types
  • Loading branch information
k8s-ci-robot authored Aug 10, 2022
2 parents 2cd8a3a + 854aed0 commit 291703a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/status/printers/printers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 13 additions & 4 deletions cmd/status/printers/table/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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,
Expand Down
26 changes: 24 additions & 2 deletions cmd/status/printers/table/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
}
}

Expand All @@ -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.
Expand All @@ -65,13 +72,28 @@ 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"),
table.MustColumn("status"),
table.MustColumn("conditions"),
table.MustColumn("age"),
table.MustColumn("message"),
invNameColumn,
}

// Print prints the table of resources with their statuses until the
Expand Down

0 comments on commit 291703a

Please sign in to comment.