Skip to content

Commit 080f0ed

Browse files
committed
fix: profile list shows host state instead of apiserver state
1 parent 63bfa7a commit 080f0ed

File tree

1 file changed

+80
-58
lines changed

1 file changed

+80
-58
lines changed

cmd/minikube/cmd/config/profile_list.go

+80-58
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ import (
2323
"strconv"
2424
"strings"
2525

26+
"k8s.io/minikube/pkg/minikube/bootstrapper/bsutil/kverify"
2627
"k8s.io/minikube/pkg/minikube/config"
2728
"k8s.io/minikube/pkg/minikube/driver"
2829
"k8s.io/minikube/pkg/minikube/exit"
2930
"k8s.io/minikube/pkg/minikube/machine"
3031
"k8s.io/minikube/pkg/minikube/out"
3132

33+
"github.com/docker/machine/libmachine"
3234
"github.com/golang/glog"
3335
"github.com/olekukonko/tablewriter"
3436
"github.com/spf13/cobra"
@@ -56,98 +58,111 @@ var profileListCmd = &cobra.Command{
5658
},
5759
}
5860

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() {
6762
validProfiles, invalidProfiles, err := config.ListProfiles()
6863

69-
if len(validProfiles) == 0 || err != nil {
64+
if len(validProfiles) == 0 {
7065
exit.UsageT("No minikube profile was found. You can create one using `minikube start`.")
7166
}
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) {
7278
api, err := machine.NewAPIClient()
7379
if err != nil {
7480
glog.Errorf("failed to get machine api client %v", err)
7581
}
7682
defer api.Close()
7783

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)
8886
}
87+
}
8988

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+
}
10294

95+
host, err := machine.LoadHost(api, driver.MachineName(*p.Config, cp))
96+
if err != nil {
97+
return ""
10398
}
10499

100+
cr, err := machine.CommandRunner(host)
105101
if err != nil {
106-
glog.Warningf("error loading profiles: %v", err)
102+
return ""
107103
}
108104

109-
}
105+
hostname, _, port, err := driver.ControlPlaneEndpoint(p.Config, &cp, host.DriverName)
106+
if err != nil {
107+
return ""
108+
}
110109

111-
var printProfilesJSON = func() {
112-
api, err := machine.NewAPIClient()
110+
status, err := kverify.APIServerStatus(cr, hostname, port)
113111
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 ""
115114
}
116-
defer api.Close()
115+
return status.String()
116+
}
117117

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)
121132
if err != nil {
122133
exit.WithError("error getting primary control plane", err)
123134
}
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})
129137
}
138+
return data
139+
}
130140

131-
var valid []*config.Profile
132-
var invalid []*config.Profile
141+
func warnInvalidProfiles(invalidProfiles []*config.Profile) {
142+
if invalidProfiles == nil {
143+
return
144+
}
133145

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)
138149
}
139150

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))
144154
}
155+
}
145156

146-
var body = map[string]interface{}{}
157+
func printProfilesJSON() {
158+
validProfiles, invalidProfiles, err := config.ListProfiles()
147159

160+
updateProfilesStatus(validProfiles)
161+
162+
var body = map[string]interface{}{}
148163
if err == nil || config.IsNotExist(err) {
149-
body["valid"] = valid
150-
body["invalid"] = invalid
164+
body["valid"] = profilesOrDefault(validProfiles)
165+
body["invalid"] = profilesOrDefault(invalidProfiles)
151166
jsonString, _ := json.Marshal(body)
152167
out.String(string(jsonString))
153168
} else {
@@ -158,6 +173,13 @@ var printProfilesJSON = func() {
158173
}
159174
}
160175

176+
func profilesOrDefault(profiles []*config.Profile) []*config.Profile {
177+
if profiles != nil {
178+
return profiles
179+
}
180+
return []*config.Profile{}
181+
}
182+
161183
func init() {
162184
profileListCmd.Flags().StringVarP(&output, "output", "o", "table", "The output format. One of 'json', 'table'")
163185
ProfileCmd.AddCommand(profileListCmd)

0 commit comments

Comments
 (0)