@@ -23,12 +23,14 @@ import (
23
23
"strconv"
24
24
"strings"
25
25
26
+ "k8s.io/minikube/pkg/minikube/bootstrapper/bsutil/kverify"
26
27
"k8s.io/minikube/pkg/minikube/config"
27
28
"k8s.io/minikube/pkg/minikube/driver"
28
29
"k8s.io/minikube/pkg/minikube/exit"
29
30
"k8s.io/minikube/pkg/minikube/machine"
30
31
"k8s.io/minikube/pkg/minikube/out"
31
32
33
+ "github.com/docker/machine/libmachine"
32
34
"github.com/golang/glog"
33
35
"github.com/olekukonko/tablewriter"
34
36
"github.com/spf13/cobra"
@@ -56,98 +58,111 @@ var profileListCmd = &cobra.Command{
56
58
},
57
59
}
58
60
59
- var printProfilesTable = func () {
60
-
61
- var validData [][]string
62
- table := tablewriter .NewWriter (os .Stdout )
63
- table .SetHeader ([]string {"Profile" , "VM Driver" , "Runtime" , "IP" , "Port" , "Version" , "Status" })
64
- table .SetAutoFormatHeaders (false )
65
- table .SetBorders (tablewriter.Border {Left : true , Top : true , Right : true , Bottom : true })
66
- table .SetCenterSeparator ("|" )
61
+ func printProfilesTable () {
67
62
validProfiles , invalidProfiles , err := config .ListProfiles ()
68
63
69
- if len (validProfiles ) == 0 || err != nil {
64
+ if len (validProfiles ) == 0 {
70
65
exit .UsageT ("No minikube profile was found. You can create one using `minikube start`." )
71
66
}
67
+
68
+ if err != nil {
69
+ glog .Warningf ("error loading profiles: %v" , err )
70
+ }
71
+
72
+ updateProfilesStatus (validProfiles )
73
+ renderProfilesTable (profilesToTableData (validProfiles ))
74
+ warnInvalidProfiles (invalidProfiles )
75
+ }
76
+
77
+ func updateProfilesStatus (profiles []* config.Profile ) {
72
78
api , err := machine .NewAPIClient ()
73
79
if err != nil {
74
80
glog .Errorf ("failed to get machine api client %v" , err )
75
81
}
76
82
defer api .Close ()
77
83
78
- for _ , p := range validProfiles {
79
- cp , err := config .PrimaryControlPlane (p .Config )
80
- if err != nil {
81
- exit .WithError ("error getting primary control plane" , err )
82
- }
83
- p .Status , err = machine .Status (api , driver .MachineName (* p .Config , cp ))
84
- if err != nil {
85
- glog .Warningf ("error getting host status for %s: %v" , p .Name , err )
86
- }
87
- validData = append (validData , []string {p .Name , p .Config .Driver , p .Config .KubernetesConfig .ContainerRuntime , cp .IP , strconv .Itoa (cp .Port ), p .Config .KubernetesConfig .KubernetesVersion , p .Status })
84
+ for _ , p := range profiles {
85
+ p .Status = profileStatus (p , api )
88
86
}
87
+ }
89
88
90
- table .AppendBulk (validData )
91
- table .Render ()
92
-
93
- if invalidProfiles != nil {
94
- out .WarningT ("Found {{.number}} invalid profile(s) ! " , out.V {"number" : len (invalidProfiles )})
95
- for _ , p := range invalidProfiles {
96
- out .ErrT (out .Empty , "\t " + p .Name )
97
- }
98
- out .ErrT (out .Tip , "You can delete them using the following command(s): " )
99
- for _ , p := range invalidProfiles {
100
- out .Err (fmt .Sprintf ("\t $ minikube delete -p %s \n " , p .Name ))
101
- }
89
+ func profileStatus (p * config.Profile , api libmachine.API ) string {
90
+ cp , err := config .PrimaryControlPlane (p .Config )
91
+ if err != nil {
92
+ exit .WithError ("error getting primary control plane" , err )
93
+ }
102
94
95
+ host , err := machine .LoadHost (api , driver .MachineName (* p .Config , cp ))
96
+ if err != nil {
97
+ return ""
103
98
}
104
99
100
+ cr , err := machine .CommandRunner (host )
105
101
if err != nil {
106
- glog . Warningf ( "error loading profiles: %v" , err )
102
+ return ""
107
103
}
108
104
109
- }
105
+ hostname , _ , port , err := driver .ControlPlaneEndpoint (p .Config , & cp , host .DriverName )
106
+ if err != nil {
107
+ return ""
108
+ }
110
109
111
- var printProfilesJSON = func () {
112
- api , err := machine .NewAPIClient ()
110
+ status , err := kverify .APIServerStatus (cr , hostname , port )
113
111
if err != nil {
114
- glog .Errorf ("failed to get machine api client %v" , err )
112
+ glog .Warningf ("error getting apiserver status for %s: %v" , p .Name , err )
113
+ return ""
115
114
}
116
- defer api .Close ()
115
+ return status .String ()
116
+ }
117
117
118
- validProfiles , invalidProfiles , err := config .ListProfiles ()
119
- for _ , v := range validProfiles {
120
- cp , err := config .PrimaryControlPlane (v .Config )
118
+ func renderProfilesTable (ps [][]string ) {
119
+ table := tablewriter .NewWriter (os .Stdout )
120
+ table .SetHeader ([]string {"Profile" , "VM Driver" , "Runtime" , "IP" , "Port" , "Version" , "Status" })
121
+ table .SetAutoFormatHeaders (false )
122
+ table .SetBorders (tablewriter.Border {Left : true , Top : true , Right : true , Bottom : true })
123
+ table .SetCenterSeparator ("|" )
124
+ table .AppendBulk (ps )
125
+ table .Render ()
126
+ }
127
+
128
+ func profilesToTableData (profiles []* config.Profile ) [][]string {
129
+ var data [][]string
130
+ for _ , p := range profiles {
131
+ cp , err := config .PrimaryControlPlane (p .Config )
121
132
if err != nil {
122
133
exit .WithError ("error getting primary control plane" , err )
123
134
}
124
- status , err := machine .Status (api , driver .MachineName (* v .Config , cp ))
125
- if err != nil {
126
- glog .Warningf ("error getting host status for %s: %v" , v .Name , err )
127
- }
128
- v .Status = status
135
+
136
+ data = append (data , []string {p .Name , p .Config .Driver , p .Config .KubernetesConfig .ContainerRuntime , cp .IP , strconv .Itoa (cp .Port ), p .Config .KubernetesConfig .KubernetesVersion , p .Status })
129
137
}
138
+ return data
139
+ }
130
140
131
- var valid []* config.Profile
132
- var invalid []* config.Profile
141
+ func warnInvalidProfiles (invalidProfiles []* config.Profile ) {
142
+ if invalidProfiles == nil {
143
+ return
144
+ }
133
145
134
- if validProfiles != nil {
135
- valid = validProfiles
136
- } else {
137
- valid = []* config.Profile {}
146
+ out .WarningT ("Found {{.number}} invalid profile(s) ! " , out.V {"number" : len (invalidProfiles )})
147
+ for _ , p := range invalidProfiles {
148
+ out .ErrT (out .Empty , "\t " + p .Name )
138
149
}
139
150
140
- if invalidProfiles != nil {
141
- invalid = invalidProfiles
142
- } else {
143
- invalid = []* config.Profile {}
151
+ out .ErrT (out .Tip , "You can delete them using the following command(s): " )
152
+ for _ , p := range invalidProfiles {
153
+ out .Err (fmt .Sprintf ("\t $ minikube delete -p %s \n " , p .Name ))
144
154
}
155
+ }
145
156
146
- var body = map [string ]interface {}{}
157
+ func printProfilesJSON () {
158
+ validProfiles , invalidProfiles , err := config .ListProfiles ()
147
159
160
+ updateProfilesStatus (validProfiles )
161
+
162
+ var body = map [string ]interface {}{}
148
163
if err == nil || config .IsNotExist (err ) {
149
- body ["valid" ] = valid
150
- body ["invalid" ] = invalid
164
+ body ["valid" ] = profilesOrDefault ( validProfiles )
165
+ body ["invalid" ] = profilesOrDefault ( invalidProfiles )
151
166
jsonString , _ := json .Marshal (body )
152
167
out .String (string (jsonString ))
153
168
} else {
@@ -158,6 +173,13 @@ var printProfilesJSON = func() {
158
173
}
159
174
}
160
175
176
+ func profilesOrDefault (profiles []* config.Profile ) []* config.Profile {
177
+ if profiles != nil {
178
+ return profiles
179
+ }
180
+ return []* config.Profile {}
181
+ }
182
+
161
183
func init () {
162
184
profileListCmd .Flags ().StringVarP (& output , "output" , "o" , "table" , "The output format. One of 'json', 'table'" )
163
185
ProfileCmd .AddCommand (profileListCmd )
0 commit comments