Merge pull request #5840 from tstromberg/better-existing-start

Health check previously configured driver & exit if not installed
pull/5868/head^2
Thomas Strömberg 2019-11-11 14:35:01 -08:00 committed by GitHub
commit e6b5d52396
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 141 additions and 88 deletions

View File

@ -283,13 +283,14 @@ func runStart(cmd *cobra.Command, args []string) {
registryMirror = viper.GetStringSlice("registry_mirror") registryMirror = viper.GetStringSlice("registry_mirror")
} }
oldConfig, err := cfg.Load() existing, err := cfg.Load()
if err != nil && !os.IsNotExist(err) { if err != nil && !os.IsNotExist(err) {
exit.WithCodeT(exit.Data, "Unable to load config: {{.error}}", out.V{"error": err}) exit.WithCodeT(exit.Data, "Unable to load config: {{.error}}", out.V{"error": err})
} }
driverName := selectDriver(oldConfig) driverName := selectDriver(existing)
glog.Infof("selected: %v", driverName) glog.Infof("selected driver: %s", driverName)
validateDriver(driverName, existing)
err = autoSetDriverOptions(cmd, driverName) err = autoSetDriverOptions(cmd, driverName)
if err != nil { if err != nil {
glog.Errorf("Error autoSetOptions : %v", err) glog.Errorf("Error autoSetOptions : %v", err)
@ -303,7 +304,7 @@ func runStart(cmd *cobra.Command, args []string) {
updateDriver(driverName) updateDriver(driverName)
} }
k8sVersion, isUpgrade := getKubernetesVersion(oldConfig) k8sVersion, isUpgrade := getKubernetesVersion(existing)
config, err := generateCfgFromFlags(cmd, k8sVersion, driverName) config, err := generateCfgFromFlags(cmd, k8sVersion, driverName)
if err != nil { if err != nil {
exit.WithError("Failed to generate config", err) exit.WithError("Failed to generate config", err)
@ -552,16 +553,25 @@ func showKubectlInfo(kcs *kubeconfig.Settings, k8sVersion string) error {
return nil return nil
} }
func selectDriver(oldConfig *cfg.Config) string { // selectDriver returns which driver to choose based on flags, existing configs, and hypervisor detection
func selectDriver(existing *cfg.Config) string {
name := viper.GetString("vm-driver") name := viper.GetString("vm-driver")
glog.Infof("selectDriver: flag=%q, old=%v", name, oldConfig) glog.Infof("selectDriver: flag=%q, old=%v", name, existing)
if name == "" {
// By default, the driver is whatever we used last time
if oldConfig != nil {
return oldConfig.MachineConfig.VMDriver
}
options := driver.Choices() options := driver.Choices()
pick, alts := driver.Choose(options) pick, alts := driver.Choose(name, options)
if name != "" {
out.T(out.Sparkle, `Selecting '{{.driver}}' driver from user configuration (alternates: {{.alternates}})`, out.V{"driver": name, "alternates": alts})
return name
}
// By default, the driver is whatever we used last time
if existing != nil {
pick, alts := driver.Choose(existing.MachineConfig.VMDriver, options)
out.T(out.Sparkle, `Selecting '{{.driver}}' driver from existing profile (alternates: {{.alternates}})`, out.V{"driver": existing.MachineConfig.VMDriver, "alternates": alts})
return pick.Name
}
if len(options) > 1 { if len(options) > 1 {
out.T(out.Sparkle, `Automatically selected the '{{.driver}}' driver (alternates: {{.alternates}})`, out.V{"driver": pick.Name, "alternates": alts}) out.T(out.Sparkle, `Automatically selected the '{{.driver}}' driver (alternates: {{.alternates}})`, out.V{"driver": pick.Name, "alternates": alts})
} else { } else {
@ -571,48 +581,55 @@ func selectDriver(oldConfig *cfg.Config) string {
if pick.Name == "" { if pick.Name == "" {
exit.WithCodeT(exit.Config, "Unable to determine a default driver to use. Try specifying --vm-driver, or see https://minikube.sigs.k8s.io/docs/start/") exit.WithCodeT(exit.Config, "Unable to determine a default driver to use. Try specifying --vm-driver, or see https://minikube.sigs.k8s.io/docs/start/")
} }
return pick.Name
}
name = pick.Name // validateDriver validates that the selected driver appears sane, exits if not
} func validateDriver(name string, existing *cfg.Config) {
glog.Infof("validating driver %q against %+v", name, existing)
if !driver.Supported(name) { if !driver.Supported(name) {
exit.WithCodeT(exit.Failure, "The driver '{{.driver}}' is not supported on {{.os}}", out.V{"driver": name, "os": runtime.GOOS}) exit.WithCodeT(exit.Unavailable, "The driver '{{.driver}}' is not supported on {{.os}}", out.V{"driver": name, "os": runtime.GOOS})
} }
st := driver.Status(name) st := driver.Status(name)
glog.Infof("status for %s: %+v", name, st)
if st.Error != nil { if st.Error != nil {
out.ErrLn("") out.ErrLn("")
out.WarningT("'{{.driver}}' driver reported a possible issue: {{.error}}", out.V{"driver": name, "error": st.Error, "fix": st.Fix})
out.WarningT("'{{.driver}}' driver reported an issue: {{.error}}", out.V{"driver": name, "error": st.Error})
out.ErrT(out.Tip, "Suggestion: {{.fix}}", out.V{"fix": translate.T(st.Fix)}) out.ErrT(out.Tip, "Suggestion: {{.fix}}", out.V{"fix": translate.T(st.Fix)})
if st.Doc != "" { if st.Doc != "" {
out.ErrT(out.Documentation, "Documentation: {{.url}}", out.V{"url": st.Doc}) out.ErrT(out.Documentation, "Documentation: {{.url}}", out.V{"url": st.Doc})
} }
out.ErrLn("") out.ErrLn("")
if !st.Installed && !viper.GetBool(force) {
if existing != nil && name == existing.MachineConfig.VMDriver {
exit.WithCodeT(exit.Unavailable, "{{.driver}} does not appear to be installed, but is specified by an existing profile. Please run 'minikube delete' or install {{.driver}}", out.V{"driver": name})
}
exit.WithCodeT(exit.Unavailable, "{{.driver}} does not appear to be installed", out.V{"driver": name})
}
}
if existing == nil {
return
} }
// Detect if our driver conflicts with a previously created VM. If we run into any errors, just move on.
api, err := machine.NewAPIClient() api, err := machine.NewAPIClient()
if err != nil { if err != nil {
glog.Infof("selectDriver NewAPIClient: %v", err) glog.Warningf("selectDriver NewAPIClient: %v", err)
return name return
}
exists, err := api.Exists(cfg.GetMachineName())
if err != nil {
glog.Infof("selectDriver api.Exists: %v", err)
return name
}
if !exists {
return name
} }
h, err := api.Load(cfg.GetMachineName()) h, err := api.Load(cfg.GetMachineName())
if err != nil { if err != nil {
glog.Infof("selectDriver api.Load: %v", err) glog.Warningf("selectDriver api.Load: %v", err)
return name return
} }
if h.Driver.DriverName() == name || h.Driver.DriverName() == "not-found" { if h.Driver.DriverName() == name {
return name return
} }
out.ErrT(out.Conflict, `The existing "{{.profile_name}}" cluster was created using the "{{.old_driver}}" driver, and cannot be started using the "{{.driver}}" driver.`, out.ErrT(out.Conflict, `The existing "{{.profile_name}}" cluster was created using the "{{.old_driver}}" driver, and cannot be started using the "{{.driver}}" driver.`,
@ -628,7 +645,6 @@ func selectDriver(oldConfig *cfg.Config) string {
`, out.V{"command": minikubeCmd(), "old_driver": h.Driver.DriverName(), "profile_name": cfg.GetMachineName()}) `, out.V{"command": minikubeCmd(), "old_driver": h.Driver.DriverName(), "profile_name": cfg.GetMachineName()})
exit.WithCodeT(exit.Config, "Exiting.") exit.WithCodeT(exit.Config, "Exiting.")
return ""
} }
func selectImageRepository(mirrorCountry string, k8sVersion string) (bool, string, error) { func selectImageRepository(mirrorCountry string, k8sVersion string) (bool, string, error) {

3
go.mod
View File

@ -21,14 +21,11 @@ require (
github.com/docker/machine v0.7.1-0.20190718054102-a555e4f7a8f5 // version is 0.7.1 to pin to a555e4f7a8f5 github.com/docker/machine v0.7.1-0.20190718054102-a555e4f7a8f5 // version is 0.7.1 to pin to a555e4f7a8f5
github.com/elazarl/goproxy v0.0.0-20190421051319-9d40249d3c2f github.com/elazarl/goproxy v0.0.0-20190421051319-9d40249d3c2f
github.com/elazarl/goproxy/ext v0.0.0-20190421051319-9d40249d3c2f // indirect github.com/elazarl/goproxy/ext v0.0.0-20190421051319-9d40249d3c2f // indirect
github.com/ghodss/yaml v1.0.0 // indirect
github.com/go-ole/go-ole v1.2.4 // indirect github.com/go-ole/go-ole v1.2.4 // indirect
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3 github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/google/btree v1.0.0 // indirect
github.com/google/go-cmp v0.3.0 github.com/google/go-cmp v0.3.0
github.com/gorilla/mux v1.7.1 // indirect github.com/gorilla/mux v1.7.1 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.5.0 // indirect
github.com/hashicorp/errwrap v0.0.0-20141028054710-7554cd9344ce // indirect github.com/hashicorp/errwrap v0.0.0-20141028054710-7554cd9344ce // indirect
github.com/hashicorp/go-getter v1.4.0 github.com/hashicorp/go-getter v1.4.0
github.com/hashicorp/go-multierror v0.0.0-20160811015721-8c5f0ad93604 // indirect github.com/hashicorp/go-multierror v0.0.0-20160811015721-8c5f0ad93604 // indirect

View File

@ -95,14 +95,7 @@ func FlagDefaults(name string) FlagHints {
// Choices returns a list of drivers which are possible on this system // Choices returns a list of drivers which are possible on this system
func Choices() []registry.DriverState { func Choices() []registry.DriverState {
options := []registry.DriverState{} options := registry.Available()
for _, ds := range registry.Installed() {
if !ds.State.Healthy {
glog.Warningf("%q is installed, but unhealthy: %v", ds.Name, ds.State.Error)
continue
}
options = append(options, ds)
}
// Descending priority for predictability and appearance // Descending priority for predictability and appearance
sort.Slice(options, func(i, j int) bool { sort.Slice(options, func(i, j int) bool {
@ -112,9 +105,25 @@ func Choices() []registry.DriverState {
} }
// Choose returns a suggested driver from a set of options // Choose returns a suggested driver from a set of options
func Choose(options []registry.DriverState) (registry.DriverState, []registry.DriverState) { func Choose(requested string, options []registry.DriverState) (registry.DriverState, []registry.DriverState) {
glog.Infof("requested: %q", requested)
pick := registry.DriverState{} pick := registry.DriverState{}
for _, ds := range options { for _, ds := range options {
if ds.Name == requested {
glog.Infof("choosing %q because it was requested", ds.Name)
pick = ds
continue
}
if !ds.State.Installed {
continue
}
if !ds.State.Healthy {
glog.Infof("not recommending %q due to health: %v", ds.Name, ds.State.Error)
continue
}
if ds.Priority <= registry.Discouraged { if ds.Priority <= registry.Discouraged {
glog.Infof("not recommending %q due to priority: %d", ds.Name, ds.Priority) glog.Infof("not recommending %q due to priority: %d", ds.Name, ds.Priority)
continue continue
@ -128,6 +137,9 @@ func Choose(options []registry.DriverState) (registry.DriverState, []registry.Dr
alternates := []registry.DriverState{} alternates := []registry.DriverState{}
for _, ds := range options { for _, ds := range options {
if ds != pick { if ds != pick {
if !ds.State.Healthy || !ds.State.Installed {
continue
}
alternates = append(alternates, ds) alternates = append(alternates, ds)
} }
} }

View File

@ -87,6 +87,7 @@ func TestChoices(t *testing.T) {
choices []string choices []string
pick string pick string
alts []string alts []string
requested string
}{ }{
{ {
def: registry.DriverDef{ def: registry.DriverDef{
@ -94,7 +95,7 @@ func TestChoices(t *testing.T) {
Priority: registry.Default, Priority: registry.Default,
Status: func() registry.State { return registry.State{Installed: true, Healthy: false} }, Status: func() registry.State { return registry.State{Installed: true, Healthy: false} },
}, },
choices: []string{}, choices: []string{"unhealthy"},
pick: "", pick: "",
alts: []string{}, alts: []string{},
}, },
@ -104,7 +105,7 @@ func TestChoices(t *testing.T) {
Priority: registry.Discouraged, Priority: registry.Discouraged,
Status: func() registry.State { return registry.State{Installed: true, Healthy: true} }, Status: func() registry.State { return registry.State{Installed: true, Healthy: true} },
}, },
choices: []string{"discouraged"}, choices: []string{"discouraged", "unhealthy"},
pick: "", pick: "",
alts: []string{"discouraged"}, alts: []string{"discouraged"},
}, },
@ -114,7 +115,7 @@ func TestChoices(t *testing.T) {
Priority: registry.Default, Priority: registry.Default,
Status: func() registry.State { return registry.State{Installed: true, Healthy: true} }, Status: func() registry.State { return registry.State{Installed: true, Healthy: true} },
}, },
choices: []string{"default", "discouraged"}, choices: []string{"default", "discouraged", "unhealthy"},
pick: "default", pick: "default",
alts: []string{"discouraged"}, alts: []string{"discouraged"},
}, },
@ -124,16 +125,24 @@ func TestChoices(t *testing.T) {
Priority: registry.Preferred, Priority: registry.Preferred,
Status: func() registry.State { return registry.State{Installed: true, Healthy: true} }, Status: func() registry.State { return registry.State{Installed: true, Healthy: true} },
}, },
choices: []string{"preferred", "default", "discouraged"}, choices: []string{"preferred", "default", "discouraged", "unhealthy"},
pick: "preferred", pick: "preferred",
alts: []string{"default", "discouraged"}, alts: []string{"default", "discouraged"},
}, },
{
requested: "unhealthy",
choices: []string{"preferred", "default", "discouraged", "unhealthy"},
pick: "unhealthy",
alts: []string{"preferred", "default", "discouraged"},
},
} }
for _, tc := range tests { for _, tc := range tests {
t.Run(tc.def.Name, func(t *testing.T) { t.Run(tc.def.Name, func(t *testing.T) {
if tc.def.Name != "" {
if err := registry.Register(tc.def); err != nil { if err := registry.Register(tc.def); err != nil {
t.Errorf("register returned error: %v", err) t.Errorf("register returned error: %v", err)
} }
}
got := Choices() got := Choices()
gotNames := []string{} gotNames := []string{}
@ -145,7 +154,7 @@ func TestChoices(t *testing.T) {
t.Errorf("choices mismatch (-want +got):\n%s", diff) t.Errorf("choices mismatch (-want +got):\n%s", diff)
} }
pick, alts := Choose(got) pick, alts := Choose(tc.requested, got)
if pick.Name != tc.pick { if pick.Name != tc.pick {
t.Errorf("pick = %q, expected %q", pick.Name, tc.pick) t.Errorf("pick = %q, expected %q", pick.Name, tc.pick)
} }

View File

@ -18,6 +18,7 @@ package registry
import ( import (
"os" "os"
"sort"
"github.com/golang/glog" "github.com/golang/glog"
) )
@ -53,8 +54,8 @@ func Driver(name string) DriverDef {
return globalRegistry.Driver(name) return globalRegistry.Driver(name)
} }
// Installed returns a list of installed drivers in the global registry // Available returns a list of available drivers in the global registry
func Installed() []DriverState { func Available() []DriverState {
sts := []DriverState{} sts := []DriverState{}
glog.Infof("Querying for installed drivers using PATH=%s", os.Getenv("PATH")) glog.Infof("Querying for installed drivers using PATH=%s", os.Getenv("PATH"))
@ -66,12 +67,18 @@ func Installed() []DriverState {
s := d.Status() s := d.Status()
glog.Infof("%s priority: %d, state: %+v", d.Name, d.Priority, s) glog.Infof("%s priority: %d, state: %+v", d.Name, d.Priority, s)
if !s.Installed { priority := d.Priority
glog.Infof("%q not installed: %v", d.Name, s.Error) if !s.Healthy {
continue priority = Unhealthy
} }
sts = append(sts, DriverState{Name: d.Name, Priority: d.Priority, State: s})
sts = append(sts, DriverState{Name: d.Name, Priority: priority, State: s})
} }
// Descending priority for predictability
sort.Slice(sts, func(i, j int) bool {
return sts[i].Priority > sts[j].Priority
})
return sts return sts
} }

View File

@ -64,7 +64,7 @@ func TestGlobalList(t *testing.T) {
} }
} }
func TestGlobalInstalled(t *testing.T) { func TestGlobalAvailable(t *testing.T) {
globalRegistry = newRegistry() globalRegistry = newRegistry()
if err := Register(DriverDef{Name: "foo"}); err != nil { if err := Register(DriverDef{Name: "foo"}); err != nil {
@ -72,26 +72,38 @@ func TestGlobalInstalled(t *testing.T) {
} }
bar := DriverDef{ bar := DriverDef{
Name: "bar", Name: "healthy-bar",
Priority: Default, Priority: Default,
Status: func() State { return State{Installed: true} }, Status: func() State { return State{Healthy: true} },
} }
if err := Register(bar); err != nil { if err := Register(bar); err != nil {
t.Errorf("register returned error: %v", err) t.Errorf("register returned error: %v", err)
} }
foo := DriverDef{
Name: "unhealthy-foo",
Priority: Default,
Status: func() State { return State{Healthy: false} },
}
if err := Register(foo); err != nil {
t.Errorf("register returned error: %v", err)
}
expected := []DriverState{ expected := []DriverState{
{ {
Name: "bar", Name: "healthy-bar",
Priority: Default, Priority: Default,
State: State{ State: State{Healthy: true},
Installed: true,
}, },
{
Name: "unhealthy-foo",
Priority: Unhealthy,
State: State{Healthy: false},
}, },
} }
if diff := cmp.Diff(Installed(), expected); diff != "" { if diff := cmp.Diff(Available(), expected); diff != "" {
t.Errorf("installed mismatch (-want +got):\n%s", diff) t.Errorf("available mismatch (-want +got):\n%s", diff)
} }
} }

View File

@ -29,20 +29,20 @@ import (
type Priority int type Priority int
const ( const (
// Unknown priority // Unknown is when there is no status check available
Unknown Priority = iota Unknown Priority = iota
// Discouraged priority // Unhealthy is when a driver does not pass health checks
Unhealthy
// Discouraged is when a driver has caveats that preclude it's recommendation
Discouraged Discouraged
// Deprecated priority // Deprecated is when a driver has been formally deprecated
Deprecated Deprecated
// Fallback priority // Fallback is when a driver works well, but may not be high performance
Fallback Fallback
// Default priority // Default is what what most 3rd party drivers are
Default Default
// Preferred priority // Preferred is for drivers that use a native hypervisor interface
Preferred Preferred
// StronglyPreferred priority
StronglyPreferred
) )
// Registry contains all the supported driver definitions on the host // Registry contains all the supported driver definitions on the host