Skip to content

Commit bece7fd

Browse files
authored
Merge pull request #221 from karampok/OCPBUGS-42604
Throw better error when prefix for same localPref is added twice
2 parents 3ac4d9f + d1052e8 commit bece7fd

File tree

2 files changed

+102
-4
lines changed

2 files changed

+102
-4
lines changed

internal/controller/api_to_config.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -514,20 +514,36 @@ func localPrefPrefixesToMap(withLocalPref []v1beta1.LocalPrefPrefixes) (localPre
514514
localPrefForPrefixV6: map[string]uint32{},
515515
}
516516

517+
seen := make(map[uint32]bool)
517518
for _, pfxs := range withLocalPref {
519+
if _, exists := seen[pfxs.LocalPref]; exists {
520+
// error when input is withLocalPref = []v1beta1.LocalPrefPrefixes{
521+
// Prefixes: []string{"192.0.2.0/24"},LocalPref: 100}, []string{"100.0.0.0/24"},LocalPref: 100}}
522+
// so we enforce withLocalPref = []v1beta1.LocalPrefPrefixes{
523+
// Prefixes: []string{"192.0.2.0/24, "100.0.0.0/24"},LocalPref: 100}}
524+
return localPrefPrefixes{}, fmt.Errorf("multiple entries with the same localPref: %d", pfxs.LocalPref)
525+
}
526+
seen[pfxs.LocalPref] = true
527+
518528
for _, p := range pfxs.Prefixes {
519529
family := ipfamily.ForCIDRString(p)
520530
lpMap := res.localPrefForPrefixV4
521531
if family == ipfamily.IPv6 {
522532
lpMap = res.localPrefForPrefixV6
523533
}
524534

525-
_, ok := lpMap[p]
526-
if ok {
527-
return localPrefPrefixes{}, fmt.Errorf("multiple local prefs specified for prefix %s", p)
535+
v, ok := lpMap[p]
536+
if !ok {
537+
lpMap[p] = pfxs.LocalPref
538+
continue
528539
}
529540

530-
lpMap[p] = pfxs.LocalPref
541+
if v != pfxs.LocalPref {
542+
return localPrefPrefixes{}, fmt.Errorf("multiple local prefs (%d,%d) specified for prefix %s", v, pfxs.LocalPref, p)
543+
}
544+
if v == pfxs.LocalPref {
545+
return localPrefPrefixes{}, fmt.Errorf("prefix %s with local prefs %d defined twice", p, v)
546+
}
531547
}
532548
}
533549

internal/controller/api_to_config_test.go

+82
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,88 @@ func TestConversion(t *testing.T) {
843843
expected: nil,
844844
err: fmt.Errorf("multiple local prefs specified for prefix %s", "192.0.4.0/24"),
845845
},
846+
{
847+
name: "One neighbor, trying to set samelocalPrefs for different prefix entries",
848+
fromK8s: []v1beta1.FRRConfiguration{
849+
{
850+
Spec: v1beta1.FRRConfigurationSpec{
851+
BGP: v1beta1.BGPConfig{
852+
Routers: []v1beta1.Router{
853+
{
854+
ASN: 65040,
855+
ID: "192.0.2.20",
856+
Neighbors: []v1beta1.Neighbor{
857+
{
858+
ASN: 65041,
859+
Address: "192.0.2.21",
860+
ToAdvertise: v1beta1.Advertise{
861+
Allowed: v1beta1.AllowedOutPrefixes{
862+
Prefixes: []string{"192.0.2.0/24", "10.0.0.0/24"},
863+
Mode: v1beta1.AllowRestricted,
864+
},
865+
PrefixesWithLocalPref: []v1beta1.LocalPrefPrefixes{
866+
{
867+
Prefixes: []string{"10.0.0.0/24"},
868+
LocalPref: 100,
869+
},
870+
{
871+
Prefixes: []string{"192.0.2.0/24"},
872+
LocalPref: 100,
873+
},
874+
},
875+
},
876+
},
877+
},
878+
Prefixes: []string{"192.0.2.0/24", "10.0.0.0/24"},
879+
},
880+
},
881+
},
882+
},
883+
},
884+
},
885+
secrets: map[string]v1.Secret{},
886+
expected: nil,
887+
err: fmt.Errorf("a not nil error"),
888+
},
889+
{
890+
name: "One neighbor, trying to set samelocalPrefs for a prefix twice",
891+
fromK8s: []v1beta1.FRRConfiguration{
892+
{
893+
Spec: v1beta1.FRRConfigurationSpec{
894+
BGP: v1beta1.BGPConfig{
895+
Routers: []v1beta1.Router{
896+
{
897+
ASN: 65040,
898+
ID: "192.0.2.20",
899+
Neighbors: []v1beta1.Neighbor{
900+
{
901+
ASN: 65041,
902+
Address: "192.0.2.21",
903+
ToAdvertise: v1beta1.Advertise{
904+
Allowed: v1beta1.AllowedOutPrefixes{
905+
Prefixes: []string{"192.0.2.0/24", "192.0.2.0/24"},
906+
Mode: v1beta1.AllowRestricted,
907+
},
908+
PrefixesWithLocalPref: []v1beta1.LocalPrefPrefixes{
909+
{
910+
Prefixes: []string{"192.0.2.0/24", "192.0.2.0/24"},
911+
LocalPref: 100,
912+
},
913+
},
914+
},
915+
},
916+
},
917+
Prefixes: []string{"192.0.2.0/24"},
918+
},
919+
},
920+
},
921+
},
922+
},
923+
},
924+
secrets: map[string]v1.Secret{},
925+
expected: nil,
926+
err: fmt.Errorf("a not nil error"),
927+
},
846928
{
847929
name: "Neighbor with ToReceiveAll",
848930
fromK8s: []v1beta1.FRRConfiguration{

0 commit comments

Comments
 (0)