Skip to content

Commit

Permalink
Merge pull request #3390 from vardhaman22/change-etcd-restore-contain…
Browse files Browse the repository at this point in the history
…er-image

changed etcd restore image to rke-tools for etcd >=3.5.7
  • Loading branch information
vardhaman22 authored Sep 27, 2023
2 parents 48ad1d6 + bcb83a7 commit 4c11fa9
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
37 changes: 36 additions & 1 deletion cluster/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"golang.org/x/sync/errgroup"
)

const MinEtcdVersionWithDistrolessImage = "v3.5.7"

func (c *Cluster) SnapshotEtcd(ctx context.Context, snapshotName string) error {
backupImage := c.getBackupImage()
containerTimeout := DefaultEtcdBackupConfigTimeout
Expand Down Expand Up @@ -225,13 +227,14 @@ func (c *Cluster) RestoreEtcdSnapshot(ctx context.Context, snapshotPath string)
// Start restore process on all etcd hosts
initCluster := services.GetEtcdInitialCluster(c.EtcdHosts)
backupImage := c.getBackupImage()
restoreImage := c.getRestoreImage()
for _, host := range c.EtcdHosts {
containerTimeout := DefaultEtcdBackupConfigTimeout
if c.Services.Etcd.BackupConfig != nil && c.Services.Etcd.BackupConfig.Timeout > 0 {
containerTimeout = c.Services.Etcd.BackupConfig.Timeout
}
newCtx := context.WithValue(ctx, docker.WaitTimeoutContextKey, containerTimeout)
if err := services.RestoreEtcdSnapshot(newCtx, host, c.PrivateRegistriesMap, c.SystemImages.Etcd, backupImage,
if err := services.RestoreEtcdSnapshot(newCtx, host, c.PrivateRegistriesMap, restoreImage, backupImage,
snapshotPath, initCluster, c.Services.Etcd, c.Version); err != nil {
return fmt.Errorf("[etcd] Failed to restore etcd snapshot: %v", err)
}
Expand Down Expand Up @@ -281,3 +284,35 @@ func (c *Cluster) getBackupImage() string {
logrus.Debugf("[etcd] Image used for etcd snapshot is: [%s]", rkeToolsImage)
return rkeToolsImage
}

func (c *Cluster) getRestoreImage() string {

// use etcd image for restore in case of custom system image
if !strings.Contains(c.SystemImages.Etcd, "rancher/mirrored-coreos-etcd") {
return c.SystemImages.Etcd
}

etcdImageTag, err := util.GetImageTagFromImage(c.SystemImages.Etcd)
if err != nil {
logrus.Errorf("[etcd] getRestoreImage: error extracting tag from etcd image: %v", err)
return ""
}

etcdVersion, err := util.StrToSemVer(etcdImageTag)
if err != nil {
logrus.Errorf("[etcd] getRestoreImage: error converting etcd image tag to semver: %v", err)
return ""
}

minEtcdVersionWithDistrolessImage, err := util.StrToSemVer(MinEtcdVersionWithDistrolessImage)
if err != nil {
logrus.Errorf("[etcd] getRestoreImage: error converting min distroless etcd image version to semver: %v", err)
return ""
}

if etcdVersion.LessThan(*minEtcdVersionWithDistrolessImage) {
return c.SystemImages.Etcd
}

return c.getBackupImage()
}
61 changes: 61 additions & 0 deletions cluster/etcd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cluster

import (
"context"
"testing"

"github.com/rancher/rke/metadata"
"github.com/rancher/rke/types"
v3 "github.com/rancher/rke/types"
"github.com/stretchr/testify/assert"
)

func TestGetRestoreImage(t *testing.T) {
ctx := context.Background()

metadata.InitMetadata(ctx)

cluster := &Cluster{
RancherKubernetesEngineConfig: v3.RancherKubernetesEngineConfig{
SystemImages: types.RKESystemImages{
Etcd: "rancher/mirrored-coreos-etcd:v3.5.7",
Alpine: "rancher/rke-tools:v0.1.90",
},
},
}

expectedRestoreImage := cluster.getBackupImage()
restoreImage := cluster.getRestoreImage()

assert.NotEmpty(t, restoreImage, "")
assert.Equal(t, expectedRestoreImage, restoreImage,
"expected restoreImage is different when etcd image version is v3.5.7")

cluster.SystemImages.Etcd = "rancher/mirrored-coreos-etcd:v3.5.8"

expectedRestoreImage = cluster.getBackupImage()
restoreImage = cluster.getRestoreImage()

assert.NotEmpty(t, restoreImage, "")
assert.Equal(t, expectedRestoreImage, restoreImage,
"expected restoreImage is different when etcd image version is greater than v3.5.7")

cluster.SystemImages.Etcd = "rancher/mirrored-coreos-etcd:v3.5.6"

expectedRestoreImage = cluster.SystemImages.Etcd
restoreImage = cluster.getRestoreImage()

assert.NotEmpty(t, restoreImage, "")
assert.Equal(t, expectedRestoreImage, restoreImage,
"expected restoreImage is different when etcd image version is less than v3.5.7")

// test for custom image
cluster.SystemImages.Etcd = "custom/mirrored-coreos-etcd:v3.5.7"

expectedRestoreImage = cluster.SystemImages.Etcd
restoreImage = cluster.getRestoreImage()

assert.NotEmpty(t, restoreImage, "")
assert.Equal(t, expectedRestoreImage, restoreImage,
"expected restoreImage is different when custom etcd image is used")
}

0 comments on commit 4c11fa9

Please sign in to comment.