@@ -246,6 +246,52 @@ func (r *ReconcileCnsFileAccessConfig) Reconcile(ctx context.Context,
246
246
msg := fmt .Sprintf ("Failed to get virtualmachine instance for the VM with name: %q. Error: %+v" ,
247
247
instance .Spec .VMName , err )
248
248
log .Error (msg )
249
+ // If virtualmachine instance is NotFound and if deletion timestamp is set on CnsFileAccessConfig instance,
250
+ // then proceed with the deletion of CnsFileAccessConfig instance.
251
+ if apierrors .IsNotFound (err ) && instance .DeletionTimestamp != nil {
252
+ log .Infof ("CnsFileAccessConfig instance %q has deletion timestamp set, but VM instance with " +
253
+ "name %q is not found. Processing the deletion of CnsFileAccessConfig instance." ,
254
+ instance .Name , instance .Spec .VMName )
255
+ // Fetch the PVC and PV instance and get volume ID
256
+ skipConfigureVolumeACL := false
257
+ volumeID , err := cnsoperatorutil .GetVolumeID (ctx , r .client , instance .Spec .PvcName , instance .Namespace )
258
+ if err != nil {
259
+ if apierrors .IsNotFound (err ) {
260
+ // If PVC instance is NotFound (deleted), then there is no need to configure ACL on file volume
261
+ skipConfigureVolumeACL = true
262
+ } else {
263
+ msg := fmt .Sprintf ("Failed to get volumeID from pvcName: %q. Error: %+v" , instance .Spec .PvcName , err )
264
+ log .Error (msg )
265
+ setInstanceError (ctx , r , instance , msg )
266
+ return reconcile.Result {RequeueAfter : timeout }, nil
267
+ }
268
+ }
269
+ if ! skipConfigureVolumeACL {
270
+ err = r .removePermissionsForFileVolume (ctx , volumeID , instance )
271
+ if err != nil {
272
+ msg := fmt .Sprintf ("Failed to remove file volume permissions with error: %+v" , err )
273
+ log .Error (msg )
274
+ setInstanceError (ctx , r , instance , msg )
275
+ return reconcile.Result {RequeueAfter : timeout }, nil
276
+ }
277
+ }
278
+
279
+ // Remove finalizer from CnsFileAccessConfig CRD
280
+ removeFinalizerFromCRDInstance (ctx , instance )
281
+ err = updateCnsFileAccessConfig (ctx , r .client , instance )
282
+ if err != nil {
283
+ msg := fmt .Sprintf ("failed to update CnsFileAccessConfig instance: %q on namespace: %q. Error: %+v" ,
284
+ instance .Name , instance .Namespace , err )
285
+ recordEvent (ctx , r , instance , v1 .EventTypeWarning , msg )
286
+ return reconcile.Result {RequeueAfter : timeout }, nil
287
+ }
288
+ // Cleanup instance entry from backOffDuration map.
289
+ backOffDurationMapMutex .Lock ()
290
+ delete (backOffDuration , instance .Name )
291
+ backOffDurationMapMutex .Unlock ()
292
+ return reconcile.Result {}, nil
293
+ }
294
+
249
295
setInstanceError (ctx , r , instance , msg )
250
296
return reconcile.Result {RequeueAfter : timeout }, nil
251
297
}
@@ -412,6 +458,46 @@ func (r *ReconcileCnsFileAccessConfig) Reconcile(ctx context.Context,
412
458
return reconcile.Result {}, nil
413
459
}
414
460
461
+ // removePermissionsForFileVolume helps to remove net permissions for a given file volume.
462
+ // This method is used when we don't have VM instance. It fetches the VM IP from CNSFileVolumeClient
463
+ // instance for the VM name assocaited with CnsFileAccessConfig.
464
+ func (r * ReconcileCnsFileAccessConfig ) removePermissionsForFileVolume (ctx context.Context ,
465
+ volumeID string , instance * cnsfileaccessconfigv1alpha1.CnsFileAccessConfig ) error {
466
+ log := logger .GetLogger (ctx )
467
+ cnsFileVolumeClientInstance , err := cnsfilevolumeclient .GetFileVolumeClientInstance (ctx )
468
+ if err != nil {
469
+ return logger .LogNewErrorf (log , "Failed to get CNSFileVolumeClient instance. Error: %+v" , err )
470
+ }
471
+
472
+ vmIP , vmsAssociatedWithIP , err := cnsFileVolumeClientInstance .GetVMIPFromVMName (ctx ,
473
+ instance .Namespace + "/" + instance .Spec .PvcName , instance .Spec .VMName )
474
+ if err != nil {
475
+ return logger .LogNewErrorf (log , "Failed to get VM IP from VM name in CNSFileVolumeClient instance. " +
476
+ "Error: %+v" , err )
477
+ }
478
+ if vmIP == "" {
479
+ // vmIP is "" if we can't find given vmName in ExternalIPtoClientVms map of CNSFileVolumeClient instance.
480
+ // Assuming that this vmName is already removed, return success.
481
+ return nil
482
+ }
483
+ if vmsAssociatedWithIP == 1 {
484
+ err = r .configureVolumeACLs (ctx , volumeID , vmIP , true )
485
+ if err != nil {
486
+ return logger .LogNewErrorf (log , "Failed to remove net permissions for file volume %q. Error: %+v" ,
487
+ volumeID , err )
488
+ }
489
+ }
490
+ err = cnsFileVolumeClientInstance .RemoveClientVMFromIPList (ctx ,
491
+ instance .Namespace + "/" + instance .Spec .PvcName , instance .Spec .VMName , vmIP )
492
+ if err != nil {
493
+ return logger .LogNewErrorf (log , "Failed to remove VM %q with IP %q from IPList. Error: %+v" ,
494
+ instance .Spec .VMName , vmIP , err )
495
+ }
496
+ log .Infof ("Successfully removed VM IP %q from IPList for CnsFileAccessConfig request with name: %q on " +
497
+ "namespace: %q" , vmIP , instance .Name , instance .Namespace )
498
+ return nil
499
+ }
500
+
415
501
// configureNetPermissionsForFileVolume helps to add or remove net permissions
416
502
// for a given file volume. The callers of this method can remove or add net
417
503
// permissions by setting the parameter removePermission to true or false
0 commit comments