@@ -505,11 +505,8 @@ func (v *VSHandler) PreparePVC(pvcName string, prepFinalSync, copyMethodDirect b
505
505
return nil
506
506
}
507
507
508
- // This doesn't need to specifically be in VSHandler - could be useful for non-volsync scenarios?
509
- // Will look at annotations on the PVC, make sure the reconcile option from ACM is set to merge (or not exists)
510
- // and then will remove ACM annotations and also add VRG as the owner. This is to break the connection between
511
- // the appsub and the PVC itself. This way we can proceed to remove the app without the PVC being removed.
512
- // We need the PVC left behind for running the final sync or for CopyMethod Direct.
508
+ // TakePVCOwnership adds do-not-delete annotation to indicate that ACM should not delete/cleanup this pvc
509
+ // when the appsub is removed and adds VRG as owner so the PVC is garbage collected when the VRG is deleted.
513
510
func (v * VSHandler ) TakePVCOwnership (pvcName string ) (bool , error ) {
514
511
l := v .log .WithValues ("pvcName" , pvcName )
515
512
@@ -521,22 +518,6 @@ func (v *VSHandler) TakePVCOwnership(pvcName string) (bool, error) {
521
518
return false , err
522
519
}
523
520
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
521
err = v .client .Update (v .ctx , pvc )
541
522
if err != nil {
542
523
l .Error (err , "Error updating annotations on PVC to break appsub ownership" )
@@ -578,23 +559,6 @@ func (v *VSHandler) pvcExistsAndInUse(pvcName string, inUsePodMustBeReady bool)
578
559
return util .IsPVAttachedToNode (v .ctx , v .client , v .log , pvc )
579
560
}
580
561
581
- func (v * VSHandler ) pvcExists (pvcName string ) (bool , error ) {
582
- _ , err := v .getPVC (pvcName )
583
- if err != nil {
584
- if ! kerrors .IsNotFound (err ) {
585
- v .log .V (1 ).Info ("failed to get PVC" )
586
-
587
- return false , err
588
- }
589
-
590
- return false , nil
591
- }
592
-
593
- v .log .V (1 ).Info ("PVC found" )
594
-
595
- return true , nil
596
- }
597
-
598
562
func (v * VSHandler ) getPVC (pvcName string ) (* corev1.PersistentVolumeClaim , error ) {
599
563
pvc := & corev1.PersistentVolumeClaim {}
600
564
@@ -604,7 +568,7 @@ func (v *VSHandler) getPVC(pvcName string) (*corev1.PersistentVolumeClaim, error
604
568
Namespace : v .owner .GetNamespace (),
605
569
}, pvc )
606
570
if err != nil {
607
- return pvc , fmt .Errorf ("%w" , err )
571
+ return nil , fmt .Errorf ("%w" , err )
608
572
}
609
573
610
574
return pvc , nil
@@ -933,6 +897,7 @@ func (v *VSHandler) EnsurePVCfromRD(rdSpec ramendrv1alpha1.VolSyncReplicationDes
933
897
return v .validateSnapshotAndEnsurePVC (rdSpec , * vsImageRef , failoverAction )
934
898
}
935
899
900
+ //nolint:cyclop
936
901
func (v * VSHandler ) EnsurePVCforDirectCopy (ctx context.Context ,
937
902
rdSpec ramendrv1alpha1.VolSyncReplicationDestinationSpec ,
938
903
) error {
@@ -946,16 +911,16 @@ func (v *VSHandler) EnsurePVCforDirectCopy(ctx context.Context,
946
911
return fmt .Errorf ("capacity must be provided %v" , rdSpec .ProtectedPVC )
947
912
}
948
913
949
- exists , err := v .pvcExists (rdSpec .ProtectedPVC .Name )
950
- if err != nil {
914
+ pvc , err := v .getPVC (rdSpec .ProtectedPVC .Name )
915
+ if err != nil && ! kerrors . IsNotFound ( err ) {
951
916
return err
952
917
}
953
918
954
- if exists {
955
- return nil
919
+ if pvc != nil {
920
+ return v . removeOCMAnnotationsAndUpdate ( pvc )
956
921
}
957
922
958
- pvc : = & corev1.PersistentVolumeClaim {
923
+ pvc = & corev1.PersistentVolumeClaim {
959
924
ObjectMeta : metav1.ObjectMeta {
960
925
Name : rdSpec .ProtectedPVC .Name ,
961
926
Namespace : v .owner .GetNamespace (),
@@ -1043,6 +1008,17 @@ func (v *VSHandler) validateSnapshotAndEnsurePVC(rdSpec ramendrv1alpha1.VolSyncR
1043
1008
}
1044
1009
}
1045
1010
1011
+ pvc , err := v .getPVC (rdSpec .ProtectedPVC .Name )
1012
+ if err != nil {
1013
+ return err
1014
+ }
1015
+
1016
+ // Once the PVC is restored/rolled back, need to re-add the annotations from old Primary
1017
+ err = v .addBackOCMAnnotationsAndUpdate (pvc , rdSpec .ProtectedPVC .Annotations )
1018
+ if err != nil {
1019
+ return err
1020
+ }
1021
+
1046
1022
// Add ownerRef on snapshot pointing to the vrg - if/when the VRG gets cleaned up, then GC can cleanup the snap
1047
1023
return v .addOwnerReferenceAndUpdate (snap , v .owner )
1048
1024
}
@@ -2110,3 +2086,40 @@ func (v *VSHandler) checkLastSnapshotSyncStatus(lrs *volsyncv1alpha1.Replication
2110
2086
2111
2087
return ! completed
2112
2088
}
2089
+
2090
+ func (v * VSHandler ) DisownVolSyncManagedPVC (pvc * corev1.PersistentVolumeClaim ) error {
2091
+ // TODO: Remove just the VRG ownerReference instead of blindly removing all ownerreferences.
2092
+ // For now, this is fine, given that the VRG is the sole owner of the PVC after DR is enabled.
2093
+ pvc .ObjectMeta .OwnerReferences = nil
2094
+ delete (pvc .Annotations , ACMAppSubDoNotDeleteAnnotation )
2095
+
2096
+ return v .client .Update (v .ctx , pvc )
2097
+ }
2098
+
2099
+ func (v * VSHandler ) addBackOCMAnnotationsAndUpdate (obj client.Object , annotations map [string ]string ) error {
2100
+ updatedAnnotations := obj .GetAnnotations ()
2101
+
2102
+ for key , val := range annotations {
2103
+ if strings .HasPrefix (key , "apps.open-cluster-management.io" ) {
2104
+ updatedAnnotations [key ] = val
2105
+ }
2106
+ }
2107
+
2108
+ obj .SetAnnotations (updatedAnnotations )
2109
+
2110
+ return v .client .Update (v .ctx , obj )
2111
+ }
2112
+
2113
+ func (v * VSHandler ) removeOCMAnnotationsAndUpdate (obj client.Object ) error {
2114
+ updatedAnnotations := map [string ]string {}
2115
+
2116
+ for key , val := range obj .GetAnnotations () {
2117
+ if ! strings .HasPrefix (key , "apps.open-cluster-management.io" ) {
2118
+ updatedAnnotations [key ] = val
2119
+ }
2120
+ }
2121
+
2122
+ obj .SetAnnotations (updatedAnnotations )
2123
+
2124
+ return v .client .Update (v .ctx , obj )
2125
+ }
0 commit comments