1 Star 0 Fork 0

powerpaas/rke

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
remove.go 3.16 KB
一键复制 编辑 原始数据 按行查看 历史
Bill Maxwell 提交于 2018-03-05 17:52 . Leverage global SSHAgentAuth setting
package cmd
import (
"bufio"
"context"
"fmt"
"os"
"strings"
"github.com/rancher/rke/cluster"
"github.com/rancher/rke/hosts"
"github.com/rancher/rke/k8s"
"github.com/rancher/rke/log"
"github.com/rancher/rke/pki"
"github.com/rancher/types/apis/management.cattle.io/v3"
"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: pki.ClusterConfig,
EnvVar: "RKE_CONFIG",
},
cli.BoolFlag{
Name: "force",
Usage: "Force removal of the cluster",
},
cli.BoolFlag{
Name: "local",
Usage: "Deploy Kubernetes cluster locally",
},
}
removeFlags = append(removeFlags, sshCliOptions...)
return cli.Command{
Name: "remove",
Usage: "Teardown the cluster and clean cluster nodes",
Action: clusterRemoveFromCli,
Flags: removeFlags,
}
}
func ClusterRemove(
ctx context.Context,
rkeConfig *v3.RancherKubernetesEngineConfig,
dialerFactory hosts.DialerFactory,
k8sWrapTransport k8s.WrapTransport,
local bool, configDir string) error {
log.Infof(ctx, "Tearing down Kubernetes cluster")
kubeCluster, err := cluster.ParseCluster(ctx, rkeConfig, clusterFilePath, configDir, dialerFactory, nil, k8sWrapTransport)
if err != nil {
return err
}
err = kubeCluster.TunnelHosts(ctx, local)
if err != nil {
return err
}
logrus.Debugf("Starting Cluster removal")
err = kubeCluster.ClusterRemove(ctx)
if err != nil {
return err
}
log.Infof(ctx, "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
}
}
if ctx.Bool("local") {
return clusterRemoveLocal(ctx)
}
clusterFile, filePath, err := resolveClusterFile(ctx)
if err != nil {
return fmt.Errorf("Failed to resolve cluster file: %v", err)
}
clusterFilePath = filePath
rkeConfig, err := cluster.ParseConfig(clusterFile)
if err != nil {
return fmt.Errorf("Failed to parse cluster file: %v", err)
}
rkeConfig, err = setOptionsFromCLI(ctx, rkeConfig)
if err != nil {
return err
}
return ClusterRemove(context.Background(), rkeConfig, nil, nil, false, "")
}
func clusterRemoveLocal(ctx *cli.Context) error {
var rkeConfig *v3.RancherKubernetesEngineConfig
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()}
}
rkeConfig, err = setOptionsFromCLI(ctx, rkeConfig)
if err != nil {
return err
}
return ClusterRemove(context.Background(), rkeConfig, nil, nil, true, "")
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/powerpaas/rke.git
git@gitee.com:powerpaas/rke.git
powerpaas
rke
rke
v0.1.6-rc1

搜索帮助