@@ -357,12 +357,15 @@ func (r *ReconcileCnsNodeVMAttachment) Reconcile(ctx context.Context,
357
357
log .Infof ("vSphere CSI driver is attaching volume: %q to nodevm: %+v for " +
358
358
"CnsNodeVmAttachment request with name: %q on namespace: %q" ,
359
359
volumeID , nodeVM , request .Name , request .Namespace )
360
- diskUUID , faulttype , attachErr := r .volumeManager .AttachVolume (internalCtx , nodeVM , volumeID , false )
361
-
362
- if attachErr != nil {
360
+ diskUUID , faulttype , err := r .volumeManager .AttachVolume (internalCtx , nodeVM , volumeID , false )
361
+ if err != nil {
363
362
log .Errorf ("failed to attach disk: %q to nodevm: %+v for CnsNodeVmAttachment " +
364
363
"request with name: %q on namespace: %q. Err: %+v" ,
365
- volumeID , nodeVM , request .Name , request .Namespace , attachErr )
364
+ volumeID , nodeVM , request .Name , request .Namespace , err )
365
+ instance .Status .Error = err .Error ()
366
+ _ = k8s .UpdateStatus (internalCtx , r .client , instance )
367
+ recordEvent (internalCtx , r , instance , v1 .EventTypeWarning , "" )
368
+ return reconcile.Result {RequeueAfter : timeout }, faulttype , nil
366
369
}
367
370
368
371
pvc := & v1.PersistentVolumeClaim {}
@@ -383,7 +386,7 @@ func (r *ReconcileCnsNodeVMAttachment) Reconcile(ctx context.Context,
383
386
}
384
387
}
385
388
if ! cnsPvcFinalizerExists {
386
- faulttype , err = addFinalizerToPVC (internalCtx , r .client , pvc )
389
+ _ , err = addFinalizerToPVC (internalCtx , r .client , pvc )
387
390
if err != nil {
388
391
msg := fmt .Sprintf ("failed to add %q finalizer on the PVC with volumename: %q on namespace: %q. Err: %+v" ,
389
392
cnsoptypes .CNSPvcFinalizer , instance .Spec .VolumeName , instance .Namespace , err )
@@ -396,31 +399,27 @@ func (r *ReconcileCnsNodeVMAttachment) Reconcile(ctx context.Context,
396
399
return reconcile.Result {RequeueAfter : timeout }, csifault .CSIInternalFault , nil
397
400
}
398
401
}
399
- if attachErr != nil {
400
- // Update CnsNodeVMAttachment instance with attach error message.
401
- instance .Status .Error = attachErr .Error ()
402
- } else {
403
- // Add the CNS volume ID in the attachment metadata. This is used later
404
- // to detach the CNS volume on deletion of CnsNodeVMAttachment instance.
405
- // Note that the supervisor PVC can be deleted due to following:
406
- // 1. Bug in external provisioner(https://github.com/kubernetes/kubernetes/issues/84226)
407
- // where DeleteVolume could be invoked in pvcsi before ControllerUnpublishVolume.
408
- // This causes supervisor PVC to be deleted.
409
- // 2. Supervisor namespace user deletes PVC used by a guest cluster.
410
- // 3. Supervisor namespace is deleted
411
- // Basically, we cannot rely on the existence of PVC in supervisor
412
- // cluster for detaching the volume from guest cluster VM. So, the
413
- // logic stores the CNS volume ID in attachmentMetadata itself which
414
- // is used during detach.
415
- // Update CnsNodeVMAttachment instance with attached status set to true
416
- // and attachment metadata.
417
- instance .Status .AttachmentMetadata = make (map [string ]string )
418
- instance .Status .AttachmentMetadata [v1a1 .AttributeCnsVolumeID ] = volumeID
419
- instance .Status .AttachmentMetadata [v1a1 .AttributeFirstClassDiskUUID ] = diskUUID
420
- instance .Status .Attached = true
421
- // Clear the error message.
422
- instance .Status .Error = ""
423
- }
402
+
403
+ // Add the CNS volume ID in the attachment metadata. This is used later
404
+ // to detach the CNS volume on deletion of CnsNodeVMAttachment instance.
405
+ // Note that the supervisor PVC can be deleted due to following:
406
+ // 1. Bug in external provisioner(https://github.com/kubernetes/kubernetes/issues/84226)
407
+ // where DeleteVolume could be invoked in pvcsi before ControllerUnpublishVolume.
408
+ // This causes supervisor PVC to be deleted.
409
+ // 2. Supervisor namespace user deletes PVC used by a guest cluster.
410
+ // 3. Supervisor namespace is deleted
411
+ // Basically, we cannot rely on the existence of PVC in supervisor
412
+ // cluster for detaching the volume from guest cluster VM. So, the
413
+ // logic stores the CNS volume ID in attachmentMetadata itself which
414
+ // is used during detach.
415
+ // Update CnsNodeVMAttachment instance with attached status set to true
416
+ // and attachment metadata.
417
+ instance .Status .AttachmentMetadata = make (map [string ]string )
418
+ instance .Status .AttachmentMetadata [v1a1 .AttributeCnsVolumeID ] = volumeID
419
+ instance .Status .AttachmentMetadata [v1a1 .AttributeFirstClassDiskUUID ] = diskUUID
420
+ instance .Status .Attached = true
421
+ // Clear the error message.
422
+ instance .Status .Error = ""
424
423
425
424
err = k8s .UpdateStatus (internalCtx , r .client , instance )
426
425
if err != nil {
@@ -431,11 +430,6 @@ func (r *ReconcileCnsNodeVMAttachment) Reconcile(ctx context.Context,
431
430
return reconcile.Result {RequeueAfter : timeout }, csifault .CSIApiServerOperationFault , nil
432
431
}
433
432
434
- if attachErr != nil {
435
- recordEvent (internalCtx , r , instance , v1 .EventTypeWarning , "" )
436
- return reconcile.Result {RequeueAfter : timeout }, faulttype , nil
437
- }
438
-
439
433
msg := fmt .Sprintf ("ReconcileCnsNodeVMAttachment: Successfully updated entry in CNS for instance " +
440
434
"with name %q and namespace %q." , request .Name , request .Namespace )
441
435
recordEvent (internalCtx , r , instance , v1 .EventTypeNormal , msg )
@@ -607,6 +601,7 @@ func (r *ReconcileCnsNodeVMAttachment) Reconcile(ctx context.Context,
607
601
log .Infof ("Finished Reconcile for CnsNodeVMAttachment request: %q" , request .NamespacedName )
608
602
return reconcile.Result {}, "" , nil
609
603
}
604
+
610
605
// creating new context for reconcileCnsNodeVMAttachmentInternal, as kubernetes supplied context can get canceled
611
606
// This is required to ensure CNS operations won't get prematurely canceled by the controller runtime’s
612
607
// internal reconcile logic.
0 commit comments