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

delete: Clean up machine directory if DeleteHost fails to #5106

Merged
merged 1 commit into from
Aug 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions cmd/minikube/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,35 +63,46 @@ func runDelete(cmd *cobra.Command, args []string) {

cc, err := pkg_config.Load()
if err != nil && !os.IsNotExist(err) {
out.ErrT(out.Sad, "Error loading profile config: {{.error}}", out.V{"name": profile})
out.ErrT(out.Sad, "Error loading profile {{.name}}: {{.error}}", out.V{"name": profile, "error": 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 {
uninstallKubernetes(api, cc.KubernetesConfig, viper.GetString(cmdcfg.Bootstrapper))
}

if err := killMountProcess(); err != nil {
out.T(out.FailureType, "Failed to kill mount process: {{.error}}", out.V{"error": err})
}

if err = cluster.DeleteHost(api); err != nil {
switch err := errors.Cause(err).(type) {
switch errors.Cause(err).(type) {
case mcnerror.ErrHostDoesNotExist:
out.T(out.Meh, `"{{.name}}" cluster does not exist`, out.V{"name": profile})
default:
exit.WithError("Failed to delete cluster", err)
out.T(out.FailureType, "Failed to delete cluster: {{.error}}", out.V{"error": err})
out.T(out.Notice, `You may need to manually remove the "{{.name}}" VM from your hypervisor`, out.V{"name": profile})
}
}

if err := killMountProcess(); err != nil {
out.FatalT("Failed to kill mount process: {{.error}}", out.V{"error": err})
// In case DeleteHost didn't complete the job.
machineDir := filepath.Join(constants.GetMinipath(), "machines", profile)
if _, err := os.Stat(machineDir); err == nil {
out.T(out.DeletingHost, `Removing {{.directory}} ...`, out.V{"directory": machineDir})
err := os.RemoveAll(machineDir)
if err != nil {
exit.WithError("Unable to remove machine directory: %v", err)
}
}

if err := pkg_config.DeleteProfile(viper.GetString(pkg_config.MachineProfile)); err != nil {
if err := pkg_config.DeleteProfile(profile); err != nil {
if os.IsNotExist(err) {
out.T(out.Meh, `"{{.profile_name}}" profile does not exist`, out.V{"profile_name": profile})
out.T(out.Meh, `"{{.name}}" profile does not exist`, out.V{"name": profile})
os.Exit(0)
}
exit.WithError("Failed to remove profile", err)
}
out.T(out.Crushed, `The "{{.cluster_name}}" cluster has been deleted.`, out.V{"cluster_name": profile})
out.T(out.Crushed, `The "{{.name}}" cluster has been deleted.`, out.V{"name": profile})

machineName := pkg_config.GetMachineName()
if err := kubeconfig.DeleteContext(constants.KubeconfigPath, machineName); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/minikube/machine/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (api *LocalClient) NewHost(driverName string, rawDriver []byte) (*host.Host
func (api *LocalClient) Load(name string) (*host.Host, error) {
h, err := api.Filestore.Load(name)
if err != nil {
return nil, errors.Wrap(err, "filestore")
return nil, errors.Wrapf(err, "filestore %q", name)
}

var def registry.DriverDef
Expand Down