Skip to content

Commit

Permalink
chore: apply the cluster.spec.backup to BackupSchedule (#7492)
Browse files Browse the repository at this point in the history
(cherry picked from commit fd2831b)
  • Loading branch information
wangyelei committed Jun 5, 2024
1 parent feaf79f commit 7897fea
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 88 deletions.
3 changes: 1 addition & 2 deletions controllers/apps/backuppolicytemplate_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ var _ = Describe("", func() {
BackupMethod = "test-bm"
ActionSetName = "test-as"
VsBackupMethodName = "test-vs-bm"
VsActionSetName = "test-vs-as"
ttl = "7d"
)

Expand Down Expand Up @@ -69,7 +68,7 @@ var _ = Describe("", func() {
SetComponentDef(compDef1, compDef2).
AddBackupMethod(BackupMethod, false, ActionSetName).
SetBackupMethodVolumeMounts("data", "/data").
AddBackupMethod(VsBackupMethodName, true, VsActionSetName).
AddBackupMethod(VsBackupMethodName, true, "").
SetBackupMethodVolumeMounts("data", "/data").
AddSchedule(BackupMethod, "0 0 * * *", ttl, true).
AddSchedule(VsBackupMethodName, "0 0 * * *", ttl, true).
Expand Down
14 changes: 13 additions & 1 deletion controllers/apps/cluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1359,10 +1359,19 @@ var _ = Describe("Cluster Controller", func() {

checkSchedule := func(g Gomega, schedule *dpv1alpha1.BackupSchedule) {
var policy *dpv1alpha1.SchedulePolicy
enableOtherFullMethod := false
for i, s := range schedule.Spec.Schedules {
if s.BackupMethod == backup.Method {
Expect(*s.Enabled).Should(BeEquivalentTo(*backup.Enabled))
policy = &schedule.Spec.Schedules[i]
if *backup.Enabled {
enableOtherFullMethod = true
}
continue
}
if enableOtherFullMethod {
// another full backup method should be disabled.
Expect(*s.Enabled).Should(BeFalse())
}
}
if backup.Enabled != nil && *backup.Enabled {
Expand Down Expand Up @@ -1535,6 +1544,9 @@ var _ = Describe("Cluster Controller", func() {
})

func createBackupPolicyTpl(clusterDefObj *appsv1alpha1.ClusterDefinition, compDef string, mappingClusterVersions ...string) {
By("create actionSet")
fakeActionSet(clusterDefObj.Name)

By("Creating a BackupPolicyTemplate")
bpt := testapps.NewBackupPolicyTemplateFactory(backupPolicyTPLName).
AddLabels(constant.ClusterDefLabelKey, clusterDefObj.Name).
Expand All @@ -1546,7 +1558,7 @@ func createBackupPolicyTpl(clusterDefObj *appsv1alpha1.ClusterDefinition, compDe
AddBackupMethod(backupMethodName, false, actionSetName, mappingClusterVersions...).
SetComponentDef(compDef).
SetBackupMethodVolumeMounts("data", "/data").
AddBackupMethod(vsBackupMethodName, true, vsActionSetName).
AddBackupMethod(vsBackupMethodName, true, "").
SetBackupMethodVolumes([]string{"data"}).
AddSchedule(backupMethodName, "0 0 * * *", ttl, true).
AddSchedule(vsBackupMethodName, "0 0 * * *", ttl, true)
Expand Down
87 changes: 45 additions & 42 deletions controllers/apps/component_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ const (
backupMethodName = "test-backup-method"
vsBackupMethodName = "test-vs-backup-method"
actionSetName = "test-action-set"
vsActionSetName = "test-vs-action-set"
)

var (
Expand Down Expand Up @@ -712,47 +711,7 @@ var _ = Describe("Component Controller", func() {

if policyType == appsv1alpha1.HScaleDataClonePolicyCloneVolume {
By("creating actionSet if backup policy is backup")
actionSet := &dpv1alpha1.ActionSet{
ObjectMeta: metav1.ObjectMeta{
Name: actionSetName,
Namespace: clusterKey.Namespace,
Labels: map[string]string{
constant.ClusterDefLabelKey: clusterDef.Name,
},
},
Spec: dpv1alpha1.ActionSetSpec{
Env: []corev1.EnvVar{
{
Name: "test-name",
Value: "test-value",
},
},
BackupType: dpv1alpha1.BackupTypeFull,
Backup: &dpv1alpha1.BackupActionSpec{
BackupData: &dpv1alpha1.BackupDataActionSpec{
JobActionSpec: dpv1alpha1.JobActionSpec{
BaseJobActionSpec: dpv1alpha1.BaseJobActionSpec{
Image: "xtrabackup",
Command: []string{""},
},
},
},
},
Restore: &dpv1alpha1.RestoreActionSpec{
PrepareData: &dpv1alpha1.JobActionSpec{
BaseJobActionSpec: dpv1alpha1.BaseJobActionSpec{
Image: "xtrabackup",
Command: []string{
"sh",
"-c",
"/backup_scripts.sh",
},
},
},
},
},
}
testapps.CheckedCreateK8sResource(&testCtx, actionSet)
fakeActionSet(clusterDef.Name)
}
}
})()).ShouldNot(HaveOccurred())
Expand Down Expand Up @@ -2470,3 +2429,47 @@ func checkRestoreAndSetCompleted(clusterKey types.NamespacedName, compName strin
By("Mocking restore phase to succeeded")
mockRestoreCompleted(ml)
}

func fakeActionSet(clusterDefName string) *dpv1alpha1.ActionSet {
actionSet := &dpv1alpha1.ActionSet{
ObjectMeta: metav1.ObjectMeta{
Name: actionSetName,
Labels: map[string]string{
constant.ClusterDefLabelKey: clusterDefName,
},
},
Spec: dpv1alpha1.ActionSetSpec{
Env: []corev1.EnvVar{
{
Name: "test-name",
Value: "test-value",
},
},
BackupType: dpv1alpha1.BackupTypeFull,
Backup: &dpv1alpha1.BackupActionSpec{
BackupData: &dpv1alpha1.BackupDataActionSpec{
JobActionSpec: dpv1alpha1.JobActionSpec{
BaseJobActionSpec: dpv1alpha1.BaseJobActionSpec{
Image: "xtrabackup",
Command: []string{""},
},
},
},
},
Restore: &dpv1alpha1.RestoreActionSpec{
PrepareData: &dpv1alpha1.JobActionSpec{
BaseJobActionSpec: dpv1alpha1.BaseJobActionSpec{
Image: "xtrabackup",
Command: []string{
"sh",
"-c",
"/backup_scripts.sh",
},
},
},
},
},
}
testapps.CheckedCreateK8sResource(&testCtx, actionSet)
return actionSet
}
33 changes: 16 additions & 17 deletions controllers/apps/transformer_cluster_backup_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,31 +606,24 @@ func (r *clusterBackupPolicyTransformer) mergeClusterBackup(
// method, for instance, the method is changed from A to B, we need to
// disable A and enable B.
exist := false
hasSyncPITRMethod := false
enableAutoBackup := boolptr.IsSetToTrue(backup.Enabled)
for i, s := range backupSchedule.Spec.Schedules {
if s.BackupMethod == backup.Method {
mergeSchedulePolicy(sp, &backupSchedule.Spec.Schedules[i])
exist = true
continue
}

// for the backup methods that are not specified in the cluster backup,
// we need to disable them.

if !boolptr.IsSetToTrue(s.Enabled) {
continue
}

// if PITR is not enabled, disable the backup schedule.
if !boolptr.IsSetToTrue(backup.PITREnabled) {
backupSchedule.Spec.Schedules[i].Enabled = boolptr.False()
m := dputils.GetBackupMethodByName(s.BackupMethod, backupPolicy)
if m == nil {
continue
}

// if PITR is enabled, we should check and disable the backup schedule if
// the backup type is not Continuous. The Continuous backup schedule is
// reconciled by the enterprise edition operator.
m := dputils.GetBackupMethodByName(s.BackupMethod, backupPolicy)
if m == nil || m.ActionSetName == "" {
if m.ActionSetName == "" {
if boolptr.IsSetToTrue(m.SnapshotVolumes) && enableAutoBackup {
// disable the automatic backup when the specified method is not a volume snapshot for volume-snapshot method
backupSchedule.Spec.Schedules[i].Enabled = boolptr.False()
}
continue
}

Expand All @@ -639,7 +632,13 @@ func (r *clusterBackupPolicyTransformer) mergeClusterBackup(
r.Error(err, "failed to get ActionSet for backup.", "ActionSet", as.Name)
continue
}
if as.Spec.BackupType != dpv1alpha1.BackupTypeContinuous {
if as.Spec.BackupType == dpv1alpha1.BackupTypeContinuous && backup.PITREnabled != nil && !hasSyncPITRMethod {
// auto-sync the first continuous backup for the 'pirtEnable' option.
backupSchedule.Spec.Schedules[i].Enabled = backup.PITREnabled
hasSyncPITRMethod = true
}
if as.Spec.BackupType == dpv1alpha1.BackupTypeFull && enableAutoBackup {
// disable the automatic backup for other full backup method
backupSchedule.Spec.Schedules[i].Enabled = boolptr.False()
}
}
Expand Down
1 change: 0 additions & 1 deletion pkg/testutil/dataprotection/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ const (
VSBackupMethodName = "volume-snapshot"
BackupPathPrefix = "/backup"
ActionSetName = "xtrabackup"
VSActionSetName = "volume-snapshot"

DataVolumeName = "data"
DataVolumeMountPath = "/data"
Expand Down
25 changes: 0 additions & 25 deletions test/testdata/backup/pitr_backuptool.yaml

This file was deleted.

0 comments on commit 7897fea

Please sign in to comment.