@@ -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,23 @@ 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 .
219
+ GetPVCNameFromCSIVolumeID (instance .Spec .VolumeID )
220
+ if pvcfound {
221
+ parts := strings .Split (pvcNamewithNamespace , "/" )
222
+ pvcNamespace = parts [0 ]
223
+ pvcName = parts [1 ]
224
+ log .Infof ("found PVC: %q in the namespace:%q for the volumd Id: %q" , pvcName , pvcNamespace ,
225
+ instance .Spec .VolumeID )
226
+ } else {
227
+ log .Infof ("cound not find PVC for the volume Id: %q" , instance .Spec .VolumeID )
264
228
}
229
+ } else {
230
+ log .Infof ("cound not find PV for the volume Id: %q" , instance .Spec .VolumeID )
265
231
}
266
232
267
233
k8sclient , err := k8s .NewClient (ctx )
@@ -271,8 +237,9 @@ func (r *ReconcileCnsUnregisterVolume) Reconcile(ctx context.Context,
271
237
setInstanceError (ctx , r , instance , "Failed to init K8S client for volume unregistration" )
272
238
return reconcile.Result {RequeueAfter : timeout }, nil
273
239
}
274
-
275
- err = validateVolumeNotInUse (ctx , cnsVol , pvcName , pvcNamespace , k8sclient )
240
+ // TODO - Add validations whether the volume is not in use in a TKC
241
+ // validateVolumeNotInUse does not check if detached PVC/PV in TKC, having reference to supervisor PVC/PV
242
+ err = validateVolumeNotInUse (ctx , instance .Spec .VolumeID , pvcName , pvcNamespace , k8sclient )
276
243
if err != nil {
277
244
log .Error (err )
278
245
setInstanceError (ctx , r , instance , err .Error ())
@@ -301,9 +268,6 @@ func (r *ReconcileCnsUnregisterVolume) Reconcile(ctx context.Context,
301
268
}
302
269
log .Infof ("Updated ReclaimPolicy on PV %q to %q" , pvName , v1 .PersistentVolumeReclaimRetain )
303
270
}
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
271
}
308
272
309
273
// Delete PVC.
@@ -322,9 +286,6 @@ func (r *ReconcileCnsUnregisterVolume) Reconcile(ctx context.Context,
322
286
} else {
323
287
log .Infof ("Deleted PVC %q in namespace %q" , pvcName , pvcNamespace )
324
288
}
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
289
}
329
290
330
291
if pvName != "" {
@@ -377,7 +338,7 @@ func (r *ReconcileCnsUnregisterVolume) Reconcile(ctx context.Context,
377
338
378
339
// validateVolumeNotInUse validates whether the volume to be unregistered is not in use by
379
340
// either PodVM, TKG cluster or Volume service VM.
380
- func validateVolumeNotInUse (ctx context.Context , cnsVol cnstypes. CnsVolume , pvcName string ,
341
+ func validateVolumeNotInUse (ctx context.Context , volumdId string , pvcName string ,
381
342
pvcNamespace string , k8sClient clientset.Interface ) error {
382
343
383
344
log := logger .GetLogger (ctx )
@@ -394,10 +355,10 @@ func validateVolumeNotInUse(ctx context.Context, cnsVol cnstypes.CnsVolume, pvcN
394
355
for _ , podVol := range pod .Spec .Volumes {
395
356
if podVol .PersistentVolumeClaim != nil &&
396
357
podVol .PersistentVolumeClaim .ClaimName == pvcName {
397
- log .Debugf ("Volume %s is in use by pod %s in namespace %s" , cnsVol . VolumeId . Id ,
358
+ log .Debugf ("Volume %s is in use by pod %s in namespace %s" , volumdId ,
398
359
pod .Name , pvcNamespace )
399
360
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 )
361
+ volumdId , pod .Name , pvcNamespace )
401
362
}
402
363
}
403
364
}
@@ -406,14 +367,6 @@ func validateVolumeNotInUse(ctx context.Context, cnsVol cnstypes.CnsVolume, pvcN
406
367
// For volumes created from TKGs Cluster, CNS metadata will have two entries for containerClusterArray.
407
368
// One for clusterFlavor: "WORKLOAD" & clusterDistribution "SupervisorCluster",
408
369
// 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
370
418
371
// Check if the Supervisor volume is not used by a volume service VM.
419
372
// If the volume is specified in the VirtualMachine's spec, then it intends
@@ -444,10 +397,10 @@ func validateVolumeNotInUse(ctx context.Context, cnsVol cnstypes.CnsVolume, pvcN
444
397
for _ , vmVol := range vmInstance .Spec .Volumes {
445
398
if vmVol .PersistentVolumeClaim != nil &&
446
399
vmVol .PersistentVolumeClaim .ClaimName == pvcName {
447
- log .Debugf ("Volume %s is in use by VirtualMachine %s in namespace %s" , cnsVol . VolumeId . Id ,
400
+ log .Debugf ("Volume %s is in use by VirtualMachine %s in namespace %s" , volumdId ,
448
401
vmInstance .Name , pvcNamespace )
449
402
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 )
403
+ volumdId , vmInstance .Name , pvcNamespace )
451
404
}
452
405
}
453
406
}
0 commit comments