1
0
mirror of https://github.com/rancher/rke.git synced 2025-06-27 15:59:37 +00:00
rke/cmd/etcd.go

153 lines
4.0 KiB
Go
Raw Normal View History

2018-05-09 17:39:19 +00:00
package cmd
import (
"context"
"fmt"
2018-05-17 22:27:35 +00:00
"time"
2018-05-09 17:39:19 +00:00
"github.com/rancher/rke/cluster"
"github.com/rancher/rke/hosts"
"github.com/rancher/rke/log"
"github.com/rancher/rke/pki"
"github.com/rancher/types/apis/management.cattle.io/v3"
2018-05-17 22:27:35 +00:00
"github.com/sirupsen/logrus"
2018-05-09 17:39:19 +00:00
"github.com/urfave/cli"
)
func EtcdCommand() cli.Command {
2018-05-17 22:27:35 +00:00
snapshotFlags := []cli.Flag{
2018-05-09 17:39:19 +00:00
cli.StringFlag{
Name: "name",
2018-05-17 22:27:35 +00:00
Usage: "Specify Snapshot name",
2018-05-09 17:39:19 +00:00
},
cli.StringFlag{
Name: "config",
Usage: "Specify an alternate cluster YAML file",
Value: pki.ClusterConfig,
EnvVar: "RKE_CONFIG",
},
}
2018-05-17 22:27:35 +00:00
snapshotFlags = append(snapshotFlags, commonFlags...)
2018-05-09 17:39:19 +00:00
return cli.Command{
Name: "etcd",
2018-05-17 22:27:35 +00:00
Usage: "etcd snapshot save/restore operations in k8s cluster",
2018-05-09 17:39:19 +00:00
Subcommands: []cli.Command{
{
Name: "snapshot-save",
Usage: "Take snapshot on all etcd hosts",
2018-05-17 22:27:35 +00:00
Flags: snapshotFlags,
Action: SnapshotSaveEtcdHostsFromCli,
2018-05-09 17:39:19 +00:00
},
{
Name: "snapshot-restore",
Usage: "Restore existing snapshot",
2018-05-17 22:27:35 +00:00
Flags: snapshotFlags,
Action: RestoreEtcdSnapshotFromCli,
2018-05-09 17:39:19 +00:00
},
},
}
}
2018-05-17 22:27:35 +00:00
func SnapshotSaveEtcdHosts(
2018-05-09 17:39:19 +00:00
ctx context.Context,
rkeConfig *v3.RancherKubernetesEngineConfig,
dockerDialerFactory hosts.DialerFactory,
2018-05-17 22:27:35 +00:00
configDir, snapshotName string) error {
2018-05-09 17:39:19 +00:00
log.Infof(ctx, "Starting saving snapshot on etcd hosts")
2018-05-09 17:39:19 +00:00
kubeCluster, err := cluster.ParseCluster(ctx, rkeConfig, clusterFilePath, configDir, dockerDialerFactory, nil, nil)
if err != nil {
return err
}
if err := kubeCluster.TunnelHosts(ctx, false); err != nil {
return err
}
2018-05-17 22:27:35 +00:00
if err := kubeCluster.SnapshotEtcd(ctx, snapshotName); err != nil {
2018-05-09 17:39:19 +00:00
return err
}
if err := kubeCluster.SaveBackupCertificateBundle(ctx); err != nil {
return err
}
2018-05-17 22:27:35 +00:00
log.Infof(ctx, "Finished saving snapshot [%s] on all etcd hosts", snapshotName)
2018-05-09 17:39:19 +00:00
return nil
}
2018-05-17 22:27:35 +00:00
func RestoreEtcdSnapshot(
2018-05-09 17:39:19 +00:00
ctx context.Context,
rkeConfig *v3.RancherKubernetesEngineConfig,
dockerDialerFactory hosts.DialerFactory,
2018-05-17 22:27:35 +00:00
configDir, snapshotName string) error {
2018-05-09 17:39:19 +00:00
log.Infof(ctx, "Starting restoring snapshot on etcd hosts")
2018-05-09 17:39:19 +00:00
kubeCluster, err := cluster.ParseCluster(ctx, rkeConfig, clusterFilePath, configDir, dockerDialerFactory, nil, nil)
if err != nil {
return err
}
if err := kubeCluster.TunnelHosts(ctx, false); err != nil {
return err
}
2018-05-17 22:27:35 +00:00
if err := kubeCluster.RestoreEtcdSnapshot(ctx, snapshotName); err != nil {
2018-05-09 17:39:19 +00:00
return err
}
if err := kubeCluster.ExtractBackupCertificateBundle(ctx); err != nil {
return err
}
2018-05-17 22:27:35 +00:00
log.Infof(ctx, "Finished restoring snapshot [%s] on all etcd hosts", snapshotName)
2018-05-09 17:39:19 +00:00
return nil
}
2018-05-17 22:27:35 +00:00
func SnapshotSaveEtcdHostsFromCli(ctx *cli.Context) error {
2018-05-09 17:39:19 +00:00
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
}
2018-05-17 22:27:35 +00:00
// Check snapshot name
etcdSnapshotName := ctx.String("name")
if etcdSnapshotName == "" {
etcdSnapshotName = fmt.Sprintf("rke_etcd_snapshot_%s", time.Now().Format(time.RFC3339))
logrus.Warnf("Name of the snapshot is not specified using [%s]", etcdSnapshotName)
}
return SnapshotSaveEtcdHosts(context.Background(), rkeConfig, nil, "", etcdSnapshotName)
2018-05-09 17:39:19 +00:00
}
2018-05-17 22:27:35 +00:00
func RestoreEtcdSnapshotFromCli(ctx *cli.Context) error {
2018-05-09 17:39:19 +00:00
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
}
2018-05-17 22:27:35 +00:00
etcdSnapshotName := ctx.String("name")
if etcdSnapshotName == "" {
return fmt.Errorf("You must specify the snapshot name to restore")
}
return RestoreEtcdSnapshot(context.Background(), rkeConfig, nil, "", etcdSnapshotName)
2018-05-09 17:39:19 +00:00
}