2017-11-28 11:26:15 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2018-01-09 22:10:56 +00:00
|
|
|
"context"
|
2017-11-28 11:26:15 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/rancher/rke/cluster"
|
2017-12-11 18:28:08 +00:00
|
|
|
"github.com/rancher/rke/hosts"
|
2018-01-09 22:10:56 +00:00
|
|
|
"github.com/rancher/rke/log"
|
2017-12-16 03:38:15 +00:00
|
|
|
"github.com/rancher/types/apis/management.cattle.io/v3"
|
2017-11-28 11:26:15 +00:00
|
|
|
"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",
|
|
|
|
},
|
2017-12-22 01:01:53 +00:00
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "local",
|
|
|
|
Usage: "Deploy Kubernetes cluster locally",
|
|
|
|
},
|
2017-11-28 11:26:15 +00:00
|
|
|
}
|
|
|
|
return cli.Command{
|
|
|
|
Name: "remove",
|
|
|
|
Usage: "Teardown the cluster and clean cluster nodes",
|
|
|
|
Action: clusterRemoveFromCli,
|
|
|
|
Flags: removeFlags,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-22 01:01:53 +00:00
|
|
|
func ClusterRemove(
|
|
|
|
ctx context.Context,
|
|
|
|
rkeConfig *v3.RancherKubernetesEngineConfig,
|
|
|
|
dialerFactory hosts.DialerFactory,
|
|
|
|
local bool, configDir string) error {
|
|
|
|
|
2018-02-01 21:28:31 +00:00
|
|
|
log.Infof(ctx, "Tearing down Kubernetes cluster")
|
2017-12-22 01:01:53 +00:00
|
|
|
kubeCluster, err := cluster.ParseCluster(ctx, rkeConfig, clusterFilePath, configDir, dialerFactory, nil)
|
2017-11-28 11:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-12-22 01:01:53 +00:00
|
|
|
err = kubeCluster.TunnelHosts(ctx, local)
|
2017-11-28 11:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
logrus.Debugf("Starting Cluster removal")
|
2018-01-09 22:10:56 +00:00
|
|
|
err = kubeCluster.ClusterRemove(ctx)
|
2017-11-28 11:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-09 22:10:56 +00:00
|
|
|
log.Infof(ctx, "Cluster removed successfully")
|
2017-11-28 11:26:15 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2018-01-15 04:36:28 +00:00
|
|
|
if ctx.Bool("local") {
|
|
|
|
return clusterRemoveLocal(ctx)
|
|
|
|
}
|
2017-12-16 03:38:15 +00:00
|
|
|
clusterFile, filePath, err := resolveClusterFile(ctx)
|
2017-11-28 11:26:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to resolve cluster file: %v", err)
|
|
|
|
}
|
2017-12-16 03:38:15 +00:00
|
|
|
clusterFilePath = filePath
|
|
|
|
rkeConfig, err := cluster.ParseConfig(clusterFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to parse cluster file: %v", err)
|
|
|
|
}
|
2018-01-15 04:36:28 +00:00
|
|
|
return ClusterRemove(context.Background(), rkeConfig, nil, false, "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func clusterRemoveLocal(ctx *cli.Context) error {
|
|
|
|
var rkeConfig *v3.RancherKubernetesEngineConfig
|
2018-02-03 01:04:53 +00:00
|
|
|
clusterFile, filePath, err := resolveClusterFile(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Infof(context.Background(), "Failed to resolve cluster file, using default cluster instead")
|
|
|
|
rkeConfig = cluster.GetLocalRKEConfig()
|
|
|
|
} else {
|
|
|
|
clusterFilePath = filePath
|
|
|
|
rkeConfig, err = cluster.ParseConfig(clusterFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to parse cluster file: %v", err)
|
|
|
|
}
|
|
|
|
rkeConfig.Nodes = []v3.RKEConfigNode{*cluster.GetLocalRKENodeConfig()}
|
|
|
|
}
|
2018-01-15 04:36:28 +00:00
|
|
|
return ClusterRemove(context.Background(), rkeConfig, nil, true, "")
|
2017-11-28 11:26:15 +00:00
|
|
|
}
|