1
0
mirror of https://github.com/rancher/rke.git synced 2025-09-17 23:49:06 +00:00

Use supported rke-tools for backup and restore in legacy clusters

This commit is contained in:
moelsayed
2019-02-15 22:40:36 +02:00
committed by Alena Prokharchyk
parent 30661bc429
commit f8b6131dd2
3 changed files with 52 additions and 32 deletions

View File

@@ -3,7 +3,6 @@ package cluster
import (
"context"
"fmt"
"strings"
"github.com/rancher/rke/docker"
"github.com/rancher/rke/hosts"
@@ -12,13 +11,13 @@ import (
"github.com/rancher/rke/util"
)
const (
SupportedSyncToolsVersion = "0.1.22"
)
func (c *Cluster) SnapshotEtcd(ctx context.Context, snapshotName string) error {
backupImage := c.getBackupImage()
if !util.IsRancherBackupSupported(c.SystemImages.Alpine) {
log.Warnf(ctx, "Auto local backup sync is not supported in `%s`. Using `%s` instead.", c.SystemImages.Alpine, backupImage)
}
for _, host := range c.EtcdHosts {
if err := services.RunEtcdSnapshotSave(ctx, host, c.PrivateRegistriesMap, c.SystemImages.Alpine, snapshotName, true, c.Services.Etcd); err != nil {
if err := services.RunEtcdSnapshotSave(ctx, host, c.PrivateRegistriesMap, backupImage, snapshotName, true, c.Services.Etcd); err != nil {
return err
}
}
@@ -28,17 +27,19 @@ func (c *Cluster) SnapshotEtcd(ctx context.Context, snapshotName string) error {
func (c *Cluster) PrepareBackup(ctx context.Context, snapshotPath string) error {
// local backup case
var backupServer *hosts.Host
// stop etcd on all etcd nodes, we need this because we start the backup server on the same port
if !isAutoSyncSupported(c.SystemImages.Alpine) {
log.Warnf(ctx, "Auto local backup sync is not supported. Use `rancher/rke-tools:%s` or up", SupportedSyncToolsVersion)
} else if c.Services.Etcd.BackupConfig == nil || // legacy rke local backup
backupImage := c.getBackupImage()
if !util.IsRancherBackupSupported(c.SystemImages.Alpine) {
log.Warnf(ctx, "Auto local backup sync is not supported in `%s`. Using `%s` instead.", c.SystemImages.Alpine, backupImage)
}
if c.Services.Etcd.BackupConfig == nil || // legacy rke local backup
(c.Services.Etcd.BackupConfig != nil && c.Services.Etcd.BackupConfig.S3BackupConfig == nil) { // rancher local backup, no s3
// stop etcd on all etcd nodes, we need this because we start the backup server on the same port
for _, host := range c.EtcdHosts {
if err := docker.StopContainer(ctx, host.DClient, host.Address, services.EtcdContainerName); err != nil {
log.Warnf(ctx, "failed to stop etcd container on host [%s]: %v", host.Address, err)
}
if backupServer == nil { // start the download server, only one node should have it!
if err := services.StartBackupServer(ctx, host, c.PrivateRegistriesMap, c.SystemImages.Alpine, snapshotPath); err != nil {
if err := services.StartBackupServer(ctx, host, c.PrivateRegistriesMap, backupImage, snapshotPath); err != nil {
log.Warnf(ctx, "failed to start backup server on host [%s]: %v", host.Address, err)
continue
}
@@ -50,7 +51,7 @@ func (c *Cluster) PrepareBackup(ctx context.Context, snapshotPath string) error
if backupServer != nil && host.Address == backupServer.Address { // we skip the backup server if it's there
continue
}
if err := services.DownloadEtcdSnapshotFromBackupServer(ctx, host, c.PrivateRegistriesMap, c.SystemImages.Alpine, snapshotPath, backupServer); err != nil {
if err := services.DownloadEtcdSnapshotFromBackupServer(ctx, host, c.PrivateRegistriesMap, backupImage, snapshotPath, backupServer); err != nil {
return err
}
}
@@ -63,7 +64,7 @@ func (c *Cluster) PrepareBackup(ctx context.Context, snapshotPath string) error
// s3 backup case
if c.Services.Etcd.BackupConfig != nil && c.Services.Etcd.BackupConfig.S3BackupConfig != nil {
for _, host := range c.EtcdHosts {
if err := services.DownloadEtcdSnapshotFromS3(ctx, host, c.PrivateRegistriesMap, c.SystemImages.Alpine, snapshotPath, c.Services.Etcd); err != nil {
if err := services.DownloadEtcdSnapshotFromS3(ctx, host, c.PrivateRegistriesMap, backupImage, snapshotPath, c.Services.Etcd); err != nil {
return err
}
}
@@ -75,6 +76,7 @@ func (c *Cluster) PrepareBackup(ctx context.Context, snapshotPath string) error
}
return nil
}
func (c *Cluster) RestoreEtcdSnapshot(ctx context.Context, snapshotPath string) error {
// Start restore process on all etcd hosts
initCluster := services.GetEtcdInitialCluster(c.EtcdHosts)
@@ -89,8 +91,10 @@ func (c *Cluster) RestoreEtcdSnapshot(ctx context.Context, snapshotPath string)
func (c *Cluster) etcdSnapshotChecksum(ctx context.Context, snapshotPath string) bool {
log.Infof(ctx, "[etcd] Checking if all snapshots are identical")
etcdChecksums := []string{}
backupImage := c.getBackupImage()
for _, etcdHost := range c.EtcdHosts {
checksum, err := services.GetEtcdSnapshotChecksum(ctx, etcdHost, c.PrivateRegistriesMap, c.SystemImages.Alpine, snapshotPath)
checksum, err := services.GetEtcdSnapshotChecksum(ctx, etcdHost, c.PrivateRegistriesMap, backupImage, snapshotPath)
if err != nil {
return false
}
@@ -106,21 +110,9 @@ func (c *Cluster) etcdSnapshotChecksum(ctx context.Context, snapshotPath string)
return true
}
func isAutoSyncSupported(image string) bool {
v := strings.Split(image, ":")
last := v[len(v)-1]
sv, err := util.StrToSemVer(last)
if err != nil {
return false
func (c *Cluster) getBackupImage() string {
if util.IsRancherBackupSupported(c.SystemImages.Alpine) {
return c.SystemImages.Alpine
}
supported, err := util.StrToSemVer(SupportedSyncToolsVersion)
if err != nil {
return false
}
if sv.LessThan(*supported) {
return false
}
return true
return util.GetDefaultRKETools()
}