Skip to content

Commit

Permalink
fix: two pods are always pending after restore the mysql (#5842)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyelei authored Nov 15, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 1cf0c3b commit 9fb4d8a
Showing 5 changed files with 31 additions and 18 deletions.
8 changes: 4 additions & 4 deletions controllers/dataprotection/restore_controller_test.go
Original file line number Diff line number Diff line change
@@ -168,7 +168,7 @@ var _ = Describe("Restore Controller test", func() {
restore := initResourcesAndWaitRestore(true, false, false, dpv1alpha1.RestorePhaseRunning,
func(f *testdp.MockRestoreFactory) {
f.SetVolumeClaimsTemplate(testdp.MysqlTemplateName, testdp.DataVolumeName,
testdp.DataVolumeMountPath, "", int32(replicas), int32(startingIndex))
testdp.DataVolumeMountPath, "", int32(replicas), int32(startingIndex), nil)
})

By("expect restore jobs and pvcs are created")
@@ -189,7 +189,7 @@ var _ = Describe("Restore Controller test", func() {
restore := initResourcesAndWaitRestore(false, false, true, dpv1alpha1.RestorePhaseRunning,
func(f *testdp.MockRestoreFactory) {
f.SetVolumeClaimsTemplate(testdp.MysqlTemplateName, testdp.DataVolumeName,
testdp.DataVolumeMountPath, "", int32(3), int32(0))
testdp.DataVolumeMountPath, "", int32(3), int32(0), nil)
})
Eventually(testapps.CheckObj(&testCtx, client.ObjectKeyFromObject(restore), func(g Gomega, r *dpv1alpha1.Restore) {
g.Expect(r.Status.Phase).Should(Equal(dpv1alpha1.RestorePhaseFailed))
@@ -201,7 +201,7 @@ var _ = Describe("Restore Controller test", func() {
restore := initResourcesAndWaitRestore(true, false, true, dpv1alpha1.RestorePhaseRunning,
func(f *testdp.MockRestoreFactory) {
f.SetVolumeClaimsTemplate(testdp.MysqlTemplateName, testdp.DataVolumeName,
testdp.DataVolumeMountPath, "", int32(3), int32(0))
testdp.DataVolumeMountPath, "", int32(3), int32(0), nil)
})

By("wait for creating first job and pvc")
@@ -238,7 +238,7 @@ var _ = Describe("Restore Controller test", func() {
restore := initResourcesAndWaitRestore(true, false, true, dpv1alpha1.RestorePhaseRunning,
func(f *testdp.MockRestoreFactory) {
f.SetVolumeClaimsTemplate(testdp.MysqlTemplateName, testdp.DataVolumeName,
testdp.DataVolumeMountPath, "", int32(replicas), int32(startingIndex))
testdp.DataVolumeMountPath, "", int32(replicas), int32(startingIndex), nil)
})

By("wait for creating first job and pvc")
3 changes: 3 additions & 0 deletions pkg/dataprotection/restore/builder.go
Original file line number Diff line number Diff line change
@@ -286,6 +286,9 @@ func (r *restoreJobBuilder) build() *batchv1.Job {
r.specificVolumes = append(r.specificVolumes, r.commonVolumes...)
podSpec.Volumes = r.specificVolumes
job.Spec.Template.Spec = podSpec
job.Spec.Template.ObjectMeta = metav1.ObjectMeta{
Labels: r.labels,
}
job.Spec.BackoffLimit = &defaultBackoffLimit

// 2. set restore container
3 changes: 3 additions & 0 deletions pkg/dataprotection/restore/manager.go
Original file line number Diff line number Diff line change
@@ -332,6 +332,9 @@ func (r *RestoreManager) BuildPrepareDataJobs(reqCtx intctrlutil.RequestCtx, cli
if err != nil {
return nil, err
}
for k, v := range claim.Labels {
jobBuilder.addLabel(k, v)
}
jobBuilder.addToSpecificVolumesAndMounts(volume, volumeMount)
}
}
17 changes: 11 additions & 6 deletions pkg/dataprotection/restore/manager_test.go
Original file line number Diff line number Diff line change
@@ -80,9 +80,10 @@ var _ = Describe("Backup Deleter Test", func() {

Context("with restore manager functions", func() {
var (
actionSet *dpv1alpha1.ActionSet
nodeName = "minikube"
replicas = 2
actionSet *dpv1alpha1.ActionSet
nodeName = "minikube"
replicas = 2
instanceName = "test"
)

BeforeEach(func() {
@@ -182,7 +183,7 @@ var _ = Describe("Backup Deleter Test", func() {
useVolumeSnapshot := true
restoreMGR, backupSet := initResources(reqCtx, startingIndex, useVolumeSnapshot, func(f *testdp.MockRestoreFactory) {
f.SetVolumeClaimsTemplate(testdp.MysqlTemplateName, testdp.DataVolumeName,
testdp.DataVolumeMountPath, "", int32(replicas), int32(startingIndex))
testdp.DataVolumeMountPath, "", int32(replicas), int32(startingIndex), nil)
})

By("test RestorePVCFromSnapshot function")
@@ -196,13 +197,17 @@ var _ = Describe("Backup Deleter Test", func() {
startingIndex := 1
restoreMGR, backupSet := initResources(reqCtx, startingIndex, false, func(f *testdp.MockRestoreFactory) {
f.SetVolumeClaimsTemplate(testdp.MysqlTemplateName, testdp.DataVolumeName,
testdp.DataVolumeMountPath, "", int32(replicas), int32(startingIndex))
testdp.DataVolumeMountPath, "", int32(replicas), int32(startingIndex), map[string]string{
constant.AppInstanceLabelKey: instanceName,
})
})

By(fmt.Sprintf("test BuildPrepareDataJobs function, expect for %d jobs", replicas))
actionSetName := "preparedata-0"
jobs, err := restoreMGR.BuildPrepareDataJobs(reqCtx, k8sClient, *backupSet, actionSetName)
Expect(err).ShouldNot(HaveOccurred())
// job contains the pvc's label
Expect(jobs[0].Spec.Template.Labels[constant.AppInstanceLabelKey]).Should(Equal(instanceName))
Expect(len(jobs)).Should(Equal(replicas))
// image should be expanded by env
Expect(jobs[0].Spec.Template.Spec.Containers[0].Image).Should(ContainSubstring(testdp.ImageTag))
@@ -215,7 +220,7 @@ var _ = Describe("Backup Deleter Test", func() {
startingIndex := 1
restoreMGR, backupSet := initResources(reqCtx, startingIndex, false, func(f *testdp.MockRestoreFactory) {
f.SetVolumeClaimsTemplate(testdp.MysqlTemplateName, testdp.DataVolumeName,
testdp.DataVolumeMountPath, "", int32(replicas), int32(startingIndex)).
testdp.DataVolumeMountPath, "", int32(replicas), int32(startingIndex), nil).
SetVolumeClaimRestorePolicy(dpv1alpha1.VolumeClaimRestorePolicySerial)
})

18 changes: 10 additions & 8 deletions pkg/testutil/dataprotection/restore_factory.go
Original file line number Diff line number Diff line change
@@ -81,13 +81,15 @@ func (f *MockRestoreFactory) initReadyConfig() {
}
}

func (f *MockRestoreFactory) buildRestoreVolumeClaim(name, volumeSource, mountPath, storageClass string) dpv1alpha1.RestoreVolumeClaim {
func (f *MockRestoreFactory) buildRestoreVolumeClaim(name, volumeSource, mountPath, storageClass string, labels map[string]string) dpv1alpha1.RestoreVolumeClaim {
if labels == nil {
labels = map[string]string{}
}
labels[constant.AppManagedByLabelKey] = "restore"
return dpv1alpha1.RestoreVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Labels: map[string]string{
constant.AppManagedByLabelKey: "restore",
},
Name: name,
Labels: labels,
},
VolumeConfig: dpv1alpha1.VolumeConfig{
VolumeSource: volumeSource,
@@ -126,13 +128,13 @@ func (f *MockRestoreFactory) SetDataSourceRef(volumeSource, mountPath string) *M
return f
}

func (f *MockRestoreFactory) SetVolumeClaimsTemplate(templateName, volumeSource, mountPath, storageClass string, replicas, startingIndex int32) *MockRestoreFactory {
func (f *MockRestoreFactory) SetVolumeClaimsTemplate(templateName, volumeSource, mountPath, storageClass string, replicas, startingIndex int32, labels map[string]string) *MockRestoreFactory {
f.initPrepareDataConfig()
f.Get().Spec.PrepareDataConfig.RestoreVolumeClaimsTemplate = &dpv1alpha1.RestoreVolumeClaimsTemplate{
Replicas: replicas,
StartingIndex: startingIndex,
Templates: []dpv1alpha1.RestoreVolumeClaim{
f.buildRestoreVolumeClaim(templateName, volumeSource, mountPath, storageClass),
f.buildRestoreVolumeClaim(templateName, volumeSource, mountPath, storageClass, labels),
},
}
return f
@@ -141,7 +143,7 @@ func (f *MockRestoreFactory) SetVolumeClaimsTemplate(templateName, volumeSource,
func (f *MockRestoreFactory) AddVolumeClaim(claimName, volumeSource, mountPath, storageClass string) *MockRestoreFactory {
f.initPrepareDataConfig()
f.Get().Spec.PrepareDataConfig.RestoreVolumeClaims = append(f.Get().Spec.PrepareDataConfig.RestoreVolumeClaims,
f.buildRestoreVolumeClaim(claimName, volumeSource, mountPath, storageClass))
f.buildRestoreVolumeClaim(claimName, volumeSource, mountPath, storageClass, nil))
return f
}

0 comments on commit 9fb4d8a

Please sign in to comment.