Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: "profile list" shows paused clusters as "Running" #8978

Merged
merged 1 commit into from
Oct 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 82 additions & 55 deletions cmd/minikube/cmd/config/profile_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strconv"
"strings"

"k8s.io/minikube/pkg/minikube/bootstrapper/bsutil/kverify"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/exit"
Expand All @@ -31,6 +32,7 @@ import (
"k8s.io/minikube/pkg/minikube/reason"
"k8s.io/minikube/pkg/minikube/style"

"github.com/docker/machine/libmachine"
"github.com/golang/glog"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
Expand All @@ -54,96 +56,114 @@ var profileListCmd = &cobra.Command{
},
}

var printProfilesTable = func() {
var validData [][]string
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Profile", "VM Driver", "Runtime", "IP", "Port", "Version", "Status"})
table.SetAutoFormatHeaders(false)
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
table.SetCenterSeparator("|")
func printProfilesTable() {
validProfiles, invalidProfiles, err := config.ListProfiles()

if len(validProfiles) == 0 || err != nil {
if err != nil {
taciomcosta marked this conversation as resolved.
Show resolved Hide resolved
glog.Warningf("error loading profiles: %v", err)
}

if len(validProfiles) == 0 {
exit.Message(reason.Usage, "No minikube profile was found. You can create one using `minikube start`.")
}

updateProfilesStatus(validProfiles)
renderProfilesTable(profilesToTableData(validProfiles))
warnInvalidProfiles(invalidProfiles)
}

func updateProfilesStatus(profiles []*config.Profile) {
api, err := machine.NewAPIClient()
if err != nil {
glog.Errorf("failed to get machine api client %v", err)
}
defer api.Close()

for _, p := range validProfiles {
cp, err := config.PrimaryControlPlane(p.Config)
if err != nil {
exit.Error(reason.GuestCpConfig, "error getting primary control plane", err)
}
p.Status, err = machine.Status(api, driver.MachineName(*p.Config, cp))
if err != nil {
glog.Warningf("error getting host status for %s: %v", p.Name, err)
}
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})
for _, p := range profiles {
p.Status = profileStatus(p, api)
}
}

table.AppendBulk(validData)
table.Render()
func profileStatus(p *config.Profile, api libmachine.API) string {
cp, err := config.PrimaryControlPlane(p.Config)
if err != nil {
exit.Error(reason.GuestCpConfig, "error getting primary control plane", err)
}

if invalidProfiles != nil {
out.WarningT("Found {{.number}} invalid profile(s) ! ", out.V{"number": len(invalidProfiles)})
for _, p := range invalidProfiles {
out.ErrT(style.Empty, "\t "+p.Name)
}
out.ErrT(style.Tip, "You can delete them using the following command(s): ")
for _, p := range invalidProfiles {
out.Err(fmt.Sprintf("\t $ minikube delete -p %s \n", p.Name))
}
host, err := machine.LoadHost(api, driver.MachineName(*p.Config, cp))
if err != nil {
glog.Warningf("error loading profiles: %v", err)
return "Unknown"
}

cr, err := machine.CommandRunner(host)
if err != nil {
glog.Warningf("error loading profiles: %v", err)
return "Unknown"
}

hostname, _, port, err := driver.ControlPlaneEndpoint(p.Config, &cp, host.DriverName)
if err != nil {
taciomcosta marked this conversation as resolved.
Show resolved Hide resolved
glog.Warningf("error loading profiles: %v", err)
return "Unknown"
}
}

var printProfilesJSON = func() {
api, err := machine.NewAPIClient()
status, err := kverify.APIServerStatus(cr, hostname, port)
if err != nil {
glog.Errorf("failed to get machine api client %v", err)
glog.Warningf("error getting apiserver status for %s: %v", p.Name, err)
return "Unknown"
}
defer api.Close()
return status.String()
}

validProfiles, invalidProfiles, err := config.ListProfiles()
for _, v := range validProfiles {
cp, err := config.PrimaryControlPlane(v.Config)
func renderProfilesTable(ps [][]string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Profile", "VM Driver", "Runtime", "IP", "Port", "Version", "Status"})
table.SetAutoFormatHeaders(false)
table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
table.SetCenterSeparator("|")
table.AppendBulk(ps)
table.Render()
}

func profilesToTableData(profiles []*config.Profile) [][]string {
var data [][]string
for _, p := range profiles {
cp, err := config.PrimaryControlPlane(p.Config)
if err != nil {
exit.Error(reason.GuestCpConfig, "error getting primary control plane", err)
}
status, err := machine.Status(api, driver.MachineName(*v.Config, cp))
if err != nil {
glog.Warningf("error getting host status for %s: %v", v.Name, err)
}
v.Status = status

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})
}
return data
}

var valid []*config.Profile
var invalid []*config.Profile
func warnInvalidProfiles(invalidProfiles []*config.Profile) {
if invalidProfiles == nil {
return
}

if validProfiles != nil {
valid = validProfiles
} else {
valid = []*config.Profile{}
out.WarningT("Found {{.number}} invalid profile(s) ! ", out.V{"number": len(invalidProfiles)})
for _, p := range invalidProfiles {
out.ErrT(style.Empty, "\t "+p.Name)
}

if invalidProfiles != nil {
invalid = invalidProfiles
} else {
invalid = []*config.Profile{}
out.ErrT(style.Tip, "You can delete them using the following command(s): ")
for _, p := range invalidProfiles {
out.Err(fmt.Sprintf("\t $ minikube delete -p %s \n", p.Name))
}
}

func printProfilesJSON() {
validProfiles, invalidProfiles, err := config.ListProfiles()

body := map[string]interface{}{}
updateProfilesStatus(validProfiles)

var body = map[string]interface{}{}
taciomcosta marked this conversation as resolved.
Show resolved Hide resolved
if err == nil || config.IsNotExist(err) {
body["valid"] = valid
body["invalid"] = invalid
body["valid"] = profilesOrDefault(validProfiles)
body["invalid"] = profilesOrDefault(invalidProfiles)
jsonString, _ := json.Marshal(body)
out.String(string(jsonString))
} else {
Expand All @@ -154,6 +174,13 @@ var printProfilesJSON = func() {
}
}

func profilesOrDefault(profiles []*config.Profile) []*config.Profile {
if profiles != nil {
return profiles
}
return []*config.Profile{}
}

func init() {
profileListCmd.Flags().StringVarP(&output, "output", "o", "table", "The output format. One of 'json', 'table'")
ProfileCmd.AddCommand(profileListCmd)
Expand Down