@@ -319,22 +319,26 @@ func reconcileNASNodeAccess(
319
319
if ! config .AutoExportPolicy {
320
320
return nil
321
321
}
322
+
322
323
err := ensureExportPolicyExists (ctx , policyName , clientAPI )
323
324
if err != nil {
324
325
return err
325
326
}
327
+
326
328
desiredRules , err := getDesiredExportPolicyRules (ctx , nodes , config )
327
329
if err != nil {
328
330
err = fmt .Errorf ("unable to determine desired export policy rules; %v" , err )
329
331
Logc (ctx ).Error (err )
330
332
return err
331
333
}
334
+
332
335
err = reconcileExportPolicyRules (ctx , policyName , desiredRules , clientAPI , config )
333
336
if err != nil {
334
- err = fmt .Errorf ("unabled to reconcile export policy rules; %v" , err )
337
+ err = fmt .Errorf ("unable to reconcile export policy rules; %v" , err )
335
338
Logc (ctx ).WithField ("ExportPolicy" , policyName ).Error (err )
336
339
return err
337
340
}
341
+
338
342
return nil
339
343
}
340
344
@@ -344,6 +348,16 @@ func ensureNodeAccessForPolicy(
344
348
ctx context.Context , targetNode * tridentmodels.Node , clientAPI api.OntapAPI ,
345
349
config * drivers.OntapStorageDriverConfig , policyName string ,
346
350
) error {
351
+ fields := LogFields {
352
+ "Method" : "ensureNodeAccessForPolicy" ,
353
+ "Type" : "ontap_common" ,
354
+ "policyName" : policyName ,
355
+ "targetNodeIPs" : targetNode .IPs ,
356
+ }
357
+
358
+ Logc (ctx ).WithFields (fields ).Debug (">>>> ensureNodeAccessForPolicy" )
359
+ defer Logc (ctx ).WithFields (fields ).Debug ("<<<< ensureNodeAccessForPolicy" )
360
+
347
361
if exists , err := clientAPI .ExportPolicyExists (ctx , policyName ); err != nil {
348
362
return err
349
363
} else if ! exists {
@@ -360,31 +374,62 @@ func ensureNodeAccessForPolicy(
360
374
Logc (ctx ).Error (err )
361
375
return err
362
376
}
377
+ Logc (ctx ).WithField ("desiredRules" , desiredRules ).Debug ("Desired export policy rules." )
363
378
364
379
// first grab all existing rules
365
380
existingRules , err := clientAPI .ExportRuleList (ctx , policyName )
366
381
if err != nil {
367
382
// Could not list rules, just log it, no action required.
368
383
Logc (ctx ).WithField ("error" , err ).Debug ("Export policy rules could not be listed." )
369
384
}
385
+ Logc (ctx ).WithField ("existingRules" , existingRules ).Debug ("Existing export policy rules." )
370
386
371
387
for _ , desiredRule := range desiredRules {
388
+ desiredRule = strings .TrimSpace (desiredRule )
389
+
390
+ desiredIP := net .ParseIP (desiredRule )
391
+ if desiredIP == nil {
392
+ Logc (ctx ).WithField ("desiredRule" , desiredRule ).Debug ("Invalid desired rule IP" )
393
+ continue
394
+ }
372
395
373
396
// Loop through the existing rules one by one and compare to make sure we cover the scenario where the
374
- // existing rule is of format "10.193.112.26, 10.244 .2.0 " and the desired rule is format "10.193.112.26 ".
397
+ // existing rule is of format "1.1.1.1, 2.2 .2.2 " and the desired rule is format "1.1.1.1 ".
375
398
// This can happen because of the difference in how ONTAP ZAPI and ONTAP REST creates export rule.
376
399
377
400
ruleFound := false
378
401
for existingRule := range existingRules {
379
- if strings .Contains (existingRule , desiredRule ) {
380
- ruleFound = true
402
+ existingIPs := strings .Split (existingRule , "," )
403
+
404
+ for _ , ip := range existingIPs {
405
+ ip = strings .TrimSpace (ip )
406
+
407
+ existingIP := net .ParseIP (ip )
408
+ if existingIP == nil {
409
+ Logc (ctx ).WithField ("existingRule" , existingRule ).Debug ("Invalid existing rule IP" )
410
+ continue
411
+ }
412
+
413
+ if existingIP .Equal (desiredIP ) {
414
+ ruleFound = true
415
+ break
416
+ }
417
+ }
418
+
419
+ if ruleFound {
381
420
break
382
421
}
383
422
}
384
423
385
424
// Rule does not exist, so create it
386
425
if ! ruleFound {
387
426
if err = clientAPI .ExportRuleCreate (ctx , policyName , desiredRule , config .NASType ); err != nil {
427
+ // Check if error is that the export policy rule already exist error
428
+ if errors .IsAlreadyExistsError (err ) {
429
+ Logc (ctx ).WithField ("desiredRule" , desiredRule ).WithError (err ).Debug (
430
+ "Export policy rule already exists" )
431
+ continue
432
+ }
388
433
return err
389
434
}
390
435
}
@@ -429,19 +474,43 @@ func reconcileExportPolicyRules(
429
474
// Could not extract rules, just log it, no action required.
430
475
Logc (ctx ).WithField ("error" , err ).Debug ("Export policy rules could not be extracted." )
431
476
}
477
+ Logc (ctx ).WithField ("existingRules" , existingRules ).Debug ("Existing export policy rules." )
432
478
433
479
undesiredRules := maps .Clone (existingRules )
434
480
435
481
for _ , desiredRule := range desiredPolicyRules {
482
+ desiredRule = strings .TrimSpace (desiredRule )
483
+
484
+ desiredIP := net .ParseIP (desiredRule )
485
+ if desiredIP == nil {
486
+ Logc (ctx ).WithField ("desiredRule" , desiredRule ).Debug ("Invalid desired rule IP" )
487
+ continue
488
+ }
436
489
437
490
// Loop through the existing rules one by one and compare to make sure we cover the scenario where the
438
491
// existing rule is of format "1.1.1.1, 2.2.2.2" and the desired rule is format "1.1.1.1".
439
492
// This can happen because of the difference in how ONTAP ZAPI and ONTAP REST creates export rule.
440
493
441
494
foundExistingRule := ""
442
495
for existingRule := range existingRules {
443
- if strings .Contains (existingRule , desiredRule ) {
444
- foundExistingRule = existingRule
496
+ existingIPs := strings .Split (existingRule , "," )
497
+
498
+ for _ , ip := range existingIPs {
499
+ ip = strings .TrimSpace (ip )
500
+
501
+ existingIP := net .ParseIP (ip )
502
+ if existingIP == nil {
503
+ Logc (ctx ).WithField ("existingRule" , existingRule ).Debug ("Invalid existing rule IP" )
504
+ continue
505
+ }
506
+
507
+ if existingIP .Equal (desiredIP ) {
508
+ foundExistingRule = existingRule
509
+ break
510
+ }
511
+ }
512
+
513
+ if foundExistingRule != "" {
445
514
break
446
515
}
447
516
}
@@ -462,6 +531,8 @@ func reconcileExportPolicyRules(
462
531
}
463
532
}
464
533
}
534
+
535
+ Logc (ctx ).WithField ("undesiredRules" , undesiredRules ).Debug ("Undesired export policy rules." )
465
536
// Now that the desired rules exists, delete the undesired rules
466
537
for _ , ruleIndex := range undesiredRules {
467
538
if err = clientAPI .ExportRuleDestroy (ctx , policyName , ruleIndex ); err != nil {
0 commit comments