@@ -521,22 +521,6 @@ func (v *VSHandler) TakePVCOwnership(pvcName string) (bool, error) {
521
521
return false , err
522
522
}
523
523
524
- // Remove acm annotations from the PVC just as a precaution - ACM uses the annotations to track that the
525
- // pvc is owned by an application. Removing them should not be necessary now that we are adding
526
- // the do-not-delete annotation. With the do-not-delete annotation (see ACMAppSubDoNotDeleteAnnotation), ACM
527
- // should not delete the pvc when the application is removed.
528
- updatedAnnotations := map [string ]string {}
529
-
530
- for currAnnotationKey , currAnnotationValue := range pvc .Annotations {
531
- // We want to only preserve annotations not from ACM (i.e. remove all ACM annotations to break ownership)
532
- if ! strings .HasPrefix (currAnnotationKey , "apps.open-cluster-management.io" ) ||
533
- currAnnotationKey == ACMAppSubDoNotDeleteAnnotation {
534
- updatedAnnotations [currAnnotationKey ] = currAnnotationValue
535
- }
536
- }
537
-
538
- pvc .Annotations = updatedAnnotations
539
-
540
524
err = v .client .Update (v .ctx , pvc )
541
525
if err != nil {
542
526
l .Error (err , "Error updating annotations on PVC to break appsub ownership" )
@@ -578,21 +562,21 @@ func (v *VSHandler) pvcExistsAndInUse(pvcName string, inUsePodMustBeReady bool)
578
562
return util .IsPVAttachedToNode (v .ctx , v .client , v .log , pvc )
579
563
}
580
564
581
- func (v * VSHandler ) pvcExists (pvcName string ) (bool , error ) {
582
- _ , err := v .getPVC (pvcName )
565
+ func (v * VSHandler ) pvcExists (pvcName string ) (bool , * corev1. PersistentVolumeClaim , error ) {
566
+ pvc , err := v .getPVC (pvcName )
583
567
if err != nil {
584
568
if ! kerrors .IsNotFound (err ) {
585
569
v .log .V (1 ).Info ("failed to get PVC" )
586
570
587
- return false , err
571
+ return false , pvc , err
588
572
}
589
573
590
- return false , nil
574
+ return false , pvc , nil
591
575
}
592
576
593
577
v .log .V (1 ).Info ("PVC found" )
594
578
595
- return true , nil
579
+ return true , pvc , nil
596
580
}
597
581
598
582
func (v * VSHandler ) getPVC (pvcName string ) (* corev1.PersistentVolumeClaim , error ) {
@@ -946,16 +930,16 @@ func (v *VSHandler) EnsurePVCforDirectCopy(ctx context.Context,
946
930
return fmt .Errorf ("capacity must be provided %v" , rdSpec .ProtectedPVC )
947
931
}
948
932
949
- exists , err := v .pvcExists (rdSpec .ProtectedPVC .Name )
933
+ exists , pvc , err := v .pvcExists (rdSpec .ProtectedPVC .Name )
950
934
if err != nil {
951
935
return err
952
936
}
953
937
954
938
if exists {
955
- return nil
939
+ return v . removeOCMAnnotationsAndUpdate ( pvc )
956
940
}
957
941
958
- pvc : = & corev1.PersistentVolumeClaim {
942
+ pvc = & corev1.PersistentVolumeClaim {
959
943
ObjectMeta : metav1.ObjectMeta {
960
944
Name : rdSpec .ProtectedPVC .Name ,
961
945
Namespace : v .owner .GetNamespace (),
@@ -1043,6 +1027,17 @@ func (v *VSHandler) validateSnapshotAndEnsurePVC(rdSpec ramendrv1alpha1.VolSyncR
1043
1027
}
1044
1028
}
1045
1029
1030
+ pvc , err := v .getPVC (rdSpec .ProtectedPVC .Name )
1031
+ if err != nil {
1032
+ return err
1033
+ }
1034
+
1035
+ // Once the PVC is restored/rolled back, need to re-add the annotations from old Primary
1036
+ err = v .addBackOCMAnnotationsAndUpdate (pvc , rdSpec .ProtectedPVC .Annotations )
1037
+ if err != nil {
1038
+ return err
1039
+ }
1040
+
1046
1041
// Add ownerRef on snapshot pointing to the vrg - if/when the VRG gets cleaned up, then GC can cleanup the snap
1047
1042
return v .addOwnerReferenceAndUpdate (snap , v .owner )
1048
1043
}
@@ -2110,3 +2105,46 @@ func (v *VSHandler) checkLastSnapshotSyncStatus(lrs *volsyncv1alpha1.Replication
2110
2105
2111
2106
return ! completed
2112
2107
}
2108
+
2109
+ func (v * VSHandler ) DisownVolSyncManagedPVC (pvc * corev1.PersistentVolumeClaim ) error {
2110
+ // TODO: Remove just the VRG ownerReference instead of blindly removing all ownerreferences.
2111
+ // For now, this is fine, given that the VRG is the sole owner of the PVC after DR is enabled.
2112
+ pvc .ObjectMeta .OwnerReferences = nil
2113
+ v .deleteAnnotation (pvc , ACMAppSubDoNotDeleteAnnotation )
2114
+
2115
+ return v .client .Update (v .ctx , pvc )
2116
+ }
2117
+
2118
+ func (v * VSHandler ) deleteAnnotation (obj client.Object , annotationName string ) {
2119
+ annotations := obj .GetAnnotations ()
2120
+ delete (annotations , annotationName )
2121
+ obj .SetAnnotations (annotations )
2122
+ }
2123
+
2124
+ func (v * VSHandler ) addBackOCMAnnotationsAndUpdate (obj client.Object , annotations map [string ]string ) error {
2125
+ updatedAnnotations := obj .GetAnnotations ()
2126
+
2127
+ for key , val := range annotations {
2128
+ if strings .HasPrefix (key , "apps.open-cluster-management.io" ) {
2129
+ updatedAnnotations [key ] = val
2130
+ }
2131
+ }
2132
+
2133
+ obj .SetAnnotations (updatedAnnotations )
2134
+
2135
+ return v .client .Update (v .ctx , obj )
2136
+ }
2137
+
2138
+ func (v * VSHandler ) removeOCMAnnotationsAndUpdate (obj client.Object ) error {
2139
+ updatedAnnotations := map [string ]string {}
2140
+
2141
+ for key , val := range obj .GetAnnotations () {
2142
+ if ! strings .HasPrefix (key , "apps.open-cluster-management.io" ) {
2143
+ updatedAnnotations [key ] = val
2144
+ }
2145
+ }
2146
+
2147
+ obj .SetAnnotations (updatedAnnotations )
2148
+
2149
+ return v .client .Update (v .ctx , obj )
2150
+ }
0 commit comments