Skip to content

Commit aef1179

Browse files
authored
Merge pull request kubernetes#72418 from liggitt/align-watch-output
fix tabwriter to remember column widths, align `kubectl get -w` output
2 parents fb1a830 + 2b1a2d3 commit aef1179

File tree

19 files changed

+775
-19
lines changed

19 files changed

+775
-19
lines changed

Godeps/Godeps.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Godeps/LICENSES

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/kubectl/cmd/config/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ go_library(
4141
"//staging/src/k8s.io/client-go/tools/clientcmd:go_default_library",
4242
"//staging/src/k8s.io/client-go/tools/clientcmd/api:go_default_library",
4343
"//staging/src/k8s.io/client-go/tools/clientcmd/api/latest:go_default_library",
44+
"//vendor/github.com/liggitt/tabwriter:go_default_library",
4445
"//vendor/github.com/spf13/cobra:go_default_library",
4546
],
4647
)

pkg/kubectl/cmd/config/get_contexts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import (
2121
"io"
2222
"sort"
2323
"strings"
24-
"text/tabwriter"
2524

25+
"github.com/liggitt/tabwriter"
2626
"github.com/spf13/cobra"
2727

2828
utilerrors "k8s.io/apimachinery/pkg/util/errors"

pkg/kubectl/cmd/get/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ go_library(
5555
"//staging/src/k8s.io/client-go/tools/watch:go_default_library",
5656
"//staging/src/k8s.io/client-go/util/integer:go_default_library",
5757
"//staging/src/k8s.io/client-go/util/jsonpath:go_default_library",
58+
"//vendor/github.com/liggitt/tabwriter:go_default_library",
5859
"//vendor/github.com/spf13/cobra:go_default_library",
5960
"//vendor/k8s.io/klog:go_default_library",
6061
"//vendor/vbom.ml/util/sortorder:go_default_library",

pkg/kubectl/cmd/get/customcolumn.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import (
2424
"reflect"
2525
"regexp"
2626
"strings"
27-
"text/tabwriter"
27+
28+
"github.com/liggitt/tabwriter"
2829

2930
"k8s.io/apimachinery/pkg/api/meta"
3031
"k8s.io/apimachinery/pkg/runtime"

pkg/kubectl/cmd/get/get.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ func (o *GetOptions) Run(f cmdutil.Factory, cmd *cobra.Command, args []string) e
537537

538538
if shouldGetNewPrinterForMapping(printer, lastMapping, mapping) {
539539
w.Flush()
540+
w.SetRememberedWidths(nil)
540541

541542
// TODO: this doesn't belong here
542543
// add linebreak between resource groups (if there is more than one)
@@ -654,10 +655,11 @@ func (o *GetOptions) watch(f cmdutil.Factory, cmd *cobra.Command, args []string)
654655
}
655656
}
656657

658+
writer := utilprinters.GetNewTabWriter(o.Out)
659+
657660
// print the current object
658661
if !o.WatchOnly {
659662
var objsToPrint []runtime.Object
660-
writer := utilprinters.GetNewTabWriter(o.Out)
661663

662664
if isList {
663665
objsToPrint, _ = meta.ExtractList(obj)
@@ -702,9 +704,10 @@ func (o *GetOptions) watch(f cmdutil.Factory, cmd *cobra.Command, args []string)
702704
internalGV := mapping.GroupVersionKind.GroupKind().WithVersion(runtime.APIVersionInternal).GroupVersion()
703705
objToPrint = attemptToConvertToInternal(e.Object, legacyscheme.Scheme, internalGV)
704706
}
705-
if err := printer.PrintObj(objToPrint, o.Out); err != nil {
707+
if err := printer.PrintObj(objToPrint, writer); err != nil {
706708
return false, err
707709
}
710+
writer.Flush()
708711
return false, nil
709712
})
710713
return err

pkg/kubectl/cmd/get/get_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,8 +1211,8 @@ func TestWatchLabelSelector(t *testing.T) {
12111211
expected := `NAME READY STATUS RESTARTS AGE
12121212
bar 0/0 0 <unknown>
12131213
foo 0/0 0 <unknown>
1214-
foo 0/0 0 <unknown>
1215-
foo 0/0 0 <unknown>
1214+
foo 0/0 0 <unknown>
1215+
foo 0/0 0 <unknown>
12161216
`
12171217
if e, a := expected, buf.String(); e != a {
12181218
t.Errorf("expected\n%v\ngot\n%v", e, a)
@@ -1262,8 +1262,8 @@ func TestWatchFieldSelector(t *testing.T) {
12621262
expected := `NAME READY STATUS RESTARTS AGE
12631263
bar 0/0 0 <unknown>
12641264
foo 0/0 0 <unknown>
1265-
foo 0/0 0 <unknown>
1266-
foo 0/0 0 <unknown>
1265+
foo 0/0 0 <unknown>
1266+
foo 0/0 0 <unknown>
12671267
`
12681268
if e, a := expected, buf.String(); e != a {
12691269
t.Errorf("expected\n%v\ngot\n%v", e, a)
@@ -1305,8 +1305,8 @@ func TestWatchResource(t *testing.T) {
13051305

13061306
expected := `NAME READY STATUS RESTARTS AGE
13071307
foo 0/0 0 <unknown>
1308-
foo 0/0 0 <unknown>
1309-
foo 0/0 0 <unknown>
1308+
foo 0/0 0 <unknown>
1309+
foo 0/0 0 <unknown>
13101310
`
13111311
if e, a := expected, buf.String(); e != a {
13121312
t.Errorf("expected\n%v\ngot\n%v", e, a)
@@ -1349,8 +1349,8 @@ func TestWatchResourceIdentifiedByFile(t *testing.T) {
13491349

13501350
expected := `NAME READY STATUS RESTARTS AGE
13511351
foo 0/0 0 <unknown>
1352-
foo 0/0 0 <unknown>
1353-
foo 0/0 0 <unknown>
1352+
foo 0/0 0 <unknown>
1353+
foo 0/0 0 <unknown>
13541354
`
13551355
if e, a := expected, buf.String(); e != a {
13561356
t.Errorf("expected\n%v\ngot\n%v", e, a)
@@ -1392,7 +1392,7 @@ func TestWatchOnlyResource(t *testing.T) {
13921392

13931393
expected := `NAME READY STATUS RESTARTS AGE
13941394
foo 0/0 0 <unknown>
1395-
foo 0/0 0 <unknown>
1395+
foo 0/0 0 <unknown>
13961396
`
13971397
if e, a := expected, buf.String(); e != a {
13981398
t.Errorf("expected\n%v\ngot\n%v", e, a)
@@ -1437,7 +1437,7 @@ func TestWatchOnlyList(t *testing.T) {
14371437

14381438
expected := `NAME READY STATUS RESTARTS AGE
14391439
foo 0/0 0 <unknown>
1440-
foo 0/0 0 <unknown>
1440+
foo 0/0 0 <unknown>
14411441
`
14421442
if e, a := expected, buf.String(); e != a {
14431443
t.Errorf("expected\n%v\ngot\n%v", e, a)

pkg/kubectl/util/printers/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go_library(
55
srcs = ["tabwriter.go"],
66
importpath = "k8s.io/kubernetes/pkg/kubectl/util/printers",
77
visibility = ["//visibility:public"],
8+
deps = ["//vendor/github.com/liggitt/tabwriter:go_default_library"],
89
)
910

1011
filegroup(

pkg/kubectl/util/printers/tabwriter.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ package printers
1818

1919
import (
2020
"io"
21-
"text/tabwriter"
21+
22+
"github.com/liggitt/tabwriter"
2223
)
2324

2425
const (
2526
tabwriterMinWidth = 6
2627
tabwriterWidth = 4
2728
tabwriterPadding = 3
2829
tabwriterPadChar = ' '
29-
tabwriterFlags = 0
30+
tabwriterFlags = tabwriter.RememberWidths
3031
)
3132

3233
// GetNewTabWriter returns a tabwriter that translates tabbed columns in input into properly aligned text.

0 commit comments

Comments
 (0)