mirror of
https://github.com/rancher/rke.git
synced 2025-04-28 19:43:26 +00:00
77 lines
1.5 KiB
Go
77 lines
1.5 KiB
Go
|
package cmd
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/rancher/rke/cluster"
|
||
|
"github.com/sirupsen/logrus"
|
||
|
"github.com/urfave/cli"
|
||
|
)
|
||
|
|
||
|
func RemoveCommand() cli.Command {
|
||
|
removeFlags := []cli.Flag{
|
||
|
cli.StringFlag{
|
||
|
Name: "config",
|
||
|
Usage: "Specify an alternate cluster YAML file",
|
||
|
Value: cluster.DefaultClusterConfig,
|
||
|
EnvVar: "RKE_CONFIG",
|
||
|
},
|
||
|
cli.BoolFlag{
|
||
|
Name: "force",
|
||
|
Usage: "Force removal of the cluster",
|
||
|
},
|
||
|
}
|
||
|
return cli.Command{
|
||
|
Name: "remove",
|
||
|
Usage: "Teardown the cluster and clean cluster nodes",
|
||
|
Action: clusterRemoveFromCli,
|
||
|
Flags: removeFlags,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func ClusterRemove(clusterFile string) error {
|
||
|
logrus.Infof("Tearing down Kubernetes cluster")
|
||
|
kubeCluster, err := cluster.ParseConfig(clusterFile)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
err = kubeCluster.TunnelHosts()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
logrus.Debugf("Starting Cluster removal")
|
||
|
err = kubeCluster.ClusterRemove()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
logrus.Infof("Cluster removed successfully")
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func clusterRemoveFromCli(ctx *cli.Context) error {
|
||
|
force := ctx.Bool("force")
|
||
|
if !force {
|
||
|
reader := bufio.NewReader(os.Stdin)
|
||
|
fmt.Printf("Are you sure you want to remove Kubernetes cluster [y/n]: ")
|
||
|
input, err := reader.ReadString('\n')
|
||
|
input = strings.TrimSpace(input)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
if input != "y" && input != "Y" {
|
||
|
return nil
|
||
|
}
|
||
|
}
|
||
|
clusterFile, err := resolveClusterFile(ctx)
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("Failed to resolve cluster file: %v", err)
|
||
|
}
|
||
|
return ClusterRemove(clusterFile)
|
||
|
}
|