Skip to content

Commit 958ecac

Browse files
Refactor table rendering (#20893)
* Refactored table rendering codes to support updated tablewriter v1.0.7 This commit makes all necessary code changes to maintain compatibility with the new tablewriter version. The update includes several required modifications across multiple files. Files modified: - cmd/minikube/cmd/config/images.go - cmd/minikube/cmd/config/addons_list.go - cmd/minikube/cmd/config/profile_list.go - hack/benchmark/time-to-k8s/chart.go - hack/benchmark/time-to-k8s/cpu.go - pkg/minikube/audit/row.go - pkg/minikube/machine/cache_images.go - pkg/minikube/perf/result_manager.go - pkg/minikube/service/service.go Now #20878 can be merged. Addresses #20879 * The tablewriter package v1.0.7 renamed SetHeaders to SetHeader * updated go mod and table.SetColumnAlignment([]int{0, 0, 0, 0}) * Changed syntax and added vendor to gitignore * simplified version of tablewriter * removed vendor * fix addon_list * fix images tablewrtier * bump tablewriter for profile list * go mod tidy * bump tablewriter for chart * bump tablewriter for cpu chart * bump tablewriter for row * bump tablewriter cache images * bump tablewriter * bump tablewriter * revert file * bump tablewritter * fix unit test * fix lint issues * fix the integration test pipe char * convert all tables to new pipe instead of asci pipe * convert all tables to new pipe instead of asci pipe * go mod tidy * go work sync --------- Co-authored-by: Medya Ghazizadeh <[email protected]>
1 parent d850b69 commit 958ecac

File tree

19 files changed

+181
-162
lines changed

19 files changed

+181
-162
lines changed

cmd/minikube/cmd/config/addons_list.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strings"
2525

2626
"github.com/olekukonko/tablewriter"
27+
"github.com/olekukonko/tablewriter/tw"
2728
"github.com/spf13/cobra"
2829
"k8s.io/klog/v2"
2930
"k8s.io/minikube/pkg/minikube/assets"
@@ -96,9 +97,10 @@ var printAddonsList = func(cc *config.ClusterConfig, printDocs bool) {
9697
sort.Strings(addonNames)
9798

9899
table := tablewriter.NewWriter(os.Stdout)
99-
table.SetAutoFormatHeaders(true)
100-
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
101-
table.SetCenterSeparator("|")
100+
101+
table.Options(
102+
tablewriter.WithHeaderAutoFormat(tw.On),
103+
)
102104

103105
// Create table header
104106
var tHeader []string
@@ -110,7 +112,7 @@ var printAddonsList = func(cc *config.ClusterConfig, printDocs bool) {
110112
if printDocs {
111113
tHeader = append(tHeader, "Docs")
112114
}
113-
table.SetHeader(tHeader)
115+
table.Header(tHeader)
114116

115117
// Create table data
116118
var tData [][]string
@@ -136,10 +138,12 @@ var printAddonsList = func(cc *config.ClusterConfig, printDocs bool) {
136138
}
137139
tData = append(tData, temp)
138140
}
139-
table.AppendBulk(tData)
140-
141-
table.Render()
142-
141+
if err := table.Bulk(tData); err != nil {
142+
klog.Error("Error rendering table (bulk)", err)
143+
}
144+
if err := table.Render(); err != nil {
145+
klog.Error("Error rendering table", err)
146+
}
143147
v, _, err := config.ListProfiles()
144148
if err != nil {
145149
klog.Errorf("list profiles returned error: %v", err)

cmd/minikube/cmd/config/addons_list_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func TestAddonsList(t *testing.T) {
3232
printDocs bool
3333
want int
3434
}{
35-
{"DisabledDocs", false, 9},
36-
{"EnabledDocs", true, 12},
35+
{"DisabledDocs", false, 3},
36+
{"EnabledDocs", true, 4},
3737
}
3838

3939
for _, tt := range tests {
@@ -57,17 +57,19 @@ func TestAddonsList(t *testing.T) {
5757
if !buf.Scan() {
5858
t.Fatalf("failed to read stdout")
5959
}
60-
pipeCount += strings.Count(buf.Text(), "|")
60+
pipeCount += strings.Count(buf.Text(), "")
6161
got += buf.Text()
6262
}
6363
if err := buf.Err(); err != nil {
6464
t.Errorf("failed to read stdout: %v", err)
6565
}
66-
// The lines we pull should look something like
67-
// |------------|------------|(------|)
68-
// | ADDON NAME | MAINTAINER |( DOCS |)
69-
// |------------|------------|(------|)
70-
// which has 9 or 12 pipes
66+
// ┌─────────────────────────────┬────────────────────────────────────────┐
67+
// │ ADDON NAME │ MAINTAINER │
68+
// ├─────────────────────────────┼────────────────────────────────────────┤
69+
// ┌─────────────────────────────┬────────────────────────────────────────┬───────────────────────────────────────────────────────────────────────────────┐
70+
// │ ADDON NAME │ MAINTAINER │ DOCS │
71+
// ├─────────────────────────────┼────────────────────────────────────────┼───────────────────────────────────────────────────────────────────────────────┤
72+
7173
expected := tt.want
7274
if pipeCount != expected {
7375
t.Errorf("Expected header to have %d pipes; got = %d: %q", expected, pipeCount, got)

cmd/minikube/cmd/config/images.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import (
2323
"strings"
2424

2525
"github.com/olekukonko/tablewriter"
26+
"github.com/olekukonko/tablewriter/tw"
2627
"github.com/spf13/cobra"
28+
"k8s.io/klog/v2"
2729
"k8s.io/minikube/pkg/minikube/assets"
2830
"k8s.io/minikube/pkg/minikube/exit"
2931
"k8s.io/minikube/pkg/minikube/out"
@@ -63,17 +65,22 @@ func printAddonImagesTable(addon string) {
6365

6466
var tData [][]string
6567
table := tablewriter.NewWriter(os.Stdout)
66-
table.SetHeader([]string{"Image Name", "Default Image", "Default Registry"})
67-
table.SetAutoFormatHeaders(true)
68-
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
69-
table.SetCenterSeparator("|")
68+
table.Header([]string{"Image Name", "Default Image", "Default Registry"})
69+
table.Header("Image Name", "Default Image", "Default Registry")
70+
table.Options(
71+
tablewriter.WithHeaderAutoFormat(tw.On),
72+
)
7073

7174
for imageName, defaultImage := range conf.Images {
7275
tData = append(tData, []string{imageName, defaultImage, conf.Registries[imageName]})
7376
}
7477

75-
table.AppendBulk(tData)
76-
table.Render()
78+
if err := table.Bulk(tData); err != nil {
79+
klog.Error("Error rendering table (bulk)", err)
80+
}
81+
if err := table.Render(); err != nil {
82+
klog.Error("Error rendering table", err)
83+
}
7784
} else {
7885
out.Infof("{{.name}} doesn't have images.", out.V{"name": addon})
7986
}

cmd/minikube/cmd/config/profile_list.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535

3636
"github.com/docker/machine/libmachine"
3737
"github.com/olekukonko/tablewriter"
38+
"github.com/olekukonko/tablewriter/tw"
3839
"github.com/spf13/cobra"
3940

4041
"k8s.io/klog/v2"
@@ -134,17 +135,21 @@ func profileStatus(p *config.Profile, api libmachine.API) cluster.State {
134135
func renderProfilesTable(ps [][]string) {
135136
table := tablewriter.NewWriter(os.Stdout)
136137
if isDetailed {
137-
table.SetHeader([]string{"Profile", "Driver", "Runtime", "IP", "Port", "Version",
138-
"Status", "Nodes", "Active Profile", "Active Kubecontext"})
138+
table.Header("Profile", "Driver", "Runtime", "IP", "Port", "Version",
139+
"Status", "Nodes", "Active Profile", "Active Kubecontext")
139140
} else {
140-
table.SetHeader([]string{"Profile", "Driver", "Runtime", "IP", "Version", "Status",
141-
"Nodes", "Active Profile", "Active Kubecontext"})
142-
}
143-
table.SetAutoFormatHeaders(false)
144-
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
145-
table.SetCenterSeparator("|")
146-
table.AppendBulk(ps)
147-
table.Render()
141+
table.Header("Profile", "Driver", "Runtime", "IP", "Version", "Status",
142+
"Nodes", "Active Profile", "Active Kubecontext")
143+
}
144+
table.Options(
145+
tablewriter.WithHeaderAutoFormat(tw.Off),
146+
)
147+
if err := table.Bulk(ps); err != nil {
148+
klog.Error("Error while bulk render table: ", err)
149+
}
150+
if err := table.Render(); err != nil {
151+
klog.Error("Error while rendering profile table: ", err)
152+
}
148153
}
149154

150155
func profilesToTableData(profiles []*config.Profile) [][]string {

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ require (
4545
github.com/mitchellh/go-ps v1.0.0
4646
github.com/moby/hyperkit v0.0.0-20210108224842-2f061e447e14
4747
github.com/moby/patternmatcher v0.6.0
48-
github.com/olekukonko/tablewriter v0.0.5
48+
github.com/olekukonko/tablewriter v1.0.8
4949
github.com/opencontainers/cgroups v0.0.1
5050
github.com/opencontainers/go-digest v1.0.0
5151
github.com/otiai10/copy v1.14.1
@@ -181,6 +181,8 @@ require (
181181
github.com/muesli/reflow v0.3.0 // indirect
182182
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
183183
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
184+
github.com/olekukonko/errors v0.0.0-20250405072817-4e6d85265da6 // indirect
185+
github.com/olekukonko/ll v0.0.8 // indirect
184186
github.com/opencontainers/image-spec v1.1.0 // indirect
185187
github.com/otiai10/mint v1.6.3 // indirect
186188
github.com/pelletier/go-toml/v2 v2.2.3 // indirect

go.sum

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,6 @@ github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/
11781178
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
11791179
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
11801180
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
1181-
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
11821181
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
11831182
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
11841183
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
@@ -1246,8 +1245,12 @@ github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f h1:y5//uYreIhSUg3J
12461245
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw=
12471246
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
12481247
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
1249-
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
1250-
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
1248+
github.com/olekukonko/errors v0.0.0-20250405072817-4e6d85265da6 h1:r3FaAI0NZK3hSmtTDrBVREhKULp8oUeqLT5Eyl2mSPo=
1249+
github.com/olekukonko/errors v0.0.0-20250405072817-4e6d85265da6/go.mod h1:ppzxA5jBKcO1vIpCXQ9ZqgDh8iwODz6OXIGKU8r5m4Y=
1250+
github.com/olekukonko/ll v0.0.8 h1:sbGZ1Fx4QxJXEqL/6IG8GEFnYojUSQ45dJVwN2FH2fc=
1251+
github.com/olekukonko/ll v0.0.8/go.mod h1:En+sEW0JNETl26+K8eZ6/W4UQ7CYSrrgg/EdIYT2H8g=
1252+
github.com/olekukonko/tablewriter v1.0.8 h1:f6wJzHg4QUtJdvrVPKco4QTrAylgaU0+b9br/lJxEiQ=
1253+
github.com/olekukonko/tablewriter v1.0.8/go.mod h1:H428M+HzoUXC6JU2Abj9IT9ooRmdq9CxuDmKMtrOCMs=
12511254
github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
12521255
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
12531256
github.com/onsi/ginkgo v1.11.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=

0 commit comments

Comments
 (0)