Skip to content

Commit eac78fd

Browse files
addon list for table write package and profile list
addons list and profile list and making pass the test
1 parent bcf69bf commit eac78fd

File tree

3 files changed

+181
-27
lines changed

3 files changed

+181
-27
lines changed

cmd/minikube/cmd/config/addons_list.go

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"k8s.io/minikube/pkg/minikube/out"
3535
"k8s.io/minikube/pkg/minikube/reason"
3636
"k8s.io/minikube/pkg/minikube/style"
37+
"k8s.io/minikube/pkg/minikube/constants"
3738
)
3839

3940
var addonListOutput string
@@ -107,7 +108,7 @@ var printAddonsList = func(cc *config.ClusterConfig, printDocs bool) {
107108
if cc == nil {
108109
tHeader = []string{"Addon Name", "Maintainer"}
109110
} else {
110-
tHeader = []string{"Addon Name", "Profile", "Status", "Maintainer"}
111+
tHeader = []string{"Addon Name", "Enabled", "Maintainer"}
111112
}
112113
if printDocs {
113114
tHeader = append(tHeader, "Docs")
@@ -127,14 +128,80 @@ var printAddonsList = func(cc *config.ClusterConfig, printDocs bool) {
127128
if docs == "" {
128129
docs = "n/a"
129130
}
131+
132+
// Determine base row structure and colors
133+
var enabled bool
134+
var colorCode string
135+
var applyColor bool
136+
137+
if cc != nil {
138+
enabled = addonBundle.IsEnabled(cc)
139+
if enabled {
140+
colorCode = constants.Enabled
141+
applyColor = true
142+
}
143+
}
144+
145+
// Build base row data
146+
var rawValues []interface{}
130147
if cc == nil {
131-
temp = []string{addonName, maintainer}
148+
rawValues = []interface{}{addonName, maintainer}
132149
} else {
133-
enabled := addonBundle.IsEnabled(cc)
134-
temp = []string{addonName, cc.Name, fmt.Sprintf("%s %s", stringFromStatus(enabled), iconFromStatus(enabled)), maintainer}
150+
status := ""
151+
if enabled {
152+
status = iconFromStatus(enabled)
153+
}
154+
rawValues = []interface{}{addonName, status, maintainer}
135155
}
156+
157+
// Add docs if needed
136158
if printDocs {
137-
temp = append(temp, docs)
159+
rawValues = append(rawValues, docs)
160+
}
161+
162+
// Apply colors using loop
163+
temp = make([]string, len(rawValues))
164+
for i, value := range rawValues {
165+
valueStr := fmt.Sprintf("%v", value)
166+
167+
// Apply color based on context
168+
shouldColorValue := false
169+
var valueColorCode string
170+
171+
if cc == nil {
172+
// No coloring for null cluster config
173+
temp[i] = valueStr
174+
continue
175+
}
176+
177+
switch i {
178+
case 0: // addonName
179+
shouldColorValue = applyColor
180+
valueColorCode = colorCode
181+
case 1: // status (only for non-null cc)
182+
shouldColorValue = applyColor
183+
valueColorCode = colorCode
184+
case 2: // maintainer
185+
shouldColorValue = applyColor
186+
valueColorCode = colorCode
187+
default: // docs or other columns
188+
if printDocs && i == len(rawValues)-1 {
189+
// This is the docs column
190+
if enabled {
191+
shouldColorValue = true
192+
valueColorCode = constants.Enabled
193+
} else {
194+
shouldColorValue = true
195+
valueColorCode = constants.Disabled
196+
}
197+
}
198+
}
199+
200+
if shouldColorValue {
201+
temp[i] = fmt.Sprintf("%s%s%s", valueColorCode, valueStr, constants.Default)
202+
} else {
203+
temp[i] = valueStr
204+
}
138205
}
139206
tData = append(tData, temp)
140207
}

cmd/minikube/cmd/config/profile_list.go

Lines changed: 103 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2019 The Kubernetes Authors All rights reserved.
2+
Copyright 2025 The Kubernetes Authors All rights reserved.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -14,6 +14,21 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
/*
18+
Copyright 2019 The Kubernetes Authors All rights reserved.
19+
20+
Licensed under the Apache License, Version 2.0 (the "License");
21+
you may not use this file except in compliance with the License.
22+
You may obtain a copy of the License at
23+
24+
http://www.apache.org/licenses/LICENSE-2.0
25+
26+
Unless required by applicable law or agreed to in writing, software
27+
distributed under the License is distributed on an "AS IS" BASIS,
28+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29+
See the License for the specific language governing permissions and
30+
limitations under the License.
31+
*/
1732
package config
1833

1934
import (
@@ -23,6 +38,7 @@ import (
2338
"strconv"
2439
"strings"
2540

41+
"k8s.io/klog/v2"
2642
"k8s.io/minikube/pkg/minikube/cluster"
2743
"k8s.io/minikube/pkg/minikube/config"
2844
"k8s.io/minikube/pkg/minikube/constants"
@@ -37,8 +53,6 @@ import (
3753
"github.com/olekukonko/tablewriter"
3854
"github.com/olekukonko/tablewriter/tw"
3955
"github.com/spf13/cobra"
40-
41-
"k8s.io/klog/v2"
4256
)
4357

4458
var (
@@ -73,21 +87,17 @@ func listProfiles() (validProfiles, invalidProfiles []*config.Profile, err error
7387
} else {
7488
validProfiles, invalidProfiles, err = config.ListProfiles()
7589
}
76-
7790
return validProfiles, invalidProfiles, err
7891
}
7992

8093
func printProfilesTable() {
8194
validProfiles, invalidProfiles, err := listProfiles()
82-
8395
if err != nil {
8496
klog.Warningf("error loading profiles: %v", err)
8597
}
86-
8798
if len(validProfiles) == 0 {
8899
exit.Message(reason.UsageNoProfileRunning, "No minikube profile was found.")
89100
}
90-
91101
updateProfilesStatus(validProfiles)
92102
renderProfilesTable(profilesToTableData(validProfiles))
93103
warnInvalidProfiles(invalidProfiles)
@@ -100,13 +110,11 @@ func updateProfilesStatus(profiles []*config.Profile) {
100110
}
101111
return
102112
}
103-
104113
api, err := machine.NewAPIClient()
105114
if err != nil {
106115
klog.Errorf("failed to get machine api client %v", err)
107116
}
108117
defer api.Close()
109-
110118
for _, p := range profiles {
111119
p.Status = profileStatus(p, api).StatusName
112120
}
@@ -127,9 +135,7 @@ func profileStatus(p *config.Profile, api libmachine.API) cluster.State {
127135
},
128136
}
129137
}
130-
clusterStatus := cluster.GetState(statuses, ClusterFlagValue(), p.Config)
131-
132-
return clusterStatus
138+
return cluster.GetState(statuses, ClusterFlagValue(), p.Config)
133139
}
134140

135141
func renderProfilesTable(ps [][]string) {
@@ -166,9 +172,8 @@ func profilesToTableData(profiles []*config.Profile) [][]string {
166172
cpIP = cp.IP
167173
cpPort = cp.Port
168174
}
169-
170175
k8sVersion := p.Config.KubernetesConfig.KubernetesVersion
171-
if k8sVersion == constants.NoKubernetesVersion { // for --no-kubernetes flag
176+
if k8sVersion == constants.NoKubernetesVersion {
172177
k8sVersion = "N/A"
173178
}
174179
var c, k string
@@ -179,11 +184,90 @@ func profilesToTableData(profiles []*config.Profile) [][]string {
179184
k = "*"
180185
}
181186
if isDetailed {
182-
data = append(data, []string{p.Name, p.Config.Driver, p.Config.KubernetesConfig.ContainerRuntime,
183-
cpIP, strconv.Itoa(cpPort), k8sVersion, p.Status, strconv.Itoa(len(p.Config.Nodes)), c, k})
187+
var colorCode string
188+
switch p.Status {
189+
case "OK":
190+
colorCode = constants.Enabled
191+
case "Broken", "Error":
192+
colorCode = constants.Error
193+
default:
194+
colorCode = constants.Default
195+
}
196+
197+
rawValues := []interface{}{
198+
p.Name,
199+
p.Config.Driver,
200+
p.Config.KubernetesConfig.ContainerRuntime,
201+
cpIP,
202+
cpPort,
203+
k8sVersion,
204+
p.Status,
205+
len(p.Config.Nodes),
206+
c,
207+
k,
208+
}
209+
210+
row := make([]string, len(rawValues))
211+
for i, value := range rawValues {
212+
switch v := value.(type) {
213+
case string:
214+
row[i] = fmt.Sprintf("%s%s%s", colorCode, v, constants.Default)
215+
case int:
216+
row[i] = fmt.Sprintf("%s%d%s", colorCode, v, constants.Default)
217+
default:
218+
row[i] = fmt.Sprintf("%s%v%s", colorCode, v, constants.Default)
219+
}
220+
}
221+
data = append(data, row)
184222
} else {
185-
data = append(data, []string{p.Name, p.Config.Driver, p.Config.KubernetesConfig.ContainerRuntime,
186-
cpIP, k8sVersion, p.Status, strconv.Itoa(len(p.Config.Nodes)), c, k})
223+
var colorCode string
224+
var applyColor bool
225+
switch p.Status {
226+
case "OK":
227+
colorCode = constants.Enabled
228+
applyColor = true
229+
case "Broken", "Error":
230+
colorCode = constants.Error
231+
applyColor = true
232+
default:
233+
applyColor = false
234+
}
235+
236+
rawValues := []interface{}{
237+
p.Name,
238+
p.Config.Driver,
239+
p.Config.KubernetesConfig.ContainerRuntime,
240+
cpIP,
241+
k8sVersion,
242+
p.Status,
243+
len(p.Config.Nodes),
244+
c,
245+
k,
246+
}
247+
248+
row := make([]string, len(rawValues))
249+
for i, value := range rawValues {
250+
if applyColor {
251+
switch v := value.(type) {
252+
case string:
253+
row[i] = fmt.Sprintf("%s%s%s", colorCode, v, constants.Default)
254+
case int:
255+
row[i] = fmt.Sprintf("%s%d%s", colorCode, v, constants.Default)
256+
default:
257+
row[i] = fmt.Sprintf("%s%v%s", colorCode, v, constants.Default)
258+
}
259+
} else {
260+
switch v := value.(type) {
261+
case string:
262+
row[i] = v
263+
case int:
264+
row[i] = strconv.Itoa(v)
265+
default:
266+
row[i] = fmt.Sprintf("%v", v)
267+
}
268+
}
269+
}
270+
data = append(data, row)
187271
}
188272
}
189273
return data
@@ -193,12 +277,10 @@ func warnInvalidProfiles(invalidProfiles []*config.Profile) {
193277
if invalidProfiles == nil {
194278
return
195279
}
196-
197280
out.WarningT("Found {{.number}} invalid profile(s) ! ", out.V{"number": len(invalidProfiles)})
198281
for _, p := range invalidProfiles {
199282
out.ErrT(style.Empty, "\t "+p.Name)
200283
}
201-
202284
out.ErrT(style.Tip, "You can delete them using the following command(s): ")
203285
for _, p := range invalidProfiles {
204286
out.Errf("\t $ minikube delete -p %s \n", p.Name)
@@ -208,8 +290,7 @@ func warnInvalidProfiles(invalidProfiles []*config.Profile) {
208290
func printProfilesJSON() {
209291
validProfiles, invalidProfiles, err := listProfiles()
210292
updateProfilesStatus(validProfiles)
211-
212-
var body = map[string]interface{}{}
293+
body := map[string]interface{}{}
213294
if err == nil || config.IsNotExist(err) {
214295
body["valid"] = profilesOrDefault(validProfiles)
215296
body["invalid"] = profilesOrDefault(invalidProfiles)

pkg/minikube/constants/constants.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,12 @@ const (
163163

164164
// Mirror CN
165165
AliyunMirror = "registry.cn-hangzhou.aliyuncs.com/google_containers"
166+
167+
//TABLE WRITER COLORS
168+
Enabled = "\033[32m"
169+
Default = "\033[0m"
170+
Disabled = "\033[0m"
171+
Error = "\033[31m"
166172
)
167173

168174
var (

0 commit comments

Comments
 (0)