Skip to content

Commit 76d9490

Browse files
authored
Fix IP comparison logic
1 parent ed81120 commit 76d9490

File tree

4 files changed

+1266
-301
lines changed

4 files changed

+1266
-301
lines changed

pkg/network/network.go

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func FilterIPs(ctx context.Context, ips, cidrs []string) ([]string, error) {
5959
}
6060

6161
for _, ip := range ips {
62+
ip = strings.TrimSpace(ip)
6263
parsedIP := net.ParseIP(ip)
6364
for _, network := range networks {
6465
fields := logging.LogFields{

storage_drivers/ontap/ontap_common.go

+77-6
Original file line numberDiff line numberDiff line change
@@ -319,22 +319,26 @@ func reconcileNASNodeAccess(
319319
if !config.AutoExportPolicy {
320320
return nil
321321
}
322+
322323
err := ensureExportPolicyExists(ctx, policyName, clientAPI)
323324
if err != nil {
324325
return err
325326
}
327+
326328
desiredRules, err := getDesiredExportPolicyRules(ctx, nodes, config)
327329
if err != nil {
328330
err = fmt.Errorf("unable to determine desired export policy rules; %v", err)
329331
Logc(ctx).Error(err)
330332
return err
331333
}
334+
332335
err = reconcileExportPolicyRules(ctx, policyName, desiredRules, clientAPI, config)
333336
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)
335338
Logc(ctx).WithField("ExportPolicy", policyName).Error(err)
336339
return err
337340
}
341+
338342
return nil
339343
}
340344

@@ -344,6 +348,16 @@ func ensureNodeAccessForPolicy(
344348
ctx context.Context, targetNode *tridentmodels.Node, clientAPI api.OntapAPI,
345349
config *drivers.OntapStorageDriverConfig, policyName string,
346350
) 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+
347361
if exists, err := clientAPI.ExportPolicyExists(ctx, policyName); err != nil {
348362
return err
349363
} else if !exists {
@@ -360,31 +374,62 @@ func ensureNodeAccessForPolicy(
360374
Logc(ctx).Error(err)
361375
return err
362376
}
377+
Logc(ctx).WithField("desiredRules", desiredRules).Debug("Desired export policy rules.")
363378

364379
// first grab all existing rules
365380
existingRules, err := clientAPI.ExportRuleList(ctx, policyName)
366381
if err != nil {
367382
// Could not list rules, just log it, no action required.
368383
Logc(ctx).WithField("error", err).Debug("Export policy rules could not be listed.")
369384
}
385+
Logc(ctx).WithField("existingRules", existingRules).Debug("Existing export policy rules.")
370386

371387
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+
}
372395

373396
// 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".
375398
// This can happen because of the difference in how ONTAP ZAPI and ONTAP REST creates export rule.
376399

377400
ruleFound := false
378401
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 {
381420
break
382421
}
383422
}
384423

385424
// Rule does not exist, so create it
386425
if !ruleFound {
387426
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+
}
388433
return err
389434
}
390435
}
@@ -429,19 +474,43 @@ func reconcileExportPolicyRules(
429474
// Could not extract rules, just log it, no action required.
430475
Logc(ctx).WithField("error", err).Debug("Export policy rules could not be extracted.")
431476
}
477+
Logc(ctx).WithField("existingRules", existingRules).Debug("Existing export policy rules.")
432478

433479
undesiredRules := maps.Clone(existingRules)
434480

435481
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+
}
436489

437490
// Loop through the existing rules one by one and compare to make sure we cover the scenario where the
438491
// existing rule is of format "1.1.1.1, 2.2.2.2" and the desired rule is format "1.1.1.1".
439492
// This can happen because of the difference in how ONTAP ZAPI and ONTAP REST creates export rule.
440493

441494
foundExistingRule := ""
442495
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 != "" {
445514
break
446515
}
447516
}
@@ -462,6 +531,8 @@ func reconcileExportPolicyRules(
462531
}
463532
}
464533
}
534+
535+
Logc(ctx).WithField("undesiredRules", undesiredRules).Debug("Undesired export policy rules.")
465536
// Now that the desired rules exists, delete the undesired rules
466537
for _, ruleIndex := range undesiredRules {
467538
if err = clientAPI.ExportRuleDestroy(ctx, policyName, ruleIndex); err != nil {

0 commit comments

Comments
 (0)