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

Improve readability, defer close api

上级 34fda98f
...@@ -39,59 +39,61 @@ var deleteCmd = &cobra.Command{ ...@@ -39,59 +39,61 @@ var deleteCmd = &cobra.Command{
Short: "Deletes a local kubernetes cluster", Short: "Deletes a local kubernetes cluster",
Long: `Deletes a local kubernetes cluster. This command deletes the VM, and removes all Long: `Deletes a local kubernetes cluster. This command deletes the VM, and removes all
associated files.`, associated files.`,
Run: func(cmd *cobra.Command, args []string) { Run: runDelete,
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()
cc, err := pkg_config.Load() // runDelete handles the executes the flow of "minikube delete"
if err != nil && !os.IsNotExist(err) { func runDelete(cmd *cobra.Command, args []string) {
console.ErrLn("Error loading profile config: %v", err) 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 cc, err := pkg_config.Load()
if err == nil && cc.MachineConfig.VMDriver == constants.DriverNone { if err != nil && !os.IsNotExist(err) {
kc := cc.KubernetesConfig console.ErrLn("Error loading profile config: %v", err)
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 = cluster.DeleteHost(api); err != nil { // In the case of "none", we want to uninstall Kubernetes as there is no VM to delete
switch err := errors.Cause(err).(type) { if err == nil && cc.MachineConfig.VMDriver == constants.DriverNone {
case mcnerror.ErrHostDoesNotExist: kc := cc.KubernetesConfig
console.OutStyle(console.Meh, "%q cluster does not exist", profile) bsName := viper.GetString(cmdcfg.Bootstrapper)
default: console.OutStyle(console.Resetting, "Uninstalling Kubernetes %s using %s ...", kc.KubernetesVersion, bsName)
exit.WithError("Failed to delete cluster", err) 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 err := os.RemoveAll(constants.GetProfilePath(viper.GetString(pkg_config.MachineProfile))); err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
console.OutStyle(console.Meh, "%q profile does not exist", profile) console.OutStyle(console.Meh, "%q profile does not exist", profile)
os.Exit(0) os.Exit(0)
}
exit.WithError("Failed to remove profile", err)
} }
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() { func init() {
......
...@@ -204,6 +204,7 @@ func runStart(cmd *cobra.Command, args []string) { ...@@ -204,6 +204,7 @@ func runStart(cmd *cobra.Command, args []string) {
} }
m, err := machine.NewAPIClient() m, err := machine.NewAPIClient()
defer m.Close()
if err != nil { if err != nil {
exit.WithError("Failed to get machine client", err) exit.WithError("Failed to get machine client", err)
} }
...@@ -525,7 +526,7 @@ func generateConfig(cmd *cobra.Command, k8sVersion string) (cfg.Config, error) { ...@@ -525,7 +526,7 @@ func generateConfig(cmd *cobra.Command, k8sVersion string) (cfg.Config, error) {
return cfg, nil 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 { func autoSetOptions(vmDriver string) error {
// options for none driver // options for none driver
if vmDriver == constants.DriverNone { if vmDriver == constants.DriverNone {
......
...@@ -39,46 +39,48 @@ var stopCmd = &cobra.Command{ ...@@ -39,46 +39,48 @@ var stopCmd = &cobra.Command{
Short: "Stops a running local kubernetes cluster", Short: "Stops a running local kubernetes cluster",
Long: `Stops a local kubernetes cluster running in Virtualbox. This command stops the VM 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.`, itself, leaving all files intact. The cluster can be started again with the "start" command.`,
Run: func(cmd *cobra.Command, args []string) { Run: runStop,
profile := viper.GetString(pkg_config.MachineProfile) }
api, err := machine.NewAPIClient()
if err != nil {
exit.WithError("Error getting client", err)
}
defer api.Close()
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) { nonexistent := false
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)
}
if err := cmdUtil.KillMountProcess(); err != nil { stop := func() (err error) {
console.OutStyle(console.WarningType, "Unable to kill mount process: %s", err) 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() if err := cmdUtil.KillMountProcess(); err != nil {
err = pkgutil.UnsetCurrentContext(constants.KubeconfigPath, machineName) console.OutStyle(console.WarningType, "Unable to kill mount process: %s", err)
if err != nil { }
exit.WithError("update config", err)
}
},
}
machineName := pkg_config.GetMachineName()
err = pkgutil.UnsetCurrentContext(constants.KubeconfigPath, machineName)
if err != nil {
exit.WithError("update config", err)
}
}
func init() { func init() {
RootCmd.AddCommand(stopCmd) RootCmd.AddCommand(stopCmd)
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册