Skip to content

Commit 43434c9

Browse files
weicaoapecloud-bot
authored andcommitted
fix(operations): preserve rebuild helper PV identity when reverting reclaim policy (#10235)
(cherry picked from commit 0e1bbdb)
1 parent 6a73a71 commit 43434c9

2 files changed

Lines changed: 48 additions & 7 deletions

File tree

pkg/operations/rebuild_instance_inplace.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -675,13 +675,12 @@ func (inPlaceHelper *inplaceRebuildHelper) revertReclaimPolicy(reqCtx intctrluti
675675
}
676676
pv.Spec.PersistentVolumeReclaimPolicy = corev1.PersistentVolumeReclaimPolicy(reclaimPolicy)
677677
}
678-
if pv.Annotations != nil {
679-
delete(pv.Annotations, rebuildFromAnnotation)
680-
delete(pv.Annotations, sourcePVReclaimPolicyAnnotation)
681-
}
682-
if pv.Labels != nil {
683-
delete(pv.Labels, rebuildTmpPVCNameLabel)
684-
}
678+
// Preserve the rebuild identity metadata (rebuildFromAnnotation,
679+
// sourcePVReclaimPolicyAnnotation, rebuildTmpPVCNameLabel) on the PV
680+
// so that a later re-entry on this OpsRequest can still resolve the
681+
// restored PV via the tmp PVC label. Clearing them eagerly here
682+
// caused getRestoredPV to return a Fatal error when the tmp PVC was
683+
// cleaned up between reconciles.
685684
return cli.Patch(reqCtx.Ctx, pv, patch)
686685
}
687686

pkg/operations/rebuild_instance_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,48 @@ var _ = Describe("OpsUtil functions", func() {
495495
Expect(sourcePVC.Spec.VolumeName).Should(ContainSubstring("restored-pv-"))
496496
})
497497

498+
It("preserves rebuild identity metadata when reverting the reclaim policy", func() {
499+
reqCtx := intctrlutil.RequestCtx{Ctx: testCtx.Ctx}
500+
opsRequestName := "rebuild-reclaim-revert-" + testCtx.GetRandomStr()
501+
tmpPVCName := "rebuild-tmp-" + testCtx.GetRandomStr()
502+
pvName := "restored-pv-" + testCtx.GetRandomStr()
503+
pv := testapps.NewPersistentVolumeFactory(testCtx.DefaultNamespace, pvName, tmpPVCName).
504+
SetStorage("20Gi").
505+
Create(&testCtx).
506+
GetObject()
507+
Expect(testapps.ChangeObj(&testCtx, pv, func(p *corev1.PersistentVolume) {
508+
if p.Labels == nil {
509+
p.Labels = map[string]string{}
510+
}
511+
p.Labels[rebuildTmpPVCNameLabel] = tmpPVCName
512+
if p.Annotations == nil {
513+
p.Annotations = map[string]string{}
514+
}
515+
p.Annotations[rebuildFromAnnotation] = opsRequestName
516+
p.Annotations[sourcePVReclaimPolicyAnnotation] = string(corev1.PersistentVolumeReclaimDelete)
517+
p.Spec.PersistentVolumeReclaimPolicy = corev1.PersistentVolumeReclaimRetain
518+
})).Should(Succeed())
519+
Expect(testapps.ChangeObjStatus(&testCtx, pv, func() {
520+
pv.Status.Phase = corev1.VolumeBound
521+
})).Should(Succeed())
522+
523+
helper := &inplaceRebuildHelper{}
524+
Expect(helper.revertReclaimPolicy(reqCtx, k8sClient, pv)).Should(Succeed())
525+
526+
Eventually(testapps.CheckObj(&testCtx, client.ObjectKeyFromObject(pv), func(g Gomega, p *corev1.PersistentVolume) {
527+
// Reclaim policy must be reverted to the value recorded in
528+
// sourcePVReclaimPolicyAnnotation.
529+
g.Expect(p.Spec.PersistentVolumeReclaimPolicy).Should(Equal(corev1.PersistentVolumeReclaimDelete))
530+
// Rebuild identity metadata must be preserved so a later
531+
// re-entry can still resolve the restored PV by its tmp
532+
// PVC label even after the tmp PVC itself has been cleaned
533+
// up.
534+
g.Expect(p.Labels).Should(HaveKeyWithValue(rebuildTmpPVCNameLabel, tmpPVCName))
535+
g.Expect(p.Annotations).Should(HaveKeyWithValue(rebuildFromAnnotation, opsRequestName))
536+
g.Expect(p.Annotations).Should(HaveKeyWithValue(sourcePVReclaimPolicyAnnotation, string(corev1.PersistentVolumeReclaimDelete)))
537+
})).Should(Succeed())
538+
})
539+
498540
testRebuildInstanceWithBackup := func(ignoreRoleCheck bool) {
499541
By("init operation resources and backup")
500542
actionSet := testapps.CreateCustomizedObj(&testCtx, "backup/actionset.yaml",

0 commit comments

Comments
 (0)