From ff969e693b3ea143955bbdbfecf3a5d820f93200 Mon Sep 17 00:00:00 2001 From: Medya Gh Date: Wed, 10 Jul 2019 23:14:15 -0700 Subject: [PATCH] Simplifying start cmd --- cmd/minikube/cmd/cache.go | 4 +- cmd/minikube/cmd/delete.go | 2 +- cmd/minikube/cmd/logs.go | 2 +- cmd/minikube/cmd/root.go | 4 +- cmd/minikube/cmd/start.go | 123 +++++++++++++++++++++---------------- cmd/minikube/cmd/status.go | 2 +- 6 files changed, 76 insertions(+), 61 deletions(-) diff --git a/cmd/minikube/cmd/cache.go b/cmd/minikube/cmd/cache.go index 7b403f4b90..6a1125bef8 100644 --- a/cmd/minikube/cmd/cache.go +++ b/cmd/minikube/cmd/cache.go @@ -93,8 +93,8 @@ func CacheImagesInConfigFile() error { return machine.CacheImages(images, constants.ImageCacheDir) } -// LoadCachedImagesInConfigFile loads the images currently in the config file (minikube start) -func LoadCachedImagesInConfigFile() error { +// loadCachedImagesInConfigFile loads the images currently in the config file (minikube start) +func loadCachedImagesInConfigFile() error { images, err := imagesInConfigFile() if err != nil { return err diff --git a/cmd/minikube/cmd/delete.go b/cmd/minikube/cmd/delete.go index 8b4f396726..d8250bb66c 100644 --- a/cmd/minikube/cmd/delete.go +++ b/cmd/minikube/cmd/delete.go @@ -96,7 +96,7 @@ func runDelete(cmd *cobra.Command, args []string) { func uninstallKubernetes(api libmachine.API, kc pkg_config.KubernetesConfig, bsName string) { console.OutStyle(console.Resetting, "Uninstalling Kubernetes %s using %s ...", kc.KubernetesVersion, bsName) - clusterBootstrapper, err := GetClusterBootstrapper(api, bsName) + clusterBootstrapper, err := getClusterBootstrapper(api, bsName) if err != nil { console.ErrLn("Unable to get bootstrapper: %v", err) } else if err = clusterBootstrapper.DeleteCluster(kc); err != nil { diff --git a/cmd/minikube/cmd/logs.go b/cmd/minikube/cmd/logs.go index 2df75e4e6a..575762aeba 100644 --- a/cmd/minikube/cmd/logs.go +++ b/cmd/minikube/cmd/logs.go @@ -66,7 +66,7 @@ var logsCmd = &cobra.Command{ if err != nil { exit.WithError("command runner", err) } - bs, err := GetClusterBootstrapper(api, viper.GetString(cmdcfg.Bootstrapper)) + bs, err := getClusterBootstrapper(api, viper.GetString(cmdcfg.Bootstrapper)) if err != nil { exit.WithError("Error getting cluster bootstrapper", err) } diff --git a/cmd/minikube/cmd/root.go b/cmd/minikube/cmd/root.go index 4c5566e8ee..40907bbaee 100644 --- a/cmd/minikube/cmd/root.go +++ b/cmd/minikube/cmd/root.go @@ -170,8 +170,8 @@ func setupViper() { setFlagsUsingViper() } -// GetClusterBootstrapper returns a new bootstrapper for the cluster -func GetClusterBootstrapper(api libmachine.API, bootstrapperName string) (bootstrapper.Bootstrapper, error) { +// getClusterBootstrapper returns a new bootstrapper for the cluster +func getClusterBootstrapper(api libmachine.API, bootstrapperName string) (bootstrapper.Bootstrapper, error) { var b bootstrapper.Bootstrapper var err error switch bootstrapperName { diff --git a/cmd/minikube/cmd/start.go b/cmd/minikube/cmd/start.go index aa33d5a0b4..ce3cce70d8 100644 --- a/cmd/minikube/cmd/start.go +++ b/cmd/minikube/cmd/start.go @@ -183,14 +183,9 @@ assumes you have already installed one of the VM drivers: virtualbox/parallels/v func runStart(cmd *cobra.Command, args []string) { console.OutT(console.Happy, "minikube {{.version}} on {{.os}} ({{.arch}})", console.Arg{"version": version.GetVersion(), "os": runtime.GOOS, "arch": runtime.GOARCH}) validateConfig() - validateUser() - oldConfig, err := cfg.Load() - if err != nil && !os.IsNotExist(err) { - exit.WithCode(exit.Data, "Unable to load config: %v", err) - } - k8sVersion, isUpgrade := validateKubernetesVersions(oldConfig) + k8sVersion, isUpgrade := getKubernetesVersion() config, err := generateConfig(cmd, k8sVersion) if err != nil { exit.WithError("Failed to generate config", err) @@ -200,7 +195,7 @@ func runStart(cmd *cobra.Command, args []string) { downloadISO(config) // With "none", images are persistently stored in Docker, so internal caching isn't necessary. - skipCache(config) + skipCache(&config) // Now that the ISO is downloaded, pull images in the background while the VM boots. var cacheGroup errgroup.Group @@ -213,30 +208,60 @@ func runStart(cmd *cobra.Command, args []string) { } validateDriverVersion(viper.GetString(vmDriver)) + // exits here in case of --download-only option. + handleDownloadOnly(&cacheGroup, k8sVersion) + mRunner, preExists, machineAPI, host := startMachine(config) + defer machineAPI.Close() + cr := configureRuntimes(mRunner) + showVersionInfo(k8sVersion, cr) + waitCacheImages(&cacheGroup) + // setup kube adm and certs and return bootstrapperx + bs := setupKubeAdm(machineAPI, config.KubernetesConfig) + // The kube config must be update must come before bootstrapping, otherwise health checks may use a stale IP + kubeconfig := updateKubeConfig(host, &config) + // pull images or restart cluster + bootstrapCluster(bs, cr, mRunner, config.KubernetesConfig, preExists, isUpgrade) + configureMounts() + if err = loadCachedImagesInConfigFile(); err != nil { + console.Failure("Unable to load cached images from config file.") + } + // special ops for none driver, like change minikube directory. + prepareNone(viper.GetString(vmDriver)) + + if err := bs.WaitCluster(config.KubernetesConfig); err != nil { + exit.WithError("Wait failed", err) + } + showKubectlConnectInfo(kubeconfig) + +} + +func handleDownloadOnly(cacheGroup *errgroup.Group, k8sVersion string) { + // If --download-only, complete the remaining downloads and exit. + if !viper.GetBool(downloadOnly) { + return + } + if err := doCacheBinaries(k8sVersion); err != nil { + exit.WithError("Failed to cache binaries", err) + } + waitCacheImages(cacheGroup) + if err := CacheImagesInConfigFile(); err != nil { + exit.WithError("Failed to cache images", err) + } + console.OutStyle(console.Check, "Download complete!") + os.Exit(0) + +} + +func startMachine(config cfg.Config) (runner command.Runner, preExists bool, machineAPI libmachine.API, host *host.Host) { m, err := machine.NewAPIClient() if err != nil { exit.WithError("Failed to get machine client", err) } - defer m.Close() - - // If --download-only, complete the remaining downloads and exit. - if viper.GetBool(downloadOnly) { - if err := doCacheBinaries(k8sVersion); err != nil { - exit.WithError("Failed to cache binaries", err) - } - waitCacheImages(&cacheGroup) - if err := CacheImagesInConfigFile(); err != nil { - exit.WithError("Failed to cache images", err) - } - console.OutStyle(console.Check, "Download complete!") - return - } - - host, preexisting := startHost(m, config.MachineConfig) + host, preExists = startHost(m, config.MachineConfig) ip := validateNetwork(host) - // Bypass proxy for minikube's vm ip + // Bypass proxy for minikube's vm host ip err = proxy.ExcludeIP(ip) if err != nil { console.ErrT(console.FailureType, "Failed to set NO_PROXY Env. Please use `export NO_PROXY=$NO_PROXY,{{.ip}}`.", console.Arg{"ip": ip}) @@ -247,37 +272,20 @@ func runStart(cmd *cobra.Command, args []string) { if err := saveConfig(config); err != nil { exit.WithError("Failed to save config", err) } - runner, err := machine.CommandRunner(host) + runner, err = machine.CommandRunner(host) if err != nil { exit.WithError("Failed to get command runner", err) } - cr := configureRuntimes(runner) - showVersionInfo(k8sVersion, cr) + return runner, preExists, m, host +} - // prepareHostEnvironment uses the downloaded images, so we need to wait for background task completion. - waitCacheImages(&cacheGroup) - - bs := prepareHostEnvironment(m, config.KubernetesConfig) - - // The kube config must be update must come before bootstrapping, otherwise health checks may use a stale IP - kubeconfig := updateKubeConfig(host, &config) - bootstrapCluster(bs, cr, runner, config.KubernetesConfig, preexisting, isUpgrade) - configureMounts() - if err = LoadCachedImagesInConfigFile(); err != nil { - console.Failure("Unable to load cached images from config file.") +func getKubernetesVersion() (k8sVersion string, isUpgrade bool) { + oldConfig, err := cfg.Load() + if err != nil && !os.IsNotExist(err) { + exit.WithCode(exit.Data, "Unable to load config: %v", err) } - - if config.MachineConfig.VMDriver == constants.DriverNone { - console.OutStyle(console.StartingNone, "Configuring local host environment ...") - prepareNone() - } - - if err := bs.WaitCluster(config.KubernetesConfig); err != nil { - exit.WithError("Wait failed", err) - } - showKubectlConnectInfo(kubeconfig) - + return validateKubernetesVersions(oldConfig) } func downloadISO(config cfg.Config) { @@ -288,11 +296,14 @@ func downloadISO(config cfg.Config) { } } -func skipCache(config cfg.Config) { +func skipCache(config *cfg.Config) { + fmt.Println("inside skip cache") + config.KubernetesConfig.ShouldLoadCachedImages = false if viper.GetString(vmDriver) == constants.DriverNone { viper.Set(cacheImages, false) config.KubernetesConfig.ShouldLoadCachedImages = false } + fmt.Printf("inside skip cache %t", config.KubernetesConfig.ShouldLoadCachedImages) } func showVersionInfo(k8sVersion string, cr cruntime.Manager) { @@ -583,7 +594,11 @@ func autoSetOptions(vmDriver string) error { } // prepareNone prepares the user and host for the joy of the "none" driver -func prepareNone() { +func prepareNone(vmDriver string) { + if vmDriver != constants.DriverNone { + return + } + console.OutStyle(console.StartingNone, "Configuring local host environment ...") if viper.GetBool(cfg.WantNoneDriverWarning) { console.OutLn("") console.Warning("The 'none' driver provides limited isolation and may reduce system security and reliability.") @@ -696,9 +711,9 @@ func validateKubernetesVersions(old *cfg.Config) (string, bool) { return nv, isUpgrade } -// prepareHostEnvironment adds any requested files into the VM before Kubernetes is started -func prepareHostEnvironment(api libmachine.API, kc cfg.KubernetesConfig) bootstrapper.Bootstrapper { - bs, err := GetClusterBootstrapper(api, viper.GetString(cmdcfg.Bootstrapper)) +// setupKubeAdm adds any requested files into the VM before Kubernetes is started +func setupKubeAdm(mAPI libmachine.API, kc cfg.KubernetesConfig) bootstrapper.Bootstrapper { + bs, err := getClusterBootstrapper(mAPI, viper.GetString(cmdcfg.Bootstrapper)) if err != nil { exit.WithError("Failed to get bootstrapper", err) } diff --git a/cmd/minikube/cmd/status.go b/cmd/minikube/cmd/status.go index 668ee9815a..452042eea3 100644 --- a/cmd/minikube/cmd/status.go +++ b/cmd/minikube/cmd/status.go @@ -75,7 +75,7 @@ var statusCmd = &cobra.Command{ apiserverSt := state.None.String() if hostSt == state.Running.String() { - clusterBootstrapper, err := GetClusterBootstrapper(api, viper.GetString(cmdcfg.Bootstrapper)) + clusterBootstrapper, err := getClusterBootstrapper(api, viper.GetString(cmdcfg.Bootstrapper)) if err != nil { exit.WithError("Error getting bootstrapper", err) }