From 129e2fdb189949aeca71cc685abfe566b6133276 Mon Sep 17 00:00:00 2001 From: Medya Gh Date: Mon, 24 Jun 2019 20:48:52 -0700 Subject: [PATCH] Improve readability, defer close api --- cmd/minikube/cmd/delete.go | 92 +++++++++++++++++++------------------- cmd/minikube/cmd/start.go | 3 +- cmd/minikube/cmd/stop.go | 70 +++++++++++++++-------------- 3 files changed, 85 insertions(+), 80 deletions(-) diff --git a/cmd/minikube/cmd/delete.go b/cmd/minikube/cmd/delete.go index 3e6a442d3..754ad7c55 100644 --- a/cmd/minikube/cmd/delete.go +++ b/cmd/minikube/cmd/delete.go @@ -39,59 +39,61 @@ var deleteCmd = &cobra.Command{ Short: "Deletes a local kubernetes cluster", Long: `Deletes a local kubernetes cluster. This command deletes the VM, and removes all associated files.`, - Run: func(cmd *cobra.Command, args []string) { - if len(args) > 0 { - exit.Usage("usage: minikube delete") - } - profile := viper.GetString(pkg_config.MachineProfile) - api, err := machine.NewAPIClient() - if err != nil { - exit.WithError("Error getting client", err) - } - defer api.Close() + Run: runDelete, +} - cc, err := pkg_config.Load() - if err != nil && !os.IsNotExist(err) { - console.ErrLn("Error loading profile config: %v", err) - } +// runDelete handles the executes the flow of "minikube delete" +func runDelete(cmd *cobra.Command, args []string) { + if len(args) > 0 { + exit.Usage("usage: minikube delete") + } + profile := viper.GetString(pkg_config.MachineProfile) + api, err := machine.NewAPIClient() + if err != nil { + exit.WithError("Error getting client", err) + } + defer api.Close() - // 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 { - kc := cc.KubernetesConfig - bsName := viper.GetString(cmdcfg.Bootstrapper) - console.OutStyle(console.Resetting, "Uninstalling Kubernetes %s using %s ...", kc.KubernetesVersion, bsName) - clusterBootstrapper, err := GetClusterBootstrapper(api, viper.GetString(cmdcfg.Bootstrapper)) - if err != nil { - console.ErrLn("Unable to get bootstrapper: %v", err) - } else { - if err = clusterBootstrapper.DeleteCluster(kc); err != nil { - console.ErrLn("Failed to delete cluster: %v", err) - } - } - } + cc, err := pkg_config.Load() + if err != nil && !os.IsNotExist(err) { + console.ErrLn("Error loading profile config: %v", err) + } - if err = cluster.DeleteHost(api); err != nil { - switch err := errors.Cause(err).(type) { - case mcnerror.ErrHostDoesNotExist: - console.OutStyle(console.Meh, "%q cluster does not exist", profile) - default: - exit.WithError("Failed to delete cluster", err) - } + // 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 { + kc := cc.KubernetesConfig + bsName := viper.GetString(cmdcfg.Bootstrapper) + console.OutStyle(console.Resetting, "Uninstalling Kubernetes %s using %s ...", kc.KubernetesVersion, bsName) + clusterBootstrapper, err := GetClusterBootstrapper(api, viper.GetString(cmdcfg.Bootstrapper)) + if err != nil { + console.ErrLn("Unable to get bootstrapper: %v", err) + } else if err = clusterBootstrapper.DeleteCluster(kc); err != nil { + console.ErrLn("Failed to delete cluster: %v", err) } - if err := cmdUtil.KillMountProcess(); err != nil { - console.Fatal("Failed to kill mount process: %v", err) + } + + if err = cluster.DeleteHost(api); err != nil { + switch err := errors.Cause(err).(type) { + case mcnerror.ErrHostDoesNotExist: + console.OutStyle(console.Meh, "%q cluster does not exist", profile) + default: + exit.WithError("Failed to delete cluster", err) } + } + + if err := cmdUtil.KillMountProcess(); err != nil { + console.Fatal("Failed to kill mount process: %v", err) + } - if err := os.RemoveAll(constants.GetProfilePath(viper.GetString(pkg_config.MachineProfile))); err != nil { - if os.IsNotExist(err) { - console.OutStyle(console.Meh, "%q profile does not exist", profile) - os.Exit(0) - } - exit.WithError("Failed to remove profile", err) + if err := os.RemoveAll(constants.GetProfilePath(viper.GetString(pkg_config.MachineProfile))); err != nil { + if os.IsNotExist(err) { + console.OutStyle(console.Meh, "%q profile does not exist", profile) + os.Exit(0) } - console.OutStyle(console.Crushed, "The %q cluster has been deleted.", profile) - }, + exit.WithError("Failed to remove profile", err) + } + console.OutStyle(console.Crushed, "The %q cluster has been deleted.", profile) } func init() { diff --git a/cmd/minikube/cmd/start.go b/cmd/minikube/cmd/start.go index ac4da6c4a..f3b25cad2 100644 --- a/cmd/minikube/cmd/start.go +++ b/cmd/minikube/cmd/start.go @@ -204,6 +204,7 @@ func runStart(cmd *cobra.Command, args []string) { } m, err := machine.NewAPIClient() + defer m.Close() if err != nil { exit.WithError("Failed to get machine client", err) } @@ -525,7 +526,7 @@ func generateConfig(cmd *cobra.Command, k8sVersion string) (cfg.Config, error) { return cfg, nil } -// autoSetOptions sets the options needed for specific configuration automatically. +// autoSetOptions sets the options needed for specific vm-driver automatically. func autoSetOptions(vmDriver string) error { // options for none driver if vmDriver == constants.DriverNone { diff --git a/cmd/minikube/cmd/stop.go b/cmd/minikube/cmd/stop.go index a43a725d8..1b2ec654f 100644 --- a/cmd/minikube/cmd/stop.go +++ b/cmd/minikube/cmd/stop.go @@ -39,46 +39,48 @@ var stopCmd = &cobra.Command{ Short: "Stops a running local kubernetes cluster", Long: `Stops a local kubernetes cluster running in Virtualbox. This command stops the VM itself, leaving all files intact. The cluster can be started again with the "start" command.`, - Run: func(cmd *cobra.Command, args []string) { - profile := viper.GetString(pkg_config.MachineProfile) - api, err := machine.NewAPIClient() - if err != nil { - exit.WithError("Error getting client", err) - } - defer api.Close() + Run: runStop, +} - nonexistent := false +// runStop handles the executes the flow of "minikube stop" +func runStop(cmd *cobra.Command, args []string) { + profile := viper.GetString(pkg_config.MachineProfile) + api, err := machine.NewAPIClient() + if err != nil { + exit.WithError("Error getting client", err) + } + defer api.Close() - stop := func() (err error) { - err = cluster.StopHost(api) - switch err := errors.Cause(err).(type) { - case mcnerror.ErrHostDoesNotExist: - console.OutStyle(console.Meh, "%q VM does not exist, nothing to stop", profile) - nonexistent = true - return nil - default: - return err - } - } - if err := pkgutil.RetryAfter(5, stop, 1*time.Second); err != nil { - exit.WithError("Unable to stop VM", err) - } - if !nonexistent { - console.OutStyle(console.Stopped, "%q stopped.", profile) - } + nonexistent := false - if err := cmdUtil.KillMountProcess(); err != nil { - console.OutStyle(console.WarningType, "Unable to kill mount process: %s", err) + stop := func() (err error) { + err = cluster.StopHost(api) + switch err := errors.Cause(err).(type) { + case mcnerror.ErrHostDoesNotExist: + console.OutStyle(console.Meh, "%q VM does not exist, nothing to stop", profile) + nonexistent = true + return nil + default: + return err } + } + if err := pkgutil.RetryAfter(5, stop, 2*time.Second); err != nil { + exit.WithError("Unable to stop VM", err) + } + if !nonexistent { + console.OutStyle(console.Stopped, "%q stopped.", profile) + } - machineName := pkg_config.GetMachineName() - err = pkgutil.UnsetCurrentContext(constants.KubeconfigPath, machineName) - if err != nil { - exit.WithError("update config", err) - } - }, -} + if err := cmdUtil.KillMountProcess(); err != nil { + console.OutStyle(console.WarningType, "Unable to kill mount process: %s", err) + } + machineName := pkg_config.GetMachineName() + err = pkgutil.UnsetCurrentContext(constants.KubeconfigPath, machineName) + if err != nil { + exit.WithError("update config", err) + } +} func init() { RootCmd.AddCommand(stopCmd) } -- GitLab