From 6007f377cc10cb99b02cabb0e36e296ef8eb0906 Mon Sep 17 00:00:00 2001 From: Federico Paolinelli Date: Mon, 26 Jun 2023 13:42:33 +0200 Subject: [PATCH] Support the "allowAll" flag The flag was not implemented, here we cover with a test and support it. Signed-off-by: Federico Paolinelli --- internal/controller/api_to_config.go | 34 +++++--- internal/controller/api_to_config_test.go | 99 +++++++++++++++++++++++ 2 files changed, 123 insertions(+), 10 deletions(-) diff --git a/internal/controller/api_to_config.go b/internal/controller/api_to_config.go index 4c630462..62662cc6 100644 --- a/internal/controller/api_to_config.go +++ b/internal/controller/api_to_config.go @@ -8,7 +8,7 @@ import ( "github.com/metallb/frrk8s/internal/ipfamily" ) -func apiToFRR(fromK8s *v1beta1.FRRConfiguration) (*frr.Config, error) { +func apiToFRR(fromK8s v1beta1.FRRConfiguration) (*frr.Config, error) { res := &frr.Config{ Routers: make([]*frr.RouterConfig, 0), //BFDProfiles: sm.bfdProfiles, @@ -34,13 +34,6 @@ func routerToFRRConfig(r v1beta1.Router) (*frr.RouterConfig, error) { IPV6Prefixes: make([]string, 0), } - for _, n := range r.Neighbors { - frrNeigh, err := neighborToFRR(n) - if err != nil { - return nil, err - } - res.Neighbors = append(res.Neighbors, frrNeigh) - } for _, p := range r.Prefixes { family := ipfamily.ForCIDRString(p) switch family { @@ -51,12 +44,20 @@ func routerToFRRConfig(r v1beta1.Router) (*frr.RouterConfig, error) { case ipfamily.Unknown: return nil, fmt.Errorf("unknown ipfamily for %s", p) } + } + for _, n := range r.Neighbors { + frrNeigh, err := neighborToFRR(n, res.IPV4Prefixes, res.IPV6Prefixes) + if err != nil { + return nil, err + } + res.Neighbors = append(res.Neighbors, frrNeigh) } + return res, nil } -func neighborToFRR(n v1beta1.Neighbor) (*frr.NeighborConfig, error) { +func neighborToFRR(n v1beta1.Neighbor, ipv4Prefixes, ipv6Prefixes []string) (*frr.NeighborConfig, error) { neighborFamily, err := ipfamily.ForAddresses(n.Address) if err != nil { return nil, fmt.Errorf("failed to find ipfamily for %s, %w", n.Address, err) @@ -71,7 +72,20 @@ func neighborToFRR(n v1beta1.Neighbor) (*frr.NeighborConfig, error) { IPFamily: neighborFamily, EBGPMultiHop: n.EBGPMultiHop, } - // TODO allow all + + if n.ToAdvertise.Allowed.Mode == v1beta1.AllowAll { + for _, p := range ipv4Prefixes { + res.Advertisements = append(res.Advertisements, &frr.AdvertisementConfig{Prefix: p, IPFamily: ipfamily.IPv4}) + res.HasV4Advertisements = true + } + for _, p := range ipv6Prefixes { + res.Advertisements = append(res.Advertisements, &frr.AdvertisementConfig{Prefix: p, IPFamily: ipfamily.IPv6}) + res.HasV6Advertisements = true + } + + return res, nil + } + for _, p := range n.ToAdvertise.Allowed.Prefixes { family := ipfamily.ForCIDRString(p) switch family { diff --git a/internal/controller/api_to_config_test.go b/internal/controller/api_to_config_test.go index 154dc274..fc4bd76f 100644 --- a/internal/controller/api_to_config_test.go +++ b/internal/controller/api_to_config_test.go @@ -319,6 +319,105 @@ func TestConversion(t *testing.T) { }, err: nil, }, + { + name: "Two Neighbor with ToAdvertise, one advertise all", + fromK8s: []v1beta1.FRRConfiguration{ + { + Spec: v1beta1.FRRConfigurationSpec{ + BGP: v1beta1.BGPConfig{ + Routers: []v1beta1.Router{ + { + ASN: 65040, + ID: "192.0.2.20", + Neighbors: []v1beta1.Neighbor{ + { + ASN: 65041, + Address: "192.0.2.21", + Port: 179, + ToAdvertise: v1beta1.Advertise{ + Allowed: v1beta1.AllowedPrefixes{ + Prefixes: []string{"192.0.2.0/24", "192.0.4.0/24"}, + Mode: v1beta1.AllowRestricted, + }, + }, + }, + { + ASN: 65041, + Address: "192.0.2.22", + Port: 179, + ToAdvertise: v1beta1.Advertise{ + Allowed: v1beta1.AllowedPrefixes{ + Mode: v1beta1.AllowAll, + }, + }, + }, + }, + Prefixes: []string{"192.0.2.0/24", "192.0.3.0/24", "192.0.4.0/24", "2001:db8::/64"}, + }, + }, + }, + }, + }, + }, + expected: &frr.Config{ + Routers: []*frr.RouterConfig{ + { + MyASN: 65040, + RouterID: "192.0.2.20", + Neighbors: []*frr.NeighborConfig{ + { + IPFamily: ipfamily.IPv4, + Name: "65041@192.0.2.21", + ASN: 65041, + Addr: "192.0.2.21", + Port: 179, + Advertisements: []*frr.AdvertisementConfig{ + { + IPFamily: ipfamily.IPv4, + Prefix: "192.0.2.0/24", + }, + { + IPFamily: ipfamily.IPv4, + Prefix: "192.0.4.0/24", + }, + }, + HasV4Advertisements: true, + }, + { + IPFamily: ipfamily.IPv4, + Name: "65041@192.0.2.22", + ASN: 65041, + Addr: "192.0.2.22", + Port: 179, + Advertisements: []*frr.AdvertisementConfig{ + { + IPFamily: ipfamily.IPv4, + Prefix: "192.0.2.0/24", + }, + { + IPFamily: ipfamily.IPv4, + Prefix: "192.0.3.0/24", + }, + { + IPFamily: ipfamily.IPv4, + Prefix: "192.0.4.0/24", + }, + { + IPFamily: ipfamily.IPv6, + Prefix: "2001:db8::/64", + }, + }, + HasV4Advertisements: true, + HasV6Advertisements: true, + }, + }, + IPV4Prefixes: []string{"192.0.2.0/24", "192.0.3.0/24", "192.0.4.0/24"}, + IPV6Prefixes: []string{"2001:db8::/64"}, + }, + }, + }, + err: nil, + }, } for _, test := range tests {