Skip to content

Commit 0e1bbdb

Browse files
authored
fix(operations): preserve rebuild helper PV identity when reverting reclaim policy (#10235)
1 parent 2f16cbe commit 0e1bbdb

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
@@ -677,13 +677,12 @@ func (inPlaceHelper *inplaceRebuildHelper) revertReclaimPolicy(reqCtx intctrluti
677677
}
678678
pv.Spec.PersistentVolumeReclaimPolicy = corev1.PersistentVolumeReclaimPolicy(reclaimPolicy)
679679
}
680-
if pv.Annotations != nil {
681-
delete(pv.Annotations, rebuildFromAnnotation)
682-
delete(pv.Annotations, sourcePVReclaimPolicyAnnotation)
683-
}
684-
if pv.Labels != nil {
685-
delete(pv.Labels, rebuildTmpPVCNameLabel)
686-
}
680+
// Preserve the rebuild identity metadata (rebuildFromAnnotation,
681+
// sourcePVReclaimPolicyAnnotation, rebuildTmpPVCNameLabel) on the PV
682+
// so that a later re-entry on this OpsRequest can still resolve the
683+
// restored PV via the tmp PVC label. Clearing them eagerly here
684+
// caused getRestoredPV to return a Fatal error when the tmp PVC was
685+
// cleaned up between reconciles.
687686
return cli.Patch(reqCtx.Ctx, pv, patch)
688687
}
689688

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)