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

Consolidate driver handling code in pkg/minikube/driver #5658

Merged
merged 10 commits into from
Oct 21, 2019
6 changes: 3 additions & 3 deletions cmd/minikube/cmd/config/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import (

"k8s.io/minikube/pkg/minikube/assets"
pkgConfig "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/driver"
)

var minikubeConfig = pkgConfig.MinikubeConfig{
"vm-driver": constants.DriverKvm2,
"vm-driver": driver.KVM2,
"cpus": 12,
"show-libmachine-logs": true,
}
Expand All @@ -49,7 +49,7 @@ func TestFindSetting(t *testing.T) {
}

func TestSetString(t *testing.T) {
err := SetString(minikubeConfig, "vm-driver", constants.DriverVirtualbox)
err := SetString(minikubeConfig, "vm-driver", driver.VirtualBox)
if err != nil {
t.Fatalf("Couldn't set string: %v", err)
}
Expand Down
12 changes: 5 additions & 7 deletions cmd/minikube/cmd/config/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
"github.com/pkg/errors"
"k8s.io/minikube/pkg/minikube/assets"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/cruntime"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/out"
)

Expand All @@ -44,13 +44,11 @@ and then start minikube again with the following flags:
minikube start --container-runtime=containerd --docker-opt containerd=/var/run/containerd/containerd.sock`

// IsValidDriver checks if a driver is supported
func IsValidDriver(string, driver string) error {
for _, d := range constants.SupportedVMDrivers {
if driver == d {
return nil
}
func IsValidDriver(string, name string) error {
if driver.Supported(name) {
return nil
}
return fmt.Errorf("driver %q is not supported", driver)
return fmt.Errorf("driver %q is not supported", name)
}

// RequiresRestartMsg returns the "requires restart" message
Expand Down
4 changes: 2 additions & 2 deletions cmd/minikube/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"k8s.io/minikube/pkg/minikube/cluster"
pkg_config "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/kubeconfig"
"k8s.io/minikube/pkg/minikube/localpath"
Expand Down Expand Up @@ -190,8 +191,7 @@ func deleteProfile(profile *pkg_config.Profile) error {
return DeletionError{Err: delErr, Errtype: MissingProfile}
}

// In the case of "none", we want to uninstall Kubernetes as there is no VM to delete
if err == nil && cc.MachineConfig.VMDriver == constants.DriverNone {
if err == nil && driver.BareMetal(cc.MachineConfig.VMDriver) {
if err := uninstallKubernetes(api, cc.KubernetesConfig, viper.GetString(cmdcfg.Bootstrapper)); err != nil {
deletionError, ok := err.(DeletionError)
if ok {
Expand Down
4 changes: 2 additions & 2 deletions cmd/minikube/cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
"github.com/spf13/cobra"
"k8s.io/minikube/pkg/minikube/cluster"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/machine"
)
Expand Down Expand Up @@ -343,7 +343,7 @@ var dockerEnvCmd = &cobra.Command{
if err != nil {
exit.WithError("Error getting host", err)
}
if host.Driver.DriverName() == constants.DriverNone {
if host.Driver.DriverName() == driver.None {
exit.UsageT(`'none' driver does not support 'minikube docker-env' command`)
}
hostSt, err := cluster.GetHostStatus(api)
Expand Down
4 changes: 2 additions & 2 deletions cmd/minikube/cmd/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/spf13/cobra"
"k8s.io/minikube/pkg/minikube/cluster"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/pkg/minikube/out"
Expand Down Expand Up @@ -107,7 +107,7 @@ var mountCmd = &cobra.Command{
if err != nil {
exit.WithError("Error loading api", err)
}
if host.Driver.DriverName() == constants.DriverNone {
if host.Driver.DriverName() == driver.None {
exit.UsageT(`'none' driver does not support 'minikube mount' command`)
}
var ip net.IP
Expand Down
4 changes: 2 additions & 2 deletions cmd/minikube/cmd/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

"k8s.io/minikube/pkg/minikube/cluster"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/pkg/minikube/out"
Expand All @@ -46,7 +46,7 @@ var sshCmd = &cobra.Command{
if err != nil {
exit.WithError("Error getting host", err)
}
if host.Driver.DriverName() == constants.DriverNone {
if host.Driver.DriverName() == driver.None {
exit.UsageT("'none' driver does not support 'minikube ssh' command")
}
if viper.GetBool(nativeSSH) {
Expand Down
93 changes: 46 additions & 47 deletions cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import (
"github.com/spf13/viper"
"golang.org/x/sync/errgroup"
cmdcfg "k8s.io/minikube/cmd/minikube/cmd/config"
"k8s.io/minikube/pkg/drivers"
"k8s.io/minikube/pkg/minikube/bootstrapper"
"k8s.io/minikube/pkg/minikube/bootstrapper/images"
"k8s.io/minikube/pkg/minikube/bootstrapper/kubeadm"
Expand All @@ -54,7 +53,7 @@ import (
cfg "k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/constants"
"k8s.io/minikube/pkg/minikube/cruntime"
"k8s.io/minikube/pkg/minikube/drivers/none"
"k8s.io/minikube/pkg/minikube/driver"
"k8s.io/minikube/pkg/minikube/exit"
"k8s.io/minikube/pkg/minikube/kubeconfig"
"k8s.io/minikube/pkg/minikube/localpath"
Expand Down Expand Up @@ -206,7 +205,7 @@ func initKubernetesFlags() {

// initDriverFlags inits the commandline flags for vm drivers
func initDriverFlags() {
startCmd.Flags().String("vm-driver", "", fmt.Sprintf("Driver is one of: %v (defaults to virtualbox)", constants.SupportedVMDrivers))
startCmd.Flags().String("vm-driver", "", fmt.Sprintf("Driver is one of: %v (defaults to %s)", driver.SupportedDrivers(), driver.Default()))
startCmd.Flags().Bool(disableDriverMounts, false, "Disables the filesystem mounts provided by the hypervisors")

// kvm2
Expand Down Expand Up @@ -300,30 +299,29 @@ func runStart(cmd *cobra.Command, args []string) {
exit.WithCodeT(exit.Data, "Unable to load config: {{.error}}", out.V{"error": err})
}

driver := selectDriver(oldConfig)
err = autoSetDriverOptions(driver)
driverName := selectDriver(oldConfig)
err = autoSetDriverOptions(cmd, driverName)
if err != nil {
glog.Errorf("Error autoSetOptions : %v", err)
}

validateFlags(driver)
validateUser(driver)
validateFlags(driverName)
validateUser(driverName)

v, err := version.GetSemverVersion()
if err != nil {
out.WarningT("Error parsing minikube version: {{.error}}", out.V{"error": err})
} else if err := drivers.InstallOrUpdate(driver, localpath.MakeMiniPath("bin"), v, viper.GetBool(interactive), viper.GetBool(autoUpdate)); err != nil {
out.WarningT("Unable to update {{.driver}} driver: {{.error}}", out.V{"driver": driver, "error": err})
} else if err := driver.InstallOrUpdate(driverName, localpath.MakeMiniPath("bin"), v, viper.GetBool(interactive), viper.GetBool(autoUpdate)); err != nil {
out.WarningT("Unable to update {{.driver}} driver: {{.error}}", out.V{"driver": driverName, "error": err})
}

k8sVersion, isUpgrade := getKubernetesVersion(oldConfig)
config, err := generateCfgFromFlags(cmd, k8sVersion, driver)
config, err := generateCfgFromFlags(cmd, k8sVersion, driverName)
if err != nil {
exit.WithError("Failed to generate config", err)
}

// For non-"none", the ISO is required to boot, so block until it is downloaded
if driver != constants.DriverNone {
if !driver.BareMetal(driverName) {
if err := cluster.CacheISO(config.MachineConfig); err != nil {
exit.WithError("Failed to cache ISO", err)
}
Expand All @@ -350,7 +348,7 @@ func runStart(cmd *cobra.Command, args []string) {
mRunner, preExists, machineAPI, host := startMachine(&config)
defer machineAPI.Close()
// configure the runtime (docker, containerd, crio)
cr := configureRuntimes(mRunner, driver, config.KubernetesConfig)
cr := configureRuntimes(mRunner, driverName, config.KubernetesConfig)
showVersionInfo(k8sVersion, cr)
waitCacheImages(&cacheGroup)

Expand Down Expand Up @@ -379,8 +377,8 @@ func runStart(cmd *cobra.Command, args []string) {
out.T(out.FailureType, "Unable to load cached images from config file.")
}

// special ops for none driver, like change minikube directory.
if driver == constants.DriverNone {
// special ops for none , like change minikube directory.
if driverName == driver.None {
prepareNone()
}
if viper.GetBool(waitUntilHealthy) {
Expand Down Expand Up @@ -546,46 +544,46 @@ func showKubectlConnectInfo(kcs *kubeconfig.Settings) {
}

func selectDriver(oldConfig *cfg.Config) string {
driver := viper.GetString("vm-driver")
name := viper.GetString("vm-driver")
// By default, the driver is whatever we used last time
if driver == "" {
driver = constants.DriverVirtualbox
if name == "" {
name = driver.Default()
if oldConfig != nil {
driver = oldConfig.MachineConfig.VMDriver
return oldConfig.MachineConfig.VMDriver
}
}
if err := cmdcfg.IsValidDriver(runtime.GOOS, driver); err != nil {
exit.WithCodeT(exit.Failure, "The driver '{{.driver}}' is not supported on {{.os}}", out.V{"driver": driver, "os": runtime.GOOS})
if !driver.Supported(name) {
exit.WithCodeT(exit.Failure, "The driver '{{.driver}}' is not supported on {{.os}}", out.V{"driver": name, "os": runtime.GOOS})
}

// Detect if our driver conflicts with a previously created VM. If we run into any errors, just move on.
api, err := machine.NewAPIClient()
if err != nil {
glog.Infof("selectDriver NewAPIClient: %v", err)
return driver
return name
}

exists, err := api.Exists(cfg.GetMachineName())
if err != nil {
glog.Infof("selectDriver api.Exists: %v", err)
return driver
return name
}
if !exists {
return driver
return name
}

h, err := api.Load(cfg.GetMachineName())
if err != nil {
glog.Infof("selectDriver api.Load: %v", err)
return driver
return name
}

if h.Driver.DriverName() == driver || h.Driver.DriverName() == "not-found" {
return driver
if h.Driver.DriverName() == name || h.Driver.DriverName() == "not-found" {
return name
}

out.ErrT(out.Conflict, `The existing "{{.profile_name}}" VM that was created using the "{{.old_driver}}" driver, and is incompatible with the "{{.driver}}" driver.`,
out.V{"profile_name": cfg.GetMachineName(), "driver": driver, "old_driver": h.Driver.DriverName()})
out.V{"profile_name": cfg.GetMachineName(), "driver": name, "old_driver": h.Driver.DriverName()})

out.ErrT(out.Workaround, `To proceed, either:
1) Delete the existing VM using: '{{.command}} delete'
Expand Down Expand Up @@ -655,7 +653,7 @@ func minikubeCmd() string {
}

// validerUser validates minikube is run by the recommended user (privileged or regular)
func validateUser(driver string) {
func validateUser(drvName string) {
u, err := user.Current()
if err != nil {
glog.Errorf("Error getting the current user: %v", err)
Expand All @@ -664,15 +662,15 @@ func validateUser(driver string) {

useForce := viper.GetBool(force)

if driver == constants.DriverNone && u.Uid != "0" && !useForce {
exit.WithCodeT(exit.Permissions, `The "{{.driver_name}}" driver requires root privileges. Please run minikube using 'sudo minikube --vm-driver={{.driver_name}}'.`, out.V{"driver_name": driver})
if driver.BareMetal(drvName) && u.Uid != "0" && !useForce {
exit.WithCodeT(exit.Permissions, `The "{{.driver_name}}" driver requires root privileges. Please run minikube using 'sudo minikube --vm-driver={{.driver_name}}'.`, out.V{"driver_name": drvName})
}

if driver == constants.DriverNone || u.Uid != "0" {
if driver.BareMetal(drvName) || u.Uid != "0" {
return
}

out.T(out.Stopped, `The "{{.driver_name}}" driver should not be used with root privileges.`, out.V{"driver_name": driver})
out.T(out.Stopped, `The "{{.driver_name}}" driver should not be used with root privileges.`, out.V{"driver_name": drvName})
out.T(out.Tip, "If you are running minikube within a VM, consider using --vm-driver=none:")
out.T(out.Documentation, " https://minikube.sigs.k8s.io/docs/reference/drivers/none/")

Expand All @@ -689,7 +687,7 @@ func validateUser(driver string) {
}

// validateFlags validates the supplied flags against known bad combinations
func validateFlags(driver string) {
func validateFlags(drvName string) {
diskSizeMB := pkgutil.CalculateSizeInMB(viper.GetString(humanReadableDiskSize))
if diskSizeMB < pkgutil.CalculateSizeInMB(minimumDiskSize) && !viper.GetBool(force) {
exit.WithCodeT(exit.Config, "Requested disk size {{.requested_size}} is less than minimum of {{.minimum_size}}", out.V{"requested_size": diskSizeMB, "minimum_size": pkgutil.CalculateSizeInMB(minimumDiskSize)})
Expand All @@ -705,7 +703,7 @@ func validateFlags(driver string) {
}

var cpuCount int
if driver == constants.DriverNone {
if driver.BareMetal(drvName) {
if cfg.GetMachineName() != constants.DefaultMachineName {
exit.WithCodeT(exit.Config, "The 'none' driver does not support multiple profiles: https://minikube.sigs.k8s.io/docs/reference/drivers/none/")
}
Expand Down Expand Up @@ -780,7 +778,7 @@ func waitCacheImages(g *errgroup.Group) {
}

// generateCfgFromFlags generates cfg.Config based on flags and supplied arguments
func generateCfgFromFlags(cmd *cobra.Command, k8sVersion string, driver string) (cfg.Config, error) {
func generateCfgFromFlags(cmd *cobra.Command, k8sVersion string, drvName string) (cfg.Config, error) {
r, err := cruntime.New(cruntime.Config{Type: viper.GetString(containerRuntime)})
if err != nil {
return cfg.Config{}, err
Expand Down Expand Up @@ -832,7 +830,7 @@ func generateCfgFromFlags(cmd *cobra.Command, k8sVersion string, driver string)
Memory: pkgutil.CalculateSizeInMB(viper.GetString(memory)),
CPUs: viper.GetInt(cpus),
DiskSize: pkgutil.CalculateSizeInMB(viper.GetString(humanReadableDiskSize)),
VMDriver: driver,
VMDriver: drvName,
ContainerRuntime: viper.GetString(containerRuntime),
HyperkitVpnKitSock: viper.GetString(vpnkitSock),
HyperkitVSockPorts: viper.GetStringSlice(vsockPorts),
Expand Down Expand Up @@ -896,12 +894,14 @@ func setDockerProxy() {
}

// autoSetDriverOptions sets the options needed for specific vm-driver automatically.
func autoSetDriverOptions(driver string) error {
if driver == constants.DriverNone {
if o := none.AutoOptions(); o != "" {
return extraOptions.Set(o)
}
viper.Set(cacheImages, false)
func autoSetDriverOptions(cmd *cobra.Command, drvName string) error {
hints := driver.FlagDefaults(drvName)
if !cmd.Flags().Changed("extra-config") && hints.ExtraOptions != "" {
return extraOptions.Set(hints.ExtraOptions)
}

if !cmd.Flags().Changed(cacheImages) {
viper.Set(cacheImages, hints.CacheImages)
}
return nil
}
Expand Down Expand Up @@ -985,8 +985,7 @@ func validateNetwork(h *host.Host, r command.Runner) string {
}
}

// none driver does not need ssh access
if h.Driver.DriverName() != constants.DriverNone {
if driver.BareMetal(h.Driver.DriverName()) {
sshAddr := fmt.Sprintf("%s:22", ip)
conn, err := net.Dial("tcp", sshAddr)
if err != nil {
Expand Down Expand Up @@ -1110,15 +1109,15 @@ func setupKubeAdm(mAPI libmachine.API, kc cfg.KubernetesConfig) bootstrapper.Boo
}

// configureRuntimes does what needs to happen to get a runtime going.
func configureRuntimes(runner cruntime.CommandRunner, driver string, k8s cfg.KubernetesConfig) cruntime.Manager {
func configureRuntimes(runner cruntime.CommandRunner, drvName string, k8s cfg.KubernetesConfig) cruntime.Manager {
config := cruntime.Config{Type: viper.GetString(containerRuntime), Runner: runner, ImageRepository: k8s.ImageRepository, KubernetesVersion: k8s.KubernetesVersion}
cr, err := cruntime.New(config)
if err != nil {
exit.WithError("Failed runtime", err)
}

disableOthers := true
if driver == constants.DriverNone {
if driver.BareMetal(drvName) {
disableOthers = false
}
err = cr.Enable(disableOthers)
Expand Down
3 changes: 3 additions & 0 deletions cmd/minikube/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import (
// Fix for https://github.com/kubernetes/minikube/issues/4866
_ "k8s.io/minikube/pkg/initflag"

// Register drivers
_ "k8s.io/minikube/pkg/minikube/registry/drvs"

mlog "github.com/docker/machine/libmachine/log"

"github.com/golang/glog"
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ github.com/opencontainers/selinux v0.0.0-20170621221121-4a2974bf1ee9/go.mod h1:+
github.com/openzipkin/zipkin-go v0.1.1/go.mod h1:NtoC/o8u3JlF1lSlyPNswIbeQH9bJTmOf0Erfk+hxe8=
github.com/otiai10/copy v1.0.2 h1:DDNipYy6RkIkjMwy+AWzgKiNTyj2RUI9yEMeETEpVyc=
github.com/otiai10/copy v1.0.2/go.mod h1:c7RpqBkwMom4bYTSkLSym4VSJz/XtncWRAj/J4PEIMY=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95 h1:+OLn68pqasWca0z5ryit9KGfp3sUsW4Lqg32iRMJyzs=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
github.com/otiai10/mint v1.3.0 h1:Ady6MKVezQwHBkGzLFbrsywyp09Ah7rkmfjV3Bcr5uc=
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
Expand Down
Loading