@@ -339,7 +339,7 @@ func (r *ReconcileCnsUnregisterVolume) Reconcile(ctx context.Context,
339339
340340// validateVolumeNotInUse validates whether the volume to be unregistered is not in use by
341341// either PodVM, TKG cluster or Volume service VM.
342- func validateVolumeNotInUse (ctx context.Context , volumdId string , pvcName string ,
342+ func validateVolumeNotInUse (ctx context.Context , volumeID string , pvcName string ,
343343 pvcNamespace string , k8sClient clientset.Interface ) error {
344344
345345 log := logger .GetLogger (ctx )
@@ -356,22 +356,14 @@ func validateVolumeNotInUse(ctx context.Context, volumdId string, pvcName string
356356 for _ , podVol := range pod .Spec .Volumes {
357357 if podVol .PersistentVolumeClaim != nil &&
358358 podVol .PersistentVolumeClaim .ClaimName == pvcName {
359- log .Debugf ("Volume %s is in use by pod %s in namespace %s" , volumdId ,
359+ log .Debugf ("Volume %s is in use by pod %s in namespace %s" , volumeID ,
360360 pod .Name , pvcNamespace )
361361 return fmt .Errorf ("cannot unregister the volume %s as it's in use by pod %s in namespace %s" ,
362- volumdId , pod .Name , pvcNamespace )
362+ volumeID , pod .Name , pvcNamespace )
363363 }
364364 }
365365 }
366366
367- // Check if the Supervisor volume is not used in any TKGs cluster.
368- // For volumes created from TKGs Cluster, CNS metadata will have two entries for containerClusterArray.
369- // One for clusterFlavor: "WORKLOAD" & clusterDistribution "SupervisorCluster",
370- // another for clusterFlavor: "GUEST_CLUSTER" & clusterDistribution: "TKGService".
371-
372- // Check if the Supervisor volume is not used by a volume service VM.
373- // If the volume is specified in the VirtualMachine's spec, then it intends
374- // to be attached to the VM. We will check for the presence of volume in VM's spec.
375367 restClientConfig , err := k8s .GetKubeConfig (ctx )
376368 if err != nil {
377369 msg := fmt .Sprintf ("Failed to initialize rest clientconfig. Error: %+v" , err )
@@ -393,17 +385,24 @@ func validateVolumeNotInUse(ctx context.Context, volumdId string, pvcName string
393385 return err
394386 }
395387
388+ log .Debugf ("Found %d VirtualMachines in namespace %s" , len (vmList .Items ), pvcNamespace )
396389 for _ , vmInstance := range vmList .Items {
390+ log .Debugf ("Checking if volume %s is in use by VirtualMachine %s in namespace %s" ,
391+ volumeID , vmInstance .Name , pvcNamespace )
397392 for _ , vmVol := range vmInstance .Spec .Volumes {
398393 if vmVol .PersistentVolumeClaim != nil &&
399394 vmVol .PersistentVolumeClaim .ClaimName == pvcName {
400- log .Debugf ("Volume %s is in use by VirtualMachine %s in namespace %s" , volumdId ,
395+ // If the volume is specified in the VirtualMachine's spec, then it is
396+ // either, in the process of being attached to the VM or is already attached.
397+ // In either case, we cannot unregister the volume.
398+ log .Debugf ("Volume %s is in use by VirtualMachine %s in namespace %s" , volumeID ,
401399 vmInstance .Name , pvcNamespace )
402400 return fmt .Errorf ("cannot unregister the volume %s as it's in use by VirtualMachine %s in namespace %s" ,
403- volumdId , vmInstance .Name , pvcNamespace )
401+ volumeID , vmInstance .Name , pvcNamespace )
404402 }
405403 }
406404 }
405+
407406 return nil
408407}
409408
0 commit comments