Skip to content

Commit

Permalink
Merge pull request #47013 from smarterclayton/fix_printer
Browse files Browse the repository at this point in the history
Automatic merge from submit-queue (batch tested with PRs 47024, 47050, 47086, 47081, 47013)

Wrap HumanReadablePrinter in tab output unless explicitly asked not to

`kubectl get` was not properly aligning its output due to #40848 

Fixes an accidental regression. In general, we should not accept an incoming tabwriter and instead manage at a higher level. Fix the bug and add a comment re: future refactoring.
  • Loading branch information
Kubernetes Submit Queue authored Jun 7, 2017
2 parents 7e0c9e7 + b1abedb commit 49866b8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
18 changes: 8 additions & 10 deletions pkg/kubectl/cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func Example_printReplicationControllerWithNamespace() {
},
}
mapper, _ := f.Object()
err := f.PrintObject(cmd, mapper, ctrl, printers.GetNewTabWriter(os.Stdout))
err := f.PrintObject(cmd, mapper, ctrl, os.Stdout)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
}
Expand Down Expand Up @@ -252,7 +252,7 @@ func Example_printMultiContainersReplicationControllerWithWide() {
},
}
mapper, _ := f.Object()
err := f.PrintObject(cmd, mapper, ctrl, printers.GetNewTabWriter(os.Stdout))
err := f.PrintObject(cmd, mapper, ctrl, os.Stdout)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
}
Expand Down Expand Up @@ -306,7 +306,7 @@ func Example_printReplicationController() {
},
}
mapper, _ := f.Object()
err := f.PrintObject(cmd, mapper, ctrl, printers.GetNewTabWriter(os.Stdout))
err := f.PrintObject(cmd, mapper, ctrl, os.Stdout)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
}
Expand Down Expand Up @@ -349,7 +349,7 @@ func Example_printPodWithWideFormat() {
},
}
mapper, _ := f.Object()
err := f.PrintObject(cmd, mapper, pod, printers.GetNewTabWriter(os.Stdout))
err := f.PrintObject(cmd, mapper, pod, os.Stdout)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
}
Expand Down Expand Up @@ -395,7 +395,7 @@ func Example_printPodWithShowLabels() {
},
}
mapper, _ := f.Object()
err := f.PrintObject(cmd, mapper, pod, printers.GetNewTabWriter(os.Stdout))
err := f.PrintObject(cmd, mapper, pod, os.Stdout)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
}
Expand Down Expand Up @@ -519,7 +519,7 @@ func Example_printPodHideTerminated() {
}
for _, pod := range filteredPodList {
mapper, _ := f.Object()
err := f.PrintObject(cmd, mapper, pod, printers.GetNewTabWriter(os.Stdout))
err := f.PrintObject(cmd, mapper, pod, os.Stdout)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
}
Expand Down Expand Up @@ -547,7 +547,7 @@ func Example_printPodShowAll() {
cmd := NewCmdRun(f, os.Stdin, os.Stdout, os.Stderr)
podList := newAllPhasePodList()
mapper, _ := f.Object()
err := f.PrintObject(cmd, mapper, podList, printers.GetNewTabWriter(os.Stdout))
err := f.PrintObject(cmd, mapper, podList, os.Stdout)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
}
Expand Down Expand Up @@ -621,10 +621,8 @@ func Example_printServiceWithNamespacesAndLabels() {
}
ld := strings.NewLineDelimiter(os.Stdout, "|")
defer ld.Flush()
out := printers.GetNewTabWriter(ld)
defer out.Flush()
mapper, _ := f.Object()
err := f.PrintObject(cmd, mapper, svc, out)
err := f.PrintObject(cmd, mapper, svc, ld)
if err != nil {
fmt.Printf("Unexpected error: %v", err)
}
Expand Down
28 changes: 20 additions & 8 deletions pkg/printers/humanreadable.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ type handlerEntry struct {
// will only be printed if the object type changes. This makes it useful for printing items
// received from watches.
type HumanReadablePrinter struct {
handlerMap map[reflect.Type]*handlerEntry
options PrintOptions
lastType reflect.Type
hiddenObjNum int
encoder runtime.Encoder
decoder runtime.Decoder
handlerMap map[reflect.Type]*handlerEntry
options PrintOptions
lastType reflect.Type
skipTabWriter bool
encoder runtime.Encoder
decoder runtime.Decoder
}

var _ PrintHandler = &HumanReadablePrinter{}
Expand All @@ -89,6 +89,13 @@ func NewTablePrinter() *HumanReadablePrinter {
}
}

// AddTabWriter sets whether the PrintObj function will format with tabwriter (true
// by default).
func (a *HumanReadablePrinter) AddTabWriter(t bool) *HumanReadablePrinter {
a.skipTabWriter = !t
return a
}

func (a *HumanReadablePrinter) With(fns ...func(PrintHandler)) *HumanReadablePrinter {
for _, fn := range fns {
fn(a)
Expand Down Expand Up @@ -267,9 +274,14 @@ func (h *HumanReadablePrinter) printHeader(columnNames []string, w io.Writer) er
}

// PrintObj prints the obj in a human-friendly format according to the type of the obj.
// TODO: unify the behavior of PrintObj, which often expects single items and tracks
// headers and filtering, with other printers, that expect list objects. The tracking
// behavior should probably be a higher level wrapper (MultiObjectTablePrinter) that
// calls into the PrintTable method and then displays consistent output.
func (h *HumanReadablePrinter) PrintObj(obj runtime.Object, output io.Writer) error {
// if output is a tabwriter (when it's called by kubectl get), we use it; create a new tabwriter otherwise
if w, found := output.(*tabwriter.Writer); found {
if w, found := output.(*tabwriter.Writer); !found && !h.skipTabWriter {
w = GetNewTabWriter(output)
output = w
defer w.Flush()
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/printers/internalversion/printers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,7 @@ func TestPrintPodTable(t *testing.T) {
t.Fatal(err)
}
buf := &bytes.Buffer{}
p := printers.NewHumanReadablePrinter(nil, nil, test.opts).With(AddHandlers)
p := printers.NewHumanReadablePrinter(nil, nil, test.opts).With(AddHandlers).AddTabWriter(false)
if err := p.PrintObj(table, buf); err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 49866b8

Please sign in to comment.