Add DeleteCluster to bootstrapper

This matches StartCluster, and is used to revert
the changes done to reset the VM machine state.

Even if it fails, continue to deleting the VM...
Make sure to handle the case of missing config.
pull/3656/head
Anders F Björklund 2019-02-12 19:47:04 +01:00
parent ec5107ea96
commit 0a91040434
4 changed files with 29 additions and 1 deletions

View File

@ -23,6 +23,7 @@ import (
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
cmdcfg "k8s.io/minikube/cmd/minikube/cmd/config"
cmdUtil "k8s.io/minikube/cmd/util"
"k8s.io/minikube/pkg/minikube/cluster"
pkg_config "k8s.io/minikube/pkg/minikube/config"
@ -43,7 +44,6 @@ associated files.`,
os.Exit(1)
}
profile := viper.GetString(pkg_config.MachineProfile)
console.OutStyle("deleting-vm", "Deleting %q Kubernetes cluster ...", profile)
api, err := machine.NewAPIClient()
if err != nil {
console.Fatal("Error getting client: %v", err)
@ -51,6 +51,22 @@ associated files.`,
}
defer api.Close()
cc, err := pkg_config.Load()
if err != nil && !os.IsNotExist(err) {
console.ErrLn("Error loading profile config: %v", err)
} else if err == nil {
kc := cc.KubernetesConfig
bsName := viper.GetString(cmdcfg.Bootstrapper) // Name ?
console.OutStyle("resetting", "Reverting Kubernetes %s using %s ...", kc.KubernetesVersion, bsName)
clusterBootstrapper, err := GetClusterBootstrapper(api, viper.GetString(cmdcfg.Bootstrapper))
if err != nil {
if err = clusterBootstrapper.DeleteCluster(kc); err != nil {
console.ErrLn("Failed to delete cluster: %v", err)
}
}
}
console.OutStyle("deleting-vm", "Deleting %q Kubernetes VM ...", profile)
if err = cluster.DeleteHost(api); err != nil {
switch err := errors.Cause(err).(type) {
case mcnerror.ErrHostDoesNotExist:

View File

@ -31,6 +31,7 @@ type Bootstrapper interface {
StartCluster(config.KubernetesConfig) error
UpdateCluster(config.KubernetesConfig) error
RestartCluster(config.KubernetesConfig) error
DeleteCluster(config.KubernetesConfig) error
GetClusterLogsTo(follow bool, out io.Writer) error
SetupCerts(cfg config.KubernetesConfig) error
GetKubeletStatus() (string, error)

View File

@ -253,6 +253,16 @@ func (k *KubeadmBootstrapper) RestartCluster(k8s config.KubernetesConfig) error
return nil
}
// DeleteCluster removes the components that were started earlier
func (k *KubeadmBootstrapper) DeleteCluster(k8s config.KubernetesConfig) error {
cmd := fmt.Sprintf("sudo kubeadm reset --config %s", constants.KubeadmConfigFile)
if err := k.c.Run(cmd); err != nil {
return errors.Wrapf(err, "running cmd: %s", cmd)
}
return nil
}
// PullImages downloads images that will be used by RestartCluster
func (k *KubeadmBootstrapper) PullImages(k8s config.KubernetesConfig) error {
cmd := fmt.Sprintf("sudo kubeadm config images pull --config %s", constants.KubeadmConfigFile)

View File

@ -57,6 +57,7 @@ var styles = map[string]style{
"caching": {Prefix: "🤹 "},
"starting-vm": {Prefix: "🔥 "},
"starting-none": {Prefix: "🤹 "},
"resetting": {Prefix: "🔄 "},
"deleting-vm": {Prefix: "🔥 "},
"copying": {Prefix: "✨ "},
"connectivity": {Prefix: "📶 "},