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

Determine etcd s3 snapshots by s3 config presence

This commit is contained in:
Sebastiaan van Steenis
2020-01-20 18:43:35 +01:00
parent b7dbac41f2
commit 23e98603bd
2 changed files with 22 additions and 25 deletions

View File

@@ -3,7 +3,6 @@ package cluster
import (
"context"
"fmt"
"regexp"
"github.com/sirupsen/logrus"
@@ -16,10 +15,6 @@ import (
"golang.org/x/sync/errgroup"
)
const (
BackupPrepareError = "failed to prepare backup: restoring S3 backups with no cluster level S3 configuration is not supported"
)
func (c *Cluster) SnapshotEtcd(ctx context.Context, snapshotName string) error {
backupImage := c.getBackupImage()
for _, host := range c.EtcdHosts {
@@ -62,7 +57,13 @@ func (c *Cluster) PrepareBackup(ctx context.Context, snapshotPath string) error
backupImage := c.getBackupImage()
var errors []error
if c.Services.Etcd.BackupConfig == nil || // legacy rke local backup
(c.Services.Etcd.BackupConfig != nil && IsLocalSnapshot(snapshotPath)) { // rancher local backup and snapshot name indicates a local snapshot
(c.Services.Etcd.BackupConfig != nil && c.Services.Etcd.BackupConfig.S3BackupConfig == nil) { // rancher local backup
if c.Services.Etcd.BackupConfig == nil {
log.Infof(ctx, "[etcd] No etcd snapshot configuration found, will use local as source")
}
if c.Services.Etcd.BackupConfig != nil && c.Services.Etcd.BackupConfig.S3BackupConfig == nil {
log.Infof(ctx, "[etcd] etcd snapshot configuration found and no s3 backup configuration found, will use local as source")
}
// 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 {
@@ -104,7 +105,8 @@ 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 && !IsLocalSnapshot(snapshotPath) {
c.Services.Etcd.BackupConfig.S3BackupConfig != nil {
log.Infof(ctx, "[etcd] etcd s3 backup configuration found, will use s3 as source")
for _, host := range c.EtcdHosts {
if err := services.DownloadEtcdSnapshotFromS3(ctx, host, c.PrivateRegistriesMap, backupImage, snapshotPath, c.Services.Etcd); err != nil {
return err
@@ -113,11 +115,6 @@ func (c *Cluster) PrepareBackup(ctx context.Context, snapshotPath string) error
backupReady = true
}
if !backupReady {
if !IsLocalSnapshot(snapshotPath) &&
c.Services.Etcd.BackupConfig != nil &&
c.Services.Etcd.BackupConfig.S3BackupConfig == nil { // s3 backup with no s3 configuration!
return fmt.Errorf(BackupPrepareError)
}
return fmt.Errorf("failed to prepare backup for restore")
}
// this applies to all cases!
@@ -179,16 +176,6 @@ func (c *Cluster) getBackupImage() string {
logrus.Errorf("[etcd] error getting backup image %v", err)
return ""
}
logrus.Debugf("[etcd] Image used for etcd snapshot is: [%s]", rkeToolsImage)
return rkeToolsImage
}
func IsLocalSnapshot(name string) bool {
// name is fmt.Sprintf("%s-%s%s-", cluster.Name, typeFlag, providerFlag)
// typeFlag = "r": recurring
// typeFlag = "m": manaul
//
// providerFlag = "l" local
// providerFlag = "s" s3
re := regexp.MustCompile("^c-[a-z0-9].*?-.l-")
return re.MatchString(name)
}