k3s/vendor/k8s.io/cli-runtime/pkg/printers/tableprinter.go

589 lines
17 KiB
Go
Raw Normal View History

2019-08-30 18:33:25 +00:00
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package printers
import (
"fmt"
"io"
"reflect"
"strings"
"time"
"github.com/liggitt/tabwriter"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/duration"
2019-09-27 21:51:53 +00:00
"k8s.io/apimachinery/pkg/watch"
2019-08-30 18:33:25 +00:00
)
var _ ResourcePrinter = &HumanReadablePrinter{}
2019-09-27 21:51:53 +00:00
type printHandler struct {
2020-03-26 21:07:15 +00:00
columnDefinitions []metav1.TableColumnDefinition
2019-09-27 21:51:53 +00:00
printFunc reflect.Value
}
2019-08-30 18:33:25 +00:00
var (
2019-09-27 21:51:53 +00:00
statusHandlerEntry = &printHandler{
columnDefinitions: statusColumnDefinitions,
printFunc: reflect.ValueOf(printStatus),
}
2020-03-26 21:07:15 +00:00
statusColumnDefinitions = []metav1.TableColumnDefinition{
2019-09-27 21:51:53 +00:00
{Name: "Status", Type: "string"},
{Name: "Reason", Type: "string"},
{Name: "Message", Type: "string"},
}
defaultHandlerEntry = &printHandler{
2019-08-30 18:33:25 +00:00
columnDefinitions: objectMetaColumnDefinitions,
printFunc: reflect.ValueOf(printObjectMeta),
}
2020-03-26 21:07:15 +00:00
objectMetaColumnDefinitions = []metav1.TableColumnDefinition{
2019-08-30 18:33:25 +00:00
{Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]},
{Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]},
}
2019-09-27 21:51:53 +00:00
withEventTypePrefixColumns = []string{"EVENT"}
2019-08-30 18:33:25 +00:00
withNamespacePrefixColumns = []string{"NAMESPACE"} // TODO(erictune): print cluster name too.
)
2019-09-27 21:51:53 +00:00
// HumanReadablePrinter is an implementation of ResourcePrinter which attempts to provide
// more elegant output. It is not threadsafe, but you may call PrintObj repeatedly; headers
// will only be printed if the object type changes. This makes it useful for printing items
// received from watches.
type HumanReadablePrinter struct {
options PrintOptions
lastType interface{}
2020-03-26 21:07:15 +00:00
lastColumns []metav1.TableColumnDefinition
2019-09-27 21:51:53 +00:00
printedHeaders bool
}
2019-08-30 18:33:25 +00:00
// NewTablePrinter creates a printer suitable for calling PrintObj().
2019-09-27 21:51:53 +00:00
func NewTablePrinter(options PrintOptions) ResourcePrinter {
2019-08-30 18:33:25 +00:00
printer := &HumanReadablePrinter{
2019-09-27 21:51:53 +00:00
options: options,
2019-08-30 18:33:25 +00:00
}
return printer
}
func printHeader(columnNames []string, w io.Writer) error {
if _, err := fmt.Fprintf(w, "%s\n", strings.Join(columnNames, "\t")); err != nil {
return err
}
return nil
}
// PrintObj prints the obj in a human-friendly format according to the type of the obj.
func (h *HumanReadablePrinter) PrintObj(obj runtime.Object, output io.Writer) error {
2019-09-27 21:51:53 +00:00
if _, found := output.(*tabwriter.Writer); !found {
w := GetNewTabWriter(output)
2019-08-30 18:33:25 +00:00
output = w
defer w.Flush()
}
2019-09-27 21:51:53 +00:00
var eventType string
if event, isEvent := obj.(*metav1.WatchEvent); isEvent {
eventType = event.Type
obj = event.Object.Object
}
// Parameter "obj" is a table from server; print it.
2019-08-30 18:33:25 +00:00
// display tables following the rules of options
2020-03-26 21:07:15 +00:00
if table, ok := obj.(*metav1.Table); ok {
2019-08-30 18:33:25 +00:00
// Do not print headers if this table has no column definitions, or they are the same as the last ones we printed
localOptions := h.options
2019-09-27 21:51:53 +00:00
if h.printedHeaders && (len(table.ColumnDefinitions) == 0 || reflect.DeepEqual(table.ColumnDefinitions, h.lastColumns)) {
2019-08-30 18:33:25 +00:00
localOptions.NoHeaders = true
}
if len(table.ColumnDefinitions) == 0 {
// If this table has no column definitions, use the columns from the last table we printed for decoration and layout.
// This is done when receiving tables in watch events to save bandwidth.
table.ColumnDefinitions = h.lastColumns
2019-09-27 21:51:53 +00:00
} else if !reflect.DeepEqual(table.ColumnDefinitions, h.lastColumns) {
2019-08-30 18:33:25 +00:00
// If this table has column definitions, remember them for future use.
h.lastColumns = table.ColumnDefinitions
2019-09-27 21:51:53 +00:00
h.printedHeaders = false
}
if len(table.Rows) > 0 {
h.printedHeaders = true
2019-08-30 18:33:25 +00:00
}
if err := decorateTable(table, localOptions); err != nil {
return err
}
2019-09-27 21:51:53 +00:00
if len(eventType) > 0 {
if err := addColumns(beginning, table,
2020-03-26 21:07:15 +00:00
[]metav1.TableColumnDefinition{{Name: "Event", Type: "string"}},
[]cellValueFunc{func(metav1.TableRow) (interface{}, error) { return formatEventType(eventType), nil }},
2019-09-27 21:51:53 +00:00
); err != nil {
return err
}
}
2019-08-30 18:33:25 +00:00
return printTable(table, output, localOptions)
}
2019-09-27 21:51:53 +00:00
// Could not find print handler for "obj"; use the default or status print handler.
// Print with the default or status handler, and use the columns from the last time
var handler *printHandler
if _, isStatus := obj.(*metav1.Status); isStatus {
handler = statusHandlerEntry
} else {
handler = defaultHandlerEntry
2019-08-30 18:33:25 +00:00
}
2019-09-27 21:51:53 +00:00
includeHeaders := h.lastType != handler && !h.options.NoHeaders
2019-08-30 18:33:25 +00:00
2019-09-27 21:51:53 +00:00
if h.lastType != nil && h.lastType != handler && !h.options.NoHeaders {
2019-08-30 18:33:25 +00:00
fmt.Fprintln(output)
}
2019-09-27 21:51:53 +00:00
if err := printRowsForHandlerEntry(output, handler, eventType, obj, h.options, includeHeaders); err != nil {
2019-08-30 18:33:25 +00:00
return err
}
2019-09-27 21:51:53 +00:00
h.lastType = handler
2019-08-30 18:33:25 +00:00
return nil
}
// printTable prints a table to the provided output respecting the filtering rules for options
// for wide columns and filtered rows. It filters out rows that are Completed. You should call
// decorateTable if you receive a table from a remote server before calling printTable.
2020-03-26 21:07:15 +00:00
func printTable(table *metav1.Table, output io.Writer, options PrintOptions) error {
2019-08-30 18:33:25 +00:00
if !options.NoHeaders {
// avoid printing headers if we have no rows to display
if len(table.Rows) == 0 {
return nil
}
first := true
for _, column := range table.ColumnDefinitions {
if !options.Wide && column.Priority != 0 {
continue
}
if first {
first = false
} else {
fmt.Fprint(output, "\t")
}
fmt.Fprint(output, strings.ToUpper(column.Name))
}
fmt.Fprintln(output)
}
for _, row := range table.Rows {
first := true
for i, cell := range row.Cells {
if i >= len(table.ColumnDefinitions) {
// https://issue.k8s.io/66379
// don't panic in case of bad output from the server, with more cells than column definitions
break
}
column := table.ColumnDefinitions[i]
if !options.Wide && column.Priority != 0 {
continue
}
if first {
first = false
} else {
fmt.Fprint(output, "\t")
}
if cell != nil {
switch val := cell.(type) {
case string:
print := val
truncated := false
// truncate at newlines
newline := strings.Index(print, "\n")
if newline >= 0 {
truncated = true
print = print[:newline]
}
fmt.Fprint(output, print)
if truncated {
fmt.Fprint(output, "...")
}
default:
fmt.Fprint(output, val)
}
2019-08-30 18:33:25 +00:00
}
}
fmt.Fprintln(output)
}
return nil
}
2020-03-26 21:07:15 +00:00
type cellValueFunc func(metav1.TableRow) (interface{}, error)
2019-09-27 21:51:53 +00:00
type columnAddPosition int
const (
beginning columnAddPosition = 1
end columnAddPosition = 2
)
2020-03-26 21:07:15 +00:00
func addColumns(pos columnAddPosition, table *metav1.Table, columns []metav1.TableColumnDefinition, valueFuncs []cellValueFunc) error {
2019-09-27 21:51:53 +00:00
if len(columns) != len(valueFuncs) {
return fmt.Errorf("cannot prepend columns, unmatched value functions")
}
if len(columns) == 0 {
return nil
}
// Compute the new rows
newRows := make([][]interface{}, len(table.Rows))
for i := range table.Rows {
newCells := make([]interface{}, 0, len(columns)+len(table.Rows[i].Cells))
if pos == end {
// If we're appending, start with the existing cells,
// then add nil cells to match the number of columns
newCells = append(newCells, table.Rows[i].Cells...)
for len(newCells) < len(table.ColumnDefinitions) {
newCells = append(newCells, nil)
}
}
// Compute cells for new columns
for _, f := range valueFuncs {
newCell, err := f(table.Rows[i])
if err != nil {
return err
}
newCells = append(newCells, newCell)
}
if pos == beginning {
// If we're prepending, add existing cells
newCells = append(newCells, table.Rows[i].Cells...)
}
// Remember the new cells for this row
newRows[i] = newCells
}
// All cells successfully computed, now replace columns and rows
2020-03-26 21:07:15 +00:00
newColumns := make([]metav1.TableColumnDefinition, 0, len(columns)+len(table.ColumnDefinitions))
2019-09-27 21:51:53 +00:00
switch pos {
case beginning:
newColumns = append(newColumns, columns...)
newColumns = append(newColumns, table.ColumnDefinitions...)
case end:
newColumns = append(newColumns, table.ColumnDefinitions...)
newColumns = append(newColumns, columns...)
default:
return fmt.Errorf("invalid column add position: %v", pos)
}
table.ColumnDefinitions = newColumns
for i := range table.Rows {
table.Rows[i].Cells = newRows[i]
}
return nil
}
2019-08-30 18:33:25 +00:00
// decorateTable takes a table and attempts to add label columns and the
// namespace column. It will fill empty columns with nil (if the object
// does not expose metadata). It returns an error if the table cannot
// be decorated.
2020-03-26 21:07:15 +00:00
func decorateTable(table *metav1.Table, options PrintOptions) error {
2019-08-30 18:33:25 +00:00
width := len(table.ColumnDefinitions) + len(options.ColumnLabels)
if options.WithNamespace {
width++
}
if options.ShowLabels {
width++
}
columns := table.ColumnDefinitions
nameColumn := -1
if options.WithKind && !options.Kind.Empty() {
for i := range columns {
if columns[i].Format == "name" && columns[i].Type == "string" {
nameColumn = i
break
}
}
}
if width != len(table.ColumnDefinitions) {
2020-03-26 21:07:15 +00:00
columns = make([]metav1.TableColumnDefinition, 0, width)
2019-08-30 18:33:25 +00:00
if options.WithNamespace {
2020-03-26 21:07:15 +00:00
columns = append(columns, metav1.TableColumnDefinition{
2019-08-30 18:33:25 +00:00
Name: "Namespace",
Type: "string",
})
}
columns = append(columns, table.ColumnDefinitions...)
for _, label := range formatLabelHeaders(options.ColumnLabels) {
2020-03-26 21:07:15 +00:00
columns = append(columns, metav1.TableColumnDefinition{
2019-08-30 18:33:25 +00:00
Name: label,
Type: "string",
})
}
if options.ShowLabels {
2020-03-26 21:07:15 +00:00
columns = append(columns, metav1.TableColumnDefinition{
2019-08-30 18:33:25 +00:00
Name: "Labels",
Type: "string",
})
}
}
rows := table.Rows
includeLabels := len(options.ColumnLabels) > 0 || options.ShowLabels
if includeLabels || options.WithNamespace || nameColumn != -1 {
for i := range rows {
row := rows[i]
if nameColumn != -1 {
row.Cells[nameColumn] = fmt.Sprintf("%s/%s", strings.ToLower(options.Kind.String()), row.Cells[nameColumn])
}
var m metav1.Object
if obj := row.Object.Object; obj != nil {
if acc, err := meta.Accessor(obj); err == nil {
m = acc
}
}
// if we can't get an accessor, fill out the appropriate columns with empty spaces
if m == nil {
if options.WithNamespace {
r := make([]interface{}, 1, width)
row.Cells = append(r, row.Cells...)
}
for j := 0; j < width-len(row.Cells); j++ {
row.Cells = append(row.Cells, nil)
}
rows[i] = row
continue
}
if options.WithNamespace {
r := make([]interface{}, 1, width)
r[0] = m.GetNamespace()
row.Cells = append(r, row.Cells...)
}
if includeLabels {
row.Cells = appendLabelCells(row.Cells, m.GetLabels(), options)
}
rows[i] = row
}
}
table.ColumnDefinitions = columns
table.Rows = rows
return nil
}
// printRowsForHandlerEntry prints the incremental table output (headers if the current type is
// different from lastType) including all the rows in the object. It returns the current type
// or an error, if any.
2019-09-27 21:51:53 +00:00
func printRowsForHandlerEntry(output io.Writer, handler *printHandler, eventType string, obj runtime.Object, options PrintOptions, includeHeaders bool) error {
2019-08-30 18:33:25 +00:00
var results []reflect.Value
args := []reflect.Value{reflect.ValueOf(obj), reflect.ValueOf(options)}
results = handler.printFunc.Call(args)
if !results[1].IsNil() {
return results[1].Interface().(error)
}
if includeHeaders {
var headers []string
for _, column := range handler.columnDefinitions {
if column.Priority != 0 && !options.Wide {
continue
}
headers = append(headers, strings.ToUpper(column.Name))
}
headers = append(headers, formatLabelHeaders(options.ColumnLabels)...)
// LABELS is always the last column.
headers = append(headers, formatShowLabelsHeader(options.ShowLabels)...)
2019-09-27 21:51:53 +00:00
// prepend namespace header
2019-08-30 18:33:25 +00:00
if options.WithNamespace {
headers = append(withNamespacePrefixColumns, headers...)
}
2019-09-27 21:51:53 +00:00
// prepend event type header
if len(eventType) > 0 {
headers = append(withEventTypePrefixColumns, headers...)
}
2019-08-30 18:33:25 +00:00
printHeader(headers, output)
}
if results[1].IsNil() {
2020-03-26 21:07:15 +00:00
rows := results[0].Interface().([]metav1.TableRow)
2019-09-27 21:51:53 +00:00
printRows(output, eventType, rows, options)
2019-08-30 18:33:25 +00:00
return nil
}
return results[1].Interface().(error)
}
2019-09-27 21:51:53 +00:00
var formattedEventType = map[string]string{
string(watch.Added): "ADDED ",
string(watch.Modified): "MODIFIED",
string(watch.Deleted): "DELETED ",
string(watch.Error): "ERROR ",
}
func formatEventType(eventType string) string {
if formatted, ok := formattedEventType[eventType]; ok {
return formatted
}
return string(eventType)
}
2019-08-30 18:33:25 +00:00
// printRows writes the provided rows to output.
2020-03-26 21:07:15 +00:00
func printRows(output io.Writer, eventType string, rows []metav1.TableRow, options PrintOptions) {
2019-08-30 18:33:25 +00:00
for _, row := range rows {
2019-09-27 21:51:53 +00:00
if len(eventType) > 0 {
fmt.Fprint(output, formatEventType(eventType))
fmt.Fprint(output, "\t")
}
2019-08-30 18:33:25 +00:00
if options.WithNamespace {
if obj := row.Object.Object; obj != nil {
if m, err := meta.Accessor(obj); err == nil {
fmt.Fprint(output, m.GetNamespace())
}
}
fmt.Fprint(output, "\t")
}
for i, cell := range row.Cells {
if i != 0 {
fmt.Fprint(output, "\t")
} else {
// TODO: remove this once we drop the legacy printers
if options.WithKind && !options.Kind.Empty() {
fmt.Fprintf(output, "%s/%s", strings.ToLower(options.Kind.String()), cell)
continue
}
}
fmt.Fprint(output, cell)
}
hasLabels := len(options.ColumnLabels) > 0
if obj := row.Object.Object; obj != nil && (hasLabels || options.ShowLabels) {
if m, err := meta.Accessor(obj); err == nil {
for _, value := range labelValues(m.GetLabels(), options) {
output.Write([]byte("\t"))
output.Write([]byte(value))
}
}
}
output.Write([]byte("\n"))
}
}
func formatLabelHeaders(columnLabels []string) []string {
formHead := make([]string, len(columnLabels))
for i, l := range columnLabels {
p := strings.Split(l, "/")
formHead[i] = strings.ToUpper((p[len(p)-1]))
}
return formHead
}
// headers for --show-labels=true
func formatShowLabelsHeader(showLabels bool) []string {
if showLabels {
return []string{"LABELS"}
}
return nil
}
// labelValues returns a slice of value columns matching the requested print options.
func labelValues(itemLabels map[string]string, opts PrintOptions) []string {
var values []string
for _, key := range opts.ColumnLabels {
values = append(values, itemLabels[key])
}
if opts.ShowLabels {
values = append(values, labels.FormatLabels(itemLabels))
}
return values
}
// appendLabelCells returns a slice of value columns matching the requested print options.
// Intended for use with tables.
func appendLabelCells(values []interface{}, itemLabels map[string]string, opts PrintOptions) []interface{} {
for _, key := range opts.ColumnLabels {
values = append(values, itemLabels[key])
}
if opts.ShowLabels {
values = append(values, labels.FormatLabels(itemLabels))
}
return values
}
2020-03-26 21:07:15 +00:00
func printStatus(obj runtime.Object, options PrintOptions) ([]metav1.TableRow, error) {
2019-09-27 21:51:53 +00:00
status, ok := obj.(*metav1.Status)
if !ok {
return nil, fmt.Errorf("expected *v1.Status, got %T", obj)
}
2020-03-26 21:07:15 +00:00
return []metav1.TableRow{{
2019-09-27 21:51:53 +00:00
Object: runtime.RawExtension{Object: obj},
Cells: []interface{}{status.Status, status.Reason, status.Message},
}}, nil
}
2020-03-26 21:07:15 +00:00
func printObjectMeta(obj runtime.Object, options PrintOptions) ([]metav1.TableRow, error) {
2019-08-30 18:33:25 +00:00
if meta.IsListType(obj) {
2020-03-26 21:07:15 +00:00
rows := make([]metav1.TableRow, 0, 16)
2019-08-30 18:33:25 +00:00
err := meta.EachListItem(obj, func(obj runtime.Object) error {
nestedRows, err := printObjectMeta(obj, options)
if err != nil {
return err
}
rows = append(rows, nestedRows...)
return nil
})
if err != nil {
return nil, err
}
return rows, nil
}
2020-03-26 21:07:15 +00:00
rows := make([]metav1.TableRow, 0, 1)
2019-08-30 18:33:25 +00:00
m, err := meta.Accessor(obj)
if err != nil {
return nil, err
}
2020-03-26 21:07:15 +00:00
row := metav1.TableRow{
2019-08-30 18:33:25 +00:00
Object: runtime.RawExtension{Object: obj},
}
row.Cells = append(row.Cells, m.GetName(), translateTimestampSince(m.GetCreationTimestamp()))
rows = append(rows, row)
return rows, nil
}
// translateTimestampSince returns the elapsed time since timestamp in
// human-readable approximation.
func translateTimestampSince(timestamp metav1.Time) string {
if timestamp.IsZero() {
return "<unknown>"
}
return duration.HumanDuration(time.Since(timestamp.Time))
}