@@ -454,33 +454,23 @@ func getVolumeMetadataMaps(ctx context.Context,
454454 return
455455}
456456
457- // getPvcsInSpec returns map of PVCs and their volumeIDs .
458- func getPvcsInSpec (ctx context.Context , instance * v1alpha1. CnsNodeVMBatchAttachment ,
459- k8sClient kubernetes. Interface ) ( map [string ]string , error ) {
457+ // getAllPvcsAttachedToVM returns PVC name to volumeID map for all the FCDs which are attached to the VM .
458+ func getAllPvcsAttachedToVM (ctx context.Context ,
459+ attachedFCDList map [ string ] FCDBackingDetails ) map [string ]string {
460460 log := logger .GetLogger (ctx )
461+ pvcsAttachedToVM := make (map [string ]string )
461462
462- pvcsInSpec := make (map [string ]string )
463- for _ , volume := range instance .Spec .Volumes {
464- volumeId , ok := commonco .ContainerOrchestratorUtility .GetVolumeIDFromPVCName (
465- instance .Namespace , volume .PersistentVolumeClaim .ClaimName )
466- if ! ok {
467- pvcName := volume .PersistentVolumeClaim .ClaimName
468- _ , err := k8sClient .CoreV1 ().PersistentVolumeClaims (instance .Namespace ).Get (ctx ,
469- pvcName , metav1.GetOptions {})
470- if err != nil {
471- if apierrors .IsNotFound (err ) {
472- log .Infof ("PVC %s has already been deleted. No action to be taken" , pvcName )
473- continue
474- }
475- return pvcsInSpec , fmt .Errorf ("failed to find volumeID for PVC %s" , volume .PersistentVolumeClaim .ClaimName )
476- }
477- return pvcsInSpec , fmt .Errorf ("failed to find volumeID for PVC %s" , volume .PersistentVolumeClaim .ClaimName )
463+ for volumeID := range attachedFCDList {
464+ pvcName , _ , found := commonco .ContainerOrchestratorUtility .GetPVCNameFromCSIVolumeID (volumeID )
465+ if ! found {
466+ // Do not fail if volumeID is not found to avoid cases where cache is not refreshed.
467+ // Clean up routine in syncer will take care of the cases where finalizer was not removed from such PVCs.
468+ log .Warnf ("failed to find PVC name for volumeID %s" , volumeID )
469+ continue
478470 }
479- pvcsInSpec [ volume . PersistentVolumeClaim . ClaimName ] = volumeId
471+ pvcsAttachedToVM [ pvcName ] = volumeID
480472 }
481-
482- return pvcsInSpec , nil
483-
473+ return pvcsAttachedToVM
484474}
485475
486476// listAttachedFcdsForVM returns list of FCDs (present in the K8s cluster)
@@ -534,7 +524,7 @@ func listAttachedFcdsForVM(ctx context.Context,
534524 log .Debugf ("failed to get diskMode and sharingMode for virtual disk" )
535525 }
536526
537- log .Infof ("Adding volume with ID %s to attachedFCDs list" , virtualDisk .VDiskId .Id )
527+ log .Debugf ("Adding volume with ID %s to attachedFCDs list" , virtualDisk .VDiskId .Id )
538528 attachedFCDs [virtualDisk .VDiskId .Id ] = FCDBackingDetails {
539529 ControllerKey : controllerKey ,
540530 UnitNumber : unitNumber ,
@@ -602,6 +592,21 @@ func constructBatchAttachRequest(ctx context.Context,
602592 return pvcsInSpec , volumeIdsInSpec , batchAttachRequest , nil
603593}
604594
595+ // getPvcsFromSpecAndStatus returns all the PVCs in spec as well as in status of the given instance.
596+ func getPvcsFromSpecAndStatus (ctx context.Context ,
597+ instance * v1alpha1.CnsNodeVMBatchAttachment ) map [string ]bool {
598+
599+ listOfPvcsToRemoveFinalzer := make (map [string ]bool , 0 )
600+
601+ for _ , volume := range instance .Spec .Volumes {
602+ listOfPvcsToRemoveFinalzer [volume .PersistentVolumeClaim .ClaimName ] = true
603+ }
604+ for _ , volume := range instance .Status .VolumeStatus {
605+ listOfPvcsToRemoveFinalzer [volume .PersistentVolumeClaim .ClaimName ] = true
606+ }
607+ return listOfPvcsToRemoveFinalzer
608+ }
609+
605610// isPvcEncrypted returns true if annotation csi.vsphere.encryption-class
606611// is present on the PVC.
607612func isPvcEncrypted (pvcAnnotations map [string ]string ) bool {
@@ -645,17 +650,6 @@ func getVolumesToAttachAndDetach(ctx context.Context, instance *v1alpha1.CnsNode
645650 pvcsToAttach := make (map [string ]string , 0 )
646651 pvcsToDetach := make (map [string ]string , 0 )
647652
648- if instance .DeletionTimestamp != nil {
649- log .Debugf ("Instance %s is being deleted, adding all volumes in spec to volumesToDetach list." , instance .Name )
650- volumesToDetach , err := getPvcsInSpec (ctx , instance , k8sClient )
651- if err != nil {
652- log .Errorf ("failed to get volumes to detach from instance spec. Err: %s" , err )
653- return pvcsToAttach , volumesToDetach , err
654- }
655- log .Debugf ("Volumes to detach list %+v for instance %s" , volumesToDetach , instance .Name )
656- return pvcsToAttach , volumesToDetach , nil
657- }
658-
659653 // Query vCenter to find the list of FCDs which are attached to the VM.
660654 attachedFcdList , err := listAttachedFcdsForVM (ctx , vm )
661655 if err != nil {
@@ -664,6 +658,14 @@ func getVolumesToAttachAndDetach(ctx context.Context, instance *v1alpha1.CnsNode
664658 }
665659 log .Infof ("List of attached FCDs %+v to VM %s" , attachedFcdList , instance .Spec .InstanceUUID )
666660
661+ if instance .DeletionTimestamp != nil {
662+ log .Debugf ("Instance %s is being deleted, adding all volumes attached to the VM to volumesToDetach list." ,
663+ instance .Name )
664+ volumesToDetach := getAllPvcsAttachedToVM (ctx , attachedFcdList )
665+ log .Debugf ("Volumes to detach list %+v for instance %s" , volumesToDetach , instance .Name )
666+ return pvcsToAttach , volumesToDetach , nil
667+ }
668+
667669 // Get all PVCs and their corresponding volumeID mapping from instance spec.
668670 volumeIdsInSpec , volumeNamesInSpec , pvcNameToVolumeIDInSpec , err := getVolumeMetadataMaps (ctx , instance )
669671 if err != nil {
@@ -923,7 +925,7 @@ func removePvcFinalizer(ctx context.Context, client client.Client,
923925 return nil
924926 }
925927
926- log .Infof ("VM %s was the last attached VM for the PVC %s. Finalizer %s can be safely removed fromt the PVC" ,
928+ log .Infof ("VM %s was the last attached VM for the PVC %s. Finalizer %s can be safely removed from the PVC" ,
927929 vmInstanceUUID , pvcName , cnsoperatortypes .CNSPvcFinalizer )
928930
929931 if ! controllerutil .ContainsFinalizer (pvc , cnsoperatortypes .CNSPvcFinalizer ) {
0 commit comments