@@ -424,33 +424,23 @@ func getVolumeMetadataMaps(ctx context.Context,
424424 return
425425}
426426
427- // getPvcsInSpec returns map of PVCs and their volumeIDs .
428- func getPvcsInSpec (ctx context.Context , instance * v1alpha1. CnsNodeVMBatchAttachment ,
429- k8sClient kubernetes. Interface ) ( map [string ]string , error ) {
427+ // getAllPvcsAttachedToVM returns PVC name to volumeID map for all the FCDs which are attached to the VM .
428+ func getAllPvcsAttachedToVM (ctx context.Context ,
429+ attachedFCDList map [ string ] FCDBackingDetails ) map [string ]string {
430430 log := logger .GetLogger (ctx )
431+ pvcsAttachedToVM := make (map [string ]string )
431432
432- pvcsInSpec := make (map [string ]string )
433- for _ , volume := range instance .Spec .Volumes {
434- volumeId , ok := commonco .ContainerOrchestratorUtility .GetVolumeIDFromPVCName (
435- instance .Namespace , volume .PersistentVolumeClaim .ClaimName )
436- if ! ok {
437- pvcName := volume .PersistentVolumeClaim .ClaimName
438- _ , err := k8sClient .CoreV1 ().PersistentVolumeClaims (instance .Namespace ).Get (ctx ,
439- pvcName , metav1.GetOptions {})
440- if err != nil {
441- if apierrors .IsNotFound (err ) {
442- log .Infof ("PVC %s has already been deleted. No action to be taken" , pvcName )
443- continue
444- }
445- return pvcsInSpec , fmt .Errorf ("failed to find volumeID for PVC %s" , volume .PersistentVolumeClaim .ClaimName )
446- }
447- return pvcsInSpec , fmt .Errorf ("failed to find volumeID for PVC %s" , volume .PersistentVolumeClaim .ClaimName )
433+ for volumeID := range attachedFCDList {
434+ pvcName , _ , found := commonco .ContainerOrchestratorUtility .GetPVCNameFromCSIVolumeID (volumeID )
435+ if ! found {
436+ // Do not fail if volumeID is not found to avoid cases where cache is not refreshed.
437+ // Clean up routine in syncer will take care of the cases where finalizer was not removed from such PVCs.
438+ log .Warnf ("failed to find PVC name for volumeID %s" , volumeID )
439+ continue
448440 }
449- pvcsInSpec [ volume . PersistentVolumeClaim . ClaimName ] = volumeId
441+ pvcsAttachedToVM [ pvcName ] = volumeID
450442 }
451-
452- return pvcsInSpec , nil
453-
443+ return pvcsAttachedToVM
454444}
455445
456446// listAttachedFcdsForVM returns list of FCDs (present in the K8s cluster)
@@ -504,7 +494,7 @@ func listAttachedFcdsForVM(ctx context.Context,
504494 log .Debugf ("failed to get diskMode and sharingMode for virtual disk" )
505495 }
506496
507- log .Infof ("Adding volume with ID %s to attachedFCDs list" , virtualDisk .VDiskId .Id )
497+ log .Debugf ("Adding volume with ID %s to attachedFCDs list" , virtualDisk .VDiskId .Id )
508498 attachedFCDs [virtualDisk .VDiskId .Id ] = FCDBackingDetails {
509499 ControllerKey : controllerKey ,
510500 UnitNumber : unitNumber ,
@@ -572,6 +562,21 @@ func constructBatchAttachRequest(ctx context.Context,
572562 return pvcsInSpec , volumeIdsInSpec , batchAttachRequest , nil
573563}
574564
565+ // getPvcsFromSpecAndStatus returns all the PVCs in spec as well as in status of the given instance.
566+ func getPvcsFromSpecAndStatus (ctx context.Context ,
567+ instance * v1alpha1.CnsNodeVMBatchAttachment ) map [string ]string {
568+
569+ listOfPvcsToRemoveFinalzer := make (map [string ]string , 0 )
570+
571+ for _ , volume := range instance .Spec .Volumes {
572+ listOfPvcsToRemoveFinalzer [volume .PersistentVolumeClaim .ClaimName ] = volume .Name
573+ }
574+ for _ , volume := range instance .Status .VolumeStatus {
575+ listOfPvcsToRemoveFinalzer [volume .PersistentVolumeClaim .ClaimName ] = volume .Name
576+ }
577+ return listOfPvcsToRemoveFinalzer
578+ }
579+
575580// isPvcEncrypted returns true if annotation csi.vsphere.encryption-class
576581// is present on the PVC.
577582func isPvcEncrypted (pvcAnnotations map [string ]string ) bool {
@@ -615,17 +620,6 @@ func getVolumesToAttachAndDetach(ctx context.Context, instance *v1alpha1.CnsNode
615620 pvcsToAttach := make (map [string ]string , 0 )
616621 pvcsToDetach := make (map [string ]string , 0 )
617622
618- if instance .DeletionTimestamp != nil {
619- log .Debugf ("Instance %s is being deleted, adding all volumes in spec to volumesToDetach list." , instance .Name )
620- volumesToDetach , err := getPvcsInSpec (ctx , instance , k8sClient )
621- if err != nil {
622- log .Errorf ("failed to get volumes to detach from instance spec. Err: %s" , err )
623- return pvcsToAttach , volumesToDetach , err
624- }
625- log .Debugf ("Volumes to detach list %+v for instance %s" , volumesToDetach , instance .Name )
626- return pvcsToAttach , volumesToDetach , nil
627- }
628-
629623 // Query vCenter to find the list of FCDs which are attached to the VM.
630624 attachedFcdList , err := listAttachedFcdsForVM (ctx , vm )
631625 if err != nil {
@@ -634,6 +628,14 @@ func getVolumesToAttachAndDetach(ctx context.Context, instance *v1alpha1.CnsNode
634628 }
635629 log .Infof ("List of attached FCDs %+v to VM %s" , attachedFcdList , instance .Spec .InstanceUUID )
636630
631+ if instance .DeletionTimestamp != nil {
632+ log .Debugf ("Instance %s is being deleted, adding all volumes attached to the VM to volumesToDetach list." ,
633+ instance .Name )
634+ volumesToDetach := getAllPvcsAttachedToVM (ctx , attachedFcdList )
635+ log .Debugf ("Volumes to detach list %+v for instance %s" , volumesToDetach , instance .Name )
636+ return pvcsToAttach , volumesToDetach , nil
637+ }
638+
637639 // Get all PVCs and their corresponding volumeID mapping from instance spec.
638640 volumeIdsInSpec , volumeNamesInSpec , pvcNameToVolumeIDInSpec , err := getVolumeMetadataMaps (ctx , instance )
639641 if err != nil {
@@ -893,7 +895,7 @@ func removePvcFinalizer(ctx context.Context, client client.Client,
893895 return nil
894896 }
895897
896- log .Infof ("VM %s was the last attached VM for the PVC %s. Finalizer %s can be safely removed fromt the PVC" ,
898+ log .Infof ("VM %s was the last attached VM for the PVC %s. Finalizer %s can be safely removed from the PVC" ,
897899 vmInstanceUUID , pvcName , cnsoperatortypes .CNSPvcFinalizer )
898900
899901 if ! controllerutil .ContainsFinalizer (pvc , cnsoperatortypes .CNSPvcFinalizer ) {
0 commit comments