@@ -19,6 +19,7 @@ package cnsunregistervolume
19
19
import (
20
20
"context"
21
21
"fmt"
22
+ "strings"
22
23
"sync"
23
24
"time"
24
25
@@ -55,7 +56,6 @@ import (
55
56
56
57
const (
57
58
defaultMaxWorkerThreadsForUnregisterVolume = 40
58
- metadata = "VOLUME_METADATA"
59
59
)
60
60
61
61
var (
@@ -211,57 +211,22 @@ func (r *ReconcileCnsUnregisterVolume) Reconcile(ctx context.Context,
211
211
// 5. Invoke CNS DeleteVolume API with deleteDisk set to false.
212
212
// 6. Set the CnsUnregisterVolumeStatus.Unregistered to true.
213
213
214
- // TODO - Add validations whether the volume is not in use in a TKC or by a VM service VM.
215
-
216
- queryFilter := cnstypes.CnsQueryFilter {
217
- VolumeIds : []cnstypes.CnsVolumeId {{Id : instance .Spec .VolumeID }},
218
- }
219
- querySelection := cnstypes.CnsQuerySelection {
220
- Names : []string {
221
- metadata ,
222
- },
223
- }
224
-
225
- queryResult , err := r .volumeManager .QueryVolumeAsync (ctx , queryFilter , & querySelection )
226
- if err != nil {
227
- msg := fmt .Sprintf ("Unable to query volume %q . Error: %+v" , instance .Spec .VolumeID , err )
228
- log .Error (msg )
229
- setInstanceError (ctx , r , instance , msg )
230
- return reconcile.Result {RequeueAfter : timeout }, nil
231
- }
232
- if len (queryResult .Volumes ) == 0 {
233
- msg := fmt .Sprintf ("Volume: %q not found while querying CNS. It may have already been unregistered." ,
234
- instance .Spec .VolumeID )
235
- err = setInstanceSuccess (ctx , r , instance , msg )
236
- if err != nil {
237
- msg := fmt .Sprintf ("Failed to update CnsUnregistered instance with error: %+v" , err )
238
- log .Error (msg )
239
- setInstanceError (ctx , r , instance , msg )
240
- return reconcile.Result {RequeueAfter : timeout }, nil
241
- }
242
- backOffDurationMapMutex .Lock ()
243
- delete (backOffDuration , instance .Name )
244
- backOffDurationMapMutex .Unlock ()
245
- log .Info (msg )
246
- return reconcile.Result {}, nil
247
- }
248
-
249
- cnsVol := queryResult .Volumes [0 ]
250
-
251
214
var pvName , pvcName , pvcNamespace string
252
- for _ , entity := range cnsVol . Metadata . EntityMetadata {
253
- if k8sEntityMetadata , ok := entity .( * cnstypes. CnsKubernetesEntityMetadata ); ok {
254
- entityType := k8sEntityMetadata . EntityType
255
-
256
- if entityType == string ( cnstypes . CnsKubernetesEntityTypePV ) {
257
- pvName = entity .( * cnstypes. CnsKubernetesEntityMetadata ). EntityName
258
- }
259
-
260
- if entityType == string ( cnstypes . CnsKubernetesEntityTypePVC ) {
261
- pvcName = entity .( * cnstypes. CnsKubernetesEntityMetadata ). EntityName
262
- pvcNamespace = entity .( * cnstypes. CnsKubernetesEntityMetadata ). Namespace
263
- }
215
+ pvName , pvfound := commonco . ContainerOrchestratorUtility . GetPVNameFromCSIVolumeID ( instance . Spec . VolumeID )
216
+ if pvfound {
217
+ log . Infof ( "found PV: %q for the volumd Id: %q" , pvName , instance . Spec . VolumeID )
218
+ pvcNamewithNamespace , pvcfound := commonco . ContainerOrchestratorUtility . GetPVCNameFromCSIVolumeID ( instance . Spec . VolumeID )
219
+ if pvcfound {
220
+ parts := strings . Split ( pvcNamewithNamespace , "/" )
221
+ pvcNamespace = parts [ 0 ]
222
+ pvcName = parts [ 1 ]
223
+ log . Infof ( "found PVC: %q in the namespace:%q for the volumd Id: %q" , pvcName , pvcNamespace ,
224
+ instance . Spec . VolumeID )
225
+ } else {
226
+ log . Infof ( "cound not find PVC for the volume Id: %q" , instance . Spec . VolumeID )
264
227
}
228
+ } else {
229
+ log .Infof ("cound not find PV for the volume Id: %q" , instance .Spec .VolumeID )
265
230
}
266
231
267
232
k8sclient , err := k8s .NewClient (ctx )
@@ -271,8 +236,9 @@ func (r *ReconcileCnsUnregisterVolume) Reconcile(ctx context.Context,
271
236
setInstanceError (ctx , r , instance , "Failed to init K8S client for volume unregistration" )
272
237
return reconcile.Result {RequeueAfter : timeout }, nil
273
238
}
274
-
275
- err = validateVolumeNotInUse (ctx , cnsVol , pvcName , pvcNamespace , k8sclient )
239
+ // TODO - Add validations whether the volume is not in use in a TKC
240
+ // validateVolumeNotInUse does not check if detached PVC/PV in TKC, having reference to supervisor PVC/PV
241
+ err = validateVolumeNotInUse (ctx , instance .Spec .VolumeID , pvcName , pvcNamespace , k8sclient )
276
242
if err != nil {
277
243
log .Error (err )
278
244
setInstanceError (ctx , r , instance , err .Error ())
@@ -301,9 +267,6 @@ func (r *ReconcileCnsUnregisterVolume) Reconcile(ctx context.Context,
301
267
}
302
268
log .Infof ("Updated ReclaimPolicy on PV %q to %q" , pvName , v1 .PersistentVolumeReclaimRetain )
303
269
}
304
- } else {
305
- log .Infof ("CNS metadata for volume %s has missing pvName." +
306
- "PV may have already been deleted. Continuing with other operations.." , instance .Spec .VolumeID )
307
270
}
308
271
309
272
// Delete PVC.
@@ -322,9 +285,6 @@ func (r *ReconcileCnsUnregisterVolume) Reconcile(ctx context.Context,
322
285
} else {
323
286
log .Infof ("Deleted PVC %q in namespace %q" , pvcName , pvcNamespace )
324
287
}
325
- } else {
326
- log .Infof ("CNS metadata for volume %s has missing pvcName or namespace." +
327
- "PVC may have already been deleted. Continuing with other operations.." , instance .Spec .VolumeID )
328
288
}
329
289
330
290
if pvName != "" {
@@ -377,7 +337,7 @@ func (r *ReconcileCnsUnregisterVolume) Reconcile(ctx context.Context,
377
337
378
338
// validateVolumeNotInUse validates whether the volume to be unregistered is not in use by
379
339
// either PodVM, TKG cluster or Volume service VM.
380
- func validateVolumeNotInUse (ctx context.Context , cnsVol cnstypes. CnsVolume , pvcName string ,
340
+ func validateVolumeNotInUse (ctx context.Context , volumdId string , pvcName string ,
381
341
pvcNamespace string , k8sClient clientset.Interface ) error {
382
342
383
343
log := logger .GetLogger (ctx )
@@ -394,10 +354,10 @@ func validateVolumeNotInUse(ctx context.Context, cnsVol cnstypes.CnsVolume, pvcN
394
354
for _ , podVol := range pod .Spec .Volumes {
395
355
if podVol .PersistentVolumeClaim != nil &&
396
356
podVol .PersistentVolumeClaim .ClaimName == pvcName {
397
- log .Debugf ("Volume %s is in use by pod %s in namespace %s" , cnsVol . VolumeId . Id ,
357
+ log .Debugf ("Volume %s is in use by pod %s in namespace %s" , volumdId ,
398
358
pod .Name , pvcNamespace )
399
359
return fmt .Errorf ("cannot unregister the volume %s as it's in use by pod %s in namespace %s" ,
400
- cnsVol . VolumeId . Id , pod .Name , pvcNamespace )
360
+ volumdId , pod .Name , pvcNamespace )
401
361
}
402
362
}
403
363
}
@@ -406,14 +366,6 @@ func validateVolumeNotInUse(ctx context.Context, cnsVol cnstypes.CnsVolume, pvcN
406
366
// For volumes created from TKGs Cluster, CNS metadata will have two entries for containerClusterArray.
407
367
// One for clusterFlavor: "WORKLOAD" & clusterDistribution "SupervisorCluster",
408
368
// another for clusterFlavor: "GUEST_CLUSTER" & clusterDistribution: "TKGService".
409
- for _ , containerCluster := range cnsVol .Metadata .ContainerClusterArray {
410
- if containerCluster .ClusterFlavor == "GUEST_CLUSTER" {
411
- log .Debugf ("Volume %s is in use by guest cluster with CNS clusterId %s" , cnsVol .VolumeId .Id ,
412
- containerCluster .ClusterId )
413
- return fmt .Errorf ("cannot unregister the volume %s as it's in use by guest cluster with CNS clusterId %s" ,
414
- cnsVol .VolumeId .Id , containerCluster .ClusterId )
415
- }
416
- }
417
369
418
370
// Check if the Supervisor volume is not used by a volume service VM.
419
371
// If the volume is specified in the VirtualMachine's spec, then it intends
@@ -444,10 +396,10 @@ func validateVolumeNotInUse(ctx context.Context, cnsVol cnstypes.CnsVolume, pvcN
444
396
for _ , vmVol := range vmInstance .Spec .Volumes {
445
397
if vmVol .PersistentVolumeClaim != nil &&
446
398
vmVol .PersistentVolumeClaim .ClaimName == pvcName {
447
- log .Debugf ("Volume %s is in use by VirtualMachine %s in namespace %s" , cnsVol . VolumeId . Id ,
399
+ log .Debugf ("Volume %s is in use by VirtualMachine %s in namespace %s" , volumdId ,
448
400
vmInstance .Name , pvcNamespace )
449
401
return fmt .Errorf ("cannot unregister the volume %s as it's in use by VirtualMachine %s in namespace %s" ,
450
- cnsVol . VolumeId . Id , vmInstance .Name , pvcNamespace )
402
+ volumdId , vmInstance .Name , pvcNamespace )
451
403
}
452
404
}
453
405
}
0 commit comments