提交 129e2fdb 编写于 作者: M Medya Gh

Improve readability, defer close api

上级 34fda98f
......@@ -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() {
......
......@@ -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 {
......
......@@ -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)
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册