1
1
/*
2
- Copyright 2019 The Kubernetes Authors All rights reserved.
2
+ Copyright 2025 The Kubernetes Authors All rights reserved.
3
3
4
4
Licensed under the Apache License, Version 2.0 (the "License");
5
5
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
14
14
limitations under the License.
15
15
*/
16
16
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
+ */
17
32
package config
18
33
19
34
import (
@@ -23,6 +38,7 @@ import (
23
38
"strconv"
24
39
"strings"
25
40
41
+ "k8s.io/klog/v2"
26
42
"k8s.io/minikube/pkg/minikube/cluster"
27
43
"k8s.io/minikube/pkg/minikube/config"
28
44
"k8s.io/minikube/pkg/minikube/constants"
@@ -37,8 +53,6 @@ import (
37
53
"github.com/olekukonko/tablewriter"
38
54
"github.com/olekukonko/tablewriter/tw"
39
55
"github.com/spf13/cobra"
40
-
41
- "k8s.io/klog/v2"
42
56
)
43
57
44
58
var (
@@ -73,21 +87,17 @@ func listProfiles() (validProfiles, invalidProfiles []*config.Profile, err error
73
87
} else {
74
88
validProfiles , invalidProfiles , err = config .ListProfiles ()
75
89
}
76
-
77
90
return validProfiles , invalidProfiles , err
78
91
}
79
92
80
93
func printProfilesTable () {
81
94
validProfiles , invalidProfiles , err := listProfiles ()
82
-
83
95
if err != nil {
84
96
klog .Warningf ("error loading profiles: %v" , err )
85
97
}
86
-
87
98
if len (validProfiles ) == 0 {
88
99
exit .Message (reason .UsageNoProfileRunning , "No minikube profile was found." )
89
100
}
90
-
91
101
updateProfilesStatus (validProfiles )
92
102
renderProfilesTable (profilesToTableData (validProfiles ))
93
103
warnInvalidProfiles (invalidProfiles )
@@ -100,13 +110,11 @@ func updateProfilesStatus(profiles []*config.Profile) {
100
110
}
101
111
return
102
112
}
103
-
104
113
api , err := machine .NewAPIClient ()
105
114
if err != nil {
106
115
klog .Errorf ("failed to get machine api client %v" , err )
107
116
}
108
117
defer api .Close ()
109
-
110
118
for _ , p := range profiles {
111
119
p .Status = profileStatus (p , api ).StatusName
112
120
}
@@ -127,9 +135,7 @@ func profileStatus(p *config.Profile, api libmachine.API) cluster.State {
127
135
},
128
136
}
129
137
}
130
- clusterStatus := cluster .GetState (statuses , ClusterFlagValue (), p .Config )
131
-
132
- return clusterStatus
138
+ return cluster .GetState (statuses , ClusterFlagValue (), p .Config )
133
139
}
134
140
135
141
func renderProfilesTable (ps [][]string ) {
@@ -166,9 +172,8 @@ func profilesToTableData(profiles []*config.Profile) [][]string {
166
172
cpIP = cp .IP
167
173
cpPort = cp .Port
168
174
}
169
-
170
175
k8sVersion := p .Config .KubernetesConfig .KubernetesVersion
171
- if k8sVersion == constants .NoKubernetesVersion { // for --no-kubernetes flag
176
+ if k8sVersion == constants .NoKubernetesVersion {
172
177
k8sVersion = "N/A"
173
178
}
174
179
var c , k string
@@ -179,11 +184,90 @@ func profilesToTableData(profiles []*config.Profile) [][]string {
179
184
k = "*"
180
185
}
181
186
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 )
184
222
} 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 )
187
271
}
188
272
}
189
273
return data
@@ -193,12 +277,10 @@ func warnInvalidProfiles(invalidProfiles []*config.Profile) {
193
277
if invalidProfiles == nil {
194
278
return
195
279
}
196
-
197
280
out .WarningT ("Found {{.number}} invalid profile(s) ! " , out.V {"number" : len (invalidProfiles )})
198
281
for _ , p := range invalidProfiles {
199
282
out .ErrT (style .Empty , "\t " + p .Name )
200
283
}
201
-
202
284
out .ErrT (style .Tip , "You can delete them using the following command(s): " )
203
285
for _ , p := range invalidProfiles {
204
286
out .Errf ("\t $ minikube delete -p %s \n " , p .Name )
@@ -208,8 +290,7 @@ func warnInvalidProfiles(invalidProfiles []*config.Profile) {
208
290
func printProfilesJSON () {
209
291
validProfiles , invalidProfiles , err := listProfiles ()
210
292
updateProfilesStatus (validProfiles )
211
-
212
- var body = map [string ]interface {}{}
293
+ body := map [string ]interface {}{}
213
294
if err == nil || config .IsNotExist (err ) {
214
295
body ["valid" ] = profilesOrDefault (validProfiles )
215
296
body ["invalid" ] = profilesOrDefault (invalidProfiles )
0 commit comments