@@ -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