From 814e05bbeb87d280781051457996a8065660e143 Mon Sep 17 00:00:00 2001 From: Kumar Atish Date: Tue, 26 Nov 2024 23:09:28 +0530 Subject: [PATCH] Add route info in output of "antctl get bgproutes" (#6803) Information includes the route type, along with a reference to the corresponding K8s object when applicable. Fixes #6794 Signed-off-by: Kumar Atish --- docs/antctl.md | 26 +- pkg/agent/apis/types.go | 8 +- .../apiserver/handlers/bgproute/handler.go | 31 +- .../handlers/bgproute/handler_test.go | 89 ++++- pkg/agent/controller/bgp/controller.go | 123 +++--- pkg/agent/controller/bgp/controller_test.go | 367 +++++++++--------- pkg/querier/querier.go | 3 +- pkg/querier/testing/mock_querier.go | 5 +- 8 files changed, 397 insertions(+), 255 deletions(-) diff --git a/docs/antctl.md b/docs/antctl.md index 62073d87dc4..d4cc5ae9345 100644 --- a/docs/antctl.md +++ b/docs/antctl.md @@ -803,25 +803,29 @@ For more information about route advertisement, please refer to [Advertisements] # Get the list of all advertised bgp routes $ antctl get bgproutes -ROUTE -10.96.10.10/32 -192.168.77.100/32 -fec0::10:96:10:10/128 -fec0::192:168:77:100/128 +ROUTE TYPE K8S-OBJ-REF +172.18.0.3/32 EgressIP egress1 +10.244.1.0/24 NodeIPAMPodCIDR +10.96.0.1/32 ServiceLoadBalancerIP default/svc1 +fec0::192:168:77:100/128 EgressIP egress2 +fd00:10:244:1::/64 NodeIPAMPodCIDR +fec0::10:96:10:10/128 ServiceLoadBalancerIP default/svc2 # Get the list of advertised IPv4 bgp routes $ antctl get bgproutes --ipv4-only -ROUTE -10.96.10.10/32 -192.168.77.100/32 +ROUTE TYPE K8S-OBJ-REF +172.18.0.3/32 EgressIP egress1 +10.244.1.0/24 NodeIPAMPodCIDR +10.96.0.1/32 ServiceLoadBalancerIP default/svc1 # Get the list of advertised IPv6 bgp routes $ antctl get bgproutes --ipv6-only -ROUTE -fec0::10:96:10:10/128 -fec0::192:168:77:100/128 +ROUTE TYPE K8S-OBJ-REF +fec0::192:168:77:100/128 EgressIP egress2 +fd00:10:244:1::/64 NodeIPAMPodCIDR +fec0::10:96:10:10/128 ServiceLoadBalancerIP default/svc2 ``` ### Upgrade existing objects of CRDs diff --git a/pkg/agent/apis/types.go b/pkg/agent/apis/types.go index 1b00c9d80b8..0c4d275bab5 100644 --- a/pkg/agent/apis/types.go +++ b/pkg/agent/apis/types.go @@ -232,15 +232,17 @@ func (r BGPPeerResponse) SortRows() bool { // BGPRouteResponse describes the response struct of bgproutes command. type BGPRouteResponse struct { - Route string `json:"route,omitempty"` + Route string `json:"route,omitempty"` + Type string `json:"type,omitempty"` + K8sObjRef string `json:"k8sObjRef,omitempty"` } func (r BGPRouteResponse) GetTableHeader() []string { - return []string{"ROUTE"} + return []string{"ROUTE", "TYPE", "K8S-OBJ-REF"} } func (r BGPRouteResponse) GetTableRow(_ int) []string { - return []string{r.Route} + return []string{r.Route, r.Type, r.K8sObjRef} } func (r BGPRouteResponse) SortRows() bool { diff --git a/pkg/agent/apiserver/handlers/bgproute/handler.go b/pkg/agent/apiserver/handlers/bgproute/handler.go index fbdd8006bcb..477a612830b 100644 --- a/pkg/agent/apiserver/handlers/bgproute/handler.go +++ b/pkg/agent/apiserver/handlers/bgproute/handler.go @@ -18,7 +18,10 @@ import ( "encoding/json" "errors" "net/http" + "net/netip" "reflect" + "slices" + "strings" "k8s.io/klog/v2" @@ -68,11 +71,35 @@ func HandleFunc(bq querier.AgentBGPPolicyInfoQuerier) http.HandlerFunc { } var bgpRoutesResp []apis.BGPRouteResponse - for _, bgpRoute := range bgpRoutes { + for bgpRoute := range bgpRoutes { bgpRoutesResp = append(bgpRoutesResp, apis.BGPRouteResponse{ - Route: bgpRoute, + Route: bgpRoute.Prefix, + Type: string(bgpRoutes[bgpRoute].Type), + K8sObjRef: bgpRoutes[bgpRoute].K8sObjRef, }) } + // make sure that we provide a stable order for the API response + slices.SortFunc(bgpRoutesResp, func(a, b apis.BGPRouteResponse) int { + pA, _ := netip.ParsePrefix(a.Route) + pB, _ := netip.ParsePrefix(b.Route) + // IPv4 routes first, then IPv6 routes + if pA.Addr().Is4() && !pB.Addr().Is4() { + return -1 + } + if !pA.Addr().Is4() && pB.Addr().Is4() { + return 1 + } + // both routes are from the same IP family, now order based on route type + if n := strings.Compare(a.Type, b.Type); n != 0 { + return n + } + // finally, for routes of the same IP family and type, order based on prefix + // shorter prefixes come first; if the length is the same we order by IP + if n := pA.Bits() - pB.Bits(); n != 0 { + return n + } + return pA.Addr().Compare(pB.Addr()) + }) if err := json.NewEncoder(w).Encode(bgpRoutesResp); err != nil { w.WriteHeader(http.StatusInternalServerError) diff --git a/pkg/agent/apiserver/handlers/bgproute/handler_test.go b/pkg/agent/apiserver/handlers/bgproute/handler_test.go index 69333f9e3df..b8cd0f7a75a 100644 --- a/pkg/agent/apiserver/handlers/bgproute/handler_test.go +++ b/pkg/agent/apiserver/handlers/bgproute/handler_test.go @@ -24,12 +24,42 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/mock/gomock" + "k8s.io/utils/net" "antrea.io/antrea/pkg/agent/apis" - "antrea.io/antrea/pkg/agent/controller/bgp" + "antrea.io/antrea/pkg/agent/bgp" + bgpcontroller "antrea.io/antrea/pkg/agent/controller/bgp" queriertest "antrea.io/antrea/pkg/querier/testing" ) +const ( + namespaceDefault = "default" + ipv4Suffix = "/32" + ipv6Suffix = "/128" +) + +var ( + podIPv4CIDR = "10.10.0.0/24" + podIPv4CIDRRoute = bgp.Route{Prefix: podIPv4CIDR} + clusterIPv4 = "10.96.10.10" + clusterIPv4Route = bgp.Route{Prefix: ipStrToPrefix(clusterIPv4)} + loadBalancerIPv6 = "fec0::192:168:77:150" + loadBalancerIPv6Route = bgp.Route{Prefix: ipStrToPrefix(loadBalancerIPv6)} + egressIPv6 = "fec0::192:168:77:200" + egressIPv6Route = bgp.Route{Prefix: ipStrToPrefix(egressIPv6)} + + ipv4ClusterIPName = "clusterip-4" + ipv6LoadBalancerName = "loadbalancer-6" + ipv6EgressName = "egress-6" + + allRoutes = map[bgp.Route]bgpcontroller.RouteMetadata{ + clusterIPv4Route: {Type: bgpcontroller.ServiceClusterIP, K8sObjRef: getServiceName(ipv4ClusterIPName)}, + loadBalancerIPv6Route: {Type: bgpcontroller.ServiceLoadBalancerIP, K8sObjRef: getServiceName(ipv6LoadBalancerName)}, + egressIPv6Route: {Type: bgpcontroller.EgressIP, K8sObjRef: ipv6EgressName}, + podIPv4CIDRRoute: {Type: bgpcontroller.NodeIPAMPodCIDR}, + } +) + func TestBGPRouteQuery(t *testing.T) { ctx := context.Background() tests := []struct { @@ -42,7 +72,7 @@ func TestBGPRouteQuery(t *testing.T) { { name: "bgpPolicyState does not exist", expectedCalls: func(mockBGPServer *queriertest.MockAgentBGPPolicyInfoQuerier) { - mockBGPServer.EXPECT().GetBGPRoutes(context.Background(), true, true).Return(nil, bgp.ErrBGPPolicyNotFound) + mockBGPServer.EXPECT().GetBGPRoutes(context.Background(), true, true).Return(nil, bgpcontroller.ErrBGPPolicyNotFound) }, expectedStatus: http.StatusNotFound, }, @@ -50,18 +80,27 @@ func TestBGPRouteQuery(t *testing.T) { name: "get all advertised routes", expectedCalls: func(mockBGPServer *queriertest.MockAgentBGPPolicyInfoQuerier) { mockBGPServer.EXPECT().GetBGPRoutes(ctx, true, true).Return( - []string{"192.168.1.0/24", "192.168.2.0/24", "fec0::10:96:10:10/128"}, nil) + map[bgp.Route]bgpcontroller.RouteMetadata{ + clusterIPv4Route: allRoutes[clusterIPv4Route], + podIPv4CIDRRoute: allRoutes[podIPv4CIDRRoute], + egressIPv6Route: allRoutes[egressIPv6Route], + }, nil) }, expectedStatus: http.StatusOK, expectedResponse: []apis.BGPRouteResponse{ { - Route: "192.168.1.0/24", + Route: podIPv4CIDR, + Type: string(bgpcontroller.NodeIPAMPodCIDR), }, { - Route: "192.168.2.0/24", + Route: clusterIPv4Route.Prefix, + Type: string(allRoutes[clusterIPv4Route].Type), + K8sObjRef: allRoutes[clusterIPv4Route].K8sObjRef, }, { - Route: "fec0::10:96:10:10/128", + Route: egressIPv6Route.Prefix, + Type: string(allRoutes[egressIPv6Route].Type), + K8sObjRef: allRoutes[egressIPv6Route].K8sObjRef, }, }, }, @@ -70,15 +109,21 @@ func TestBGPRouteQuery(t *testing.T) { url: "?ipv4-only", expectedCalls: func(mockBGPServer *queriertest.MockAgentBGPPolicyInfoQuerier) { mockBGPServer.EXPECT().GetBGPRoutes(ctx, true, false).Return( - []string{"192.168.1.0/24", "192.168.2.0/24"}, nil) + map[bgp.Route]bgpcontroller.RouteMetadata{ + clusterIPv4Route: allRoutes[clusterIPv4Route], + podIPv4CIDRRoute: allRoutes[podIPv4CIDRRoute], + }, nil) }, expectedStatus: http.StatusOK, expectedResponse: []apis.BGPRouteResponse{ { - Route: "192.168.1.0/24", + Route: podIPv4CIDRRoute.Prefix, + Type: string(allRoutes[podIPv4CIDRRoute].Type), }, { - Route: "192.168.2.0/24", + Route: clusterIPv4Route.Prefix, + Type: string(allRoutes[clusterIPv4Route].Type), + K8sObjRef: allRoutes[clusterIPv4Route].K8sObjRef, }, }, }, @@ -87,15 +132,22 @@ func TestBGPRouteQuery(t *testing.T) { url: "?ipv6-only=", expectedCalls: func(mockBGPServer *queriertest.MockAgentBGPPolicyInfoQuerier) { mockBGPServer.EXPECT().GetBGPRoutes(ctx, false, true).Return( - []string{"fec0::192:168:77:150/128", "fec0::10:10:0:10/128"}, nil) + map[bgp.Route]bgpcontroller.RouteMetadata{ + loadBalancerIPv6Route: allRoutes[loadBalancerIPv6Route], + egressIPv6Route: allRoutes[egressIPv6Route], + }, nil) }, expectedStatus: http.StatusOK, expectedResponse: []apis.BGPRouteResponse{ { - Route: "fec0::192:168:77:150/128", + Route: egressIPv6Route.Prefix, + Type: string(allRoutes[egressIPv6Route].Type), + K8sObjRef: allRoutes[egressIPv6Route].K8sObjRef, }, { - Route: "fec0::10:10:0:10/128", + Route: loadBalancerIPv6Route.Prefix, + Type: string(allRoutes[loadBalancerIPv6Route].Type), + K8sObjRef: allRoutes[loadBalancerIPv6Route].K8sObjRef, }, }, }, @@ -136,3 +188,16 @@ func TestBGPRouteQuery(t *testing.T) { }) } } + +func getServiceName(name string) string { + return namespaceDefault + "/" + name +} + +func ipStrToPrefix(ipStr string) string { + if net.IsIPv4String(ipStr) { + return ipStr + ipv4Suffix + } else if net.IsIPv6String(ipStr) { + return ipStr + ipv6Suffix + } + return "" +} diff --git a/pkg/agent/controller/bgp/controller.go b/pkg/agent/controller/bgp/controller.go index 324f1c1d822..6070bb85bee 100644 --- a/pkg/agent/controller/bgp/controller.go +++ b/pkg/agent/controller/bgp/controller.go @@ -78,6 +78,21 @@ var ( ErrBGPPolicyNotFound = errors.New("BGPPolicy not found") ) +type AdvertisedRouteType string + +const ( + EgressIP AdvertisedRouteType = "EgressIP" + ServiceLoadBalancerIP AdvertisedRouteType = "ServiceLoadBalancerIP" + ServiceExternalIP AdvertisedRouteType = "ServiceExternalIP" + ServiceClusterIP AdvertisedRouteType = "ServiceClusterIP" + NodeIPAMPodCIDR AdvertisedRouteType = "NodeIPAMPodCIDR" +) + +type RouteMetadata struct { + Type AdvertisedRouteType + K8sObjRef string +} + type bgpPolicyState struct { // The local BGP server. bgpServer bgp.Interface @@ -89,8 +104,8 @@ type bgpPolicyState struct { localASN int32 // The router ID used by the local BGP server. routerID string - // routes stores all BGP routers advertised to BGP peers. - routes sets.Set[bgp.Route] + // routes stores all BGP routes advertised to BGP peers. + routes map[bgp.Route]RouteMetadata // peerConfigs is a map that stores configurations of BGP peers. The map keys are the concatenated strings of BGP // peer IP address and ASN (e.g., "192.168.77.100-65000", "2001::1-65000"). peerConfigs map[string]bgp.PeerConfig @@ -387,7 +402,7 @@ func (c *Controller) syncBGPPolicy(ctx context.Context) error { routerID: routerID, listenPort: listenPort, localASN: localASN, - routes: make(sets.Set[bgp.Route]), + routes: make(map[bgp.Route]RouteMetadata), peerConfigs: make(map[string]bgp.PeerConfig), } } else if c.bgpPolicyState.bgpPolicyName != bgpPolicyName { @@ -452,26 +467,29 @@ func (c *Controller) reconcileBGPPeers(ctx context.Context, bgpPeers []v1alpha1. } func (c *Controller) reconcileBGPAdvertisements(ctx context.Context, bgpAdvertisements v1alpha1.Advertisements) error { - curRoutes, err := c.getRoutes(bgpAdvertisements) - if err != nil { - return err - } + curRoutes := c.getRoutes(bgpAdvertisements) preRoutes := c.bgpPolicyState.routes - routesToAdvertise := curRoutes.Difference(preRoutes) - routesToWithdraw := preRoutes.Difference(curRoutes) + currRoutesKeys := sets.KeySet(curRoutes) + preRoutesKeys := sets.KeySet(preRoutes) + + routesToAdvertise := currRoutesKeys.Difference(preRoutesKeys) + routesToWithdraw := preRoutesKeys.Difference(currRoutesKeys) bgpServer := c.bgpPolicyState.bgpServer for route := range routesToAdvertise { if err := bgpServer.AdvertiseRoutes(ctx, []bgp.Route{route}); err != nil { return err } - c.bgpPolicyState.routes.Insert(route) + c.bgpPolicyState.routes[route] = RouteMetadata{ + Type: curRoutes[route].Type, + K8sObjRef: curRoutes[route].K8sObjRef, + } } for route := range routesToWithdraw { if err := bgpServer.WithdrawRoutes(ctx, []bgp.Route{route}); err != nil { return err } - c.bgpPolicyState.routes.Delete(route) + delete(c.bgpPolicyState.routes, route) } return nil @@ -540,8 +558,8 @@ func (c *Controller) getRouterID() (string, error) { return routerID, nil } -func (c *Controller) getRoutes(advertisements v1alpha1.Advertisements) (sets.Set[bgp.Route], error) { - allRoutes := sets.New[bgp.Route]() +func (c *Controller) getRoutes(advertisements v1alpha1.Advertisements) map[bgp.Route]RouteMetadata { + allRoutes := make(map[bgp.Route]RouteMetadata) if advertisements.Service != nil { c.addServiceRoutes(advertisements.Service, allRoutes) @@ -553,15 +571,15 @@ func (c *Controller) getRoutes(advertisements v1alpha1.Advertisements) (sets.Set c.addPodRoutes(allRoutes) } - return allRoutes, nil + return allRoutes } -func (c *Controller) addServiceRoutes(advertisement *v1alpha1.ServiceAdvertisement, allRoutes sets.Set[bgp.Route]) { +func (c *Controller) addServiceRoutes(advertisement *v1alpha1.ServiceAdvertisement, allRoutes map[bgp.Route]RouteMetadata) { ipTypes := sets.New(advertisement.IPTypes...) services, _ := c.serviceLister.List(labels.Everything()) - var serviceIPs []string for _, svc := range services { + svcRef := svc.Namespace + "/" + svc.Name internalLocal := svc.Spec.InternalTrafficPolicy != nil && *svc.Spec.InternalTrafficPolicy == corev1.ServiceInternalTrafficPolicyLocal externalLocal := svc.Spec.ExternalTrafficPolicy == corev1.ServiceExternalTrafficPolicyLocal var hasLocalEndpoints bool @@ -571,35 +589,41 @@ func (c *Controller) addServiceRoutes(advertisement *v1alpha1.ServiceAdvertiseme if ipTypes.Has(v1alpha1.ServiceIPTypeClusterIP) { if internalLocal && hasLocalEndpoints || !internalLocal { for _, clusterIP := range svc.Spec.ClusterIPs { - serviceIPs = append(serviceIPs, clusterIP) + if c.enabledIPv4 && utilnet.IsIPv4String(clusterIP) { + addRoutes(allRoutes, clusterIP+ipv4Suffix, svcRef, ServiceClusterIP) + } else if c.enabledIPv6 && utilnet.IsIPv6String(clusterIP) { + addRoutes(allRoutes, clusterIP+ipv6Suffix, svcRef, ServiceClusterIP) + } } } } if ipTypes.Has(v1alpha1.ServiceIPTypeExternalIP) { if externalLocal && hasLocalEndpoints || !externalLocal { for _, externalIP := range svc.Spec.ExternalIPs { - serviceIPs = append(serviceIPs, externalIP) + if c.enabledIPv4 && utilnet.IsIPv4String(externalIP) { + addRoutes(allRoutes, externalIP+ipv4Suffix, svcRef, ServiceExternalIP) + } else if c.enabledIPv6 && utilnet.IsIPv6String(externalIP) { + addRoutes(allRoutes, externalIP+ipv6Suffix, svcRef, ServiceExternalIP) + } } } } if ipTypes.Has(v1alpha1.ServiceIPTypeLoadBalancerIP) && svc.Spec.Type == corev1.ServiceTypeLoadBalancer { if externalLocal && hasLocalEndpoints || !externalLocal { - serviceIPs = append(serviceIPs, getIngressIPs(svc)...) + loadBalancerIPs := getIngressIPs(svc) + for _, loadBalancerIP := range loadBalancerIPs { + if c.enabledIPv4 && utilnet.IsIPv4String(loadBalancerIP) { + addRoutes(allRoutes, loadBalancerIP+ipv4Suffix, svcRef, ServiceLoadBalancerIP) + } else if c.enabledIPv6 && utilnet.IsIPv6String(loadBalancerIP) { + addRoutes(allRoutes, loadBalancerIP+ipv6Suffix, svcRef, ServiceLoadBalancerIP) + } + } } } } - - for _, ip := range serviceIPs { - if c.enabledIPv4 && utilnet.IsIPv4String(ip) { - allRoutes.Insert(bgp.Route{Prefix: ip + ipv4Suffix}) - } - if c.enabledIPv6 && utilnet.IsIPv6String(ip) { - allRoutes.Insert(bgp.Route{Prefix: ip + ipv6Suffix}) - } - } } -func (c *Controller) addEgressRoutes(allRoutes sets.Set[bgp.Route]) { +func (c *Controller) addEgressRoutes(allRoutes map[bgp.Route]RouteMetadata) { egresses, _ := c.egressLister.List(labels.Everything()) for _, eg := range egresses { if eg.Status.EgressNode != c.nodeName { @@ -607,20 +631,26 @@ func (c *Controller) addEgressRoutes(allRoutes sets.Set[bgp.Route]) { } ip := eg.Status.EgressIP if c.enabledIPv4 && utilnet.IsIPv4String(ip) { - allRoutes.Insert(bgp.Route{Prefix: ip + ipv4Suffix}) - } - if c.enabledIPv6 && utilnet.IsIPv6String(ip) { - allRoutes.Insert(bgp.Route{Prefix: ip + ipv6Suffix}) + addRoutes(allRoutes, ip+ipv4Suffix, eg.Name, EgressIP) + } else if c.enabledIPv6 && utilnet.IsIPv6String(ip) { + addRoutes(allRoutes, ip+ipv6Suffix, eg.Name, EgressIP) } } } -func (c *Controller) addPodRoutes(allRoutes sets.Set[bgp.Route]) { +func (c *Controller) addPodRoutes(allRoutes map[bgp.Route]RouteMetadata) { if c.enabledIPv4 { - allRoutes.Insert(bgp.Route{Prefix: c.podIPv4CIDR}) + addRoutes(allRoutes, c.podIPv4CIDR, "", NodeIPAMPodCIDR) } if c.enabledIPv6 { - allRoutes.Insert(bgp.Route{Prefix: c.podIPv6CIDR}) + addRoutes(allRoutes, c.podIPv6CIDR, "", NodeIPAMPodCIDR) + } +} + +func addRoutes(allRoutes map[bgp.Route]RouteMetadata, prefix, k8sObjRef string, routeType AdvertisedRouteType) { + allRoutes[bgp.Route{Prefix: prefix}] = RouteMetadata{ + Type: routeType, + K8sObjRef: k8sObjRef, } } @@ -1009,7 +1039,7 @@ func (c *Controller) GetBGPPeerStatus(ctx context.Context, ipv4Peers, ipv6Peers } // GetBGPRoutes returns the advertised BGP routes. -func (c *Controller) GetBGPRoutes(ctx context.Context, ipv4Routes, ipv6Routes bool) ([]string, error) { +func (c *Controller) GetBGPRoutes(ctx context.Context, ipv4Routes, ipv6Routes bool) (map[bgp.Route]RouteMetadata, error) { c.bgpPolicyStateMutex.RLock() defer c.bgpPolicyStateMutex.RUnlock() @@ -1017,19 +1047,12 @@ func (c *Controller) GetBGPRoutes(ctx context.Context, ipv4Routes, ipv6Routes bo return nil, ErrBGPPolicyNotFound } - bgpRoutes := make([]string, 0, c.bgpPolicyState.routes.Len()) - if ipv4Routes { // insert IPv4 advertised routes - for route := range c.bgpPolicyState.routes { - if utilnet.IsIPv4CIDRString(route.Prefix) { - bgpRoutes = append(bgpRoutes, route.Prefix) - } - } - } - if ipv6Routes { // insert IPv6 advertised routes - for route := range c.bgpPolicyState.routes { - if utilnet.IsIPv6CIDRString(route.Prefix) { - bgpRoutes = append(bgpRoutes, route.Prefix) - } + bgpRoutes := make(map[bgp.Route]RouteMetadata) + for route := range c.bgpPolicyState.routes { + if ipv4Routes && utilnet.IsIPv4CIDRString(route.Prefix) { + bgpRoutes[route] = c.bgpPolicyState.routes[route] + } else if ipv6Routes && utilnet.IsIPv6CIDRString(route.Prefix) { + bgpRoutes[route] = c.bgpPolicyState.routes[route] } } return bgpRoutes, nil diff --git a/pkg/agent/controller/bgp/controller_test.go b/pkg/agent/controller/bgp/controller_test.go index bb1cc2d4327..d275ab7c226 100644 --- a/pkg/agent/controller/bgp/controller_test.go +++ b/pkg/agent/controller/bgp/controller_test.go @@ -30,7 +30,6 @@ import ( "k8s.io/apimachinery/pkg/fields" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/rand" - "k8s.io/apimachinery/pkg/util/sets" "k8s.io/client-go/informers" coreinformers "k8s.io/client-go/informers/core/v1" "k8s.io/client-go/kubernetes/fake" @@ -55,9 +54,11 @@ const ( ) var ( - podIPv4CIDR = ip.MustParseCIDR("10.10.0.0/24") - podIPv6CIDR = ip.MustParseCIDR("fec0:10:10::/64") - nodeIPv4Addr = ip.MustParseCIDR("192.168.77.100/24") + podIPv4CIDR = ip.MustParseCIDR("10.10.0.0/24") + podIPv4CIDRRoute = bgp.Route{Prefix: podIPv4CIDR.String()} + podIPv6CIDR = ip.MustParseCIDR("fec0:10:10::/64") + podIPv6CIDRRoute = bgp.Route{Prefix: podIPv6CIDR.String()} + nodeIPv4Addr = ip.MustParseCIDR("192.168.77.100/24") testNodeConfig = &config.NodeConfig{ PodIPv4CIDR: podIPv4CIDR, @@ -127,10 +128,12 @@ var ( localNodeName = "local" node = generateNode(localNodeName, nodeLabels1, nodeAnnotations1) - ipv4EgressIP1 = "192.168.77.200" - ipv6EgressIP1 = "fec0::192:168:77:200" - ipv4EgressIP2 = "192.168.77.201" - ipv6EgressIP2 = "fec0::192:168:77:2001" + ipv4EgressIP1 = "192.168.77.200" + ipv4EgressIP1Route = bgp.Route{Prefix: ipStrToPrefix(ipv4EgressIP1)} + ipv6EgressIP1 = "fec0::192:168:77:200" + ipv6EgressIP1Route = bgp.Route{Prefix: ipStrToPrefix(ipv6EgressIP1)} + ipv4EgressIP2 = "192.168.77.201" + ipv6EgressIP2 = "fec0::192:168:77:2001" ipv4Egress1 = generateEgress("eg1-4", ipv4EgressIP1, localNodeName) ipv6Egress1 = generateEgress("eg1-6", ipv6EgressIP1, localNodeName) @@ -147,12 +150,12 @@ var ( creationTimestampAdd2s = metav1.NewTime(creationTimestamp.Add(2 * time.Second)) creationTimestampAdd3s = metav1.NewTime(creationTimestamp.Add(3 * time.Second)) - clusterIPv4 = "10.96.10.10" - externalIPv4 = "192.168.77.100" + clusterIPv4s = []string{"10.96.10.10", "10.96.10.11"} + externalIPv4s = []string{"192.168.77.100", "192.168.77.101"} loadBalancerIPv4 = "192.168.77.150" endpointIPv4 = "10.10.0.10" - clusterIPv6 = "fec0::10:96:10:10" - externalIPv6 = "fec0::192:168:77:100" + clusterIPv6s = []string{"fec0::10:96:10:10", "fec0::10:96:10:11"} + externalIPv6s = []string{"fec0::192:168:77:100", "fec0::192:168:77:101"} loadBalancerIPv6 = "fec0::192:168:77:150" endpointIPv6 = "fec0::10:10:0:10" @@ -163,20 +166,48 @@ var ( ipv4LoadBalancerName = "loadbalancer-4" ipv6LoadBalancerName = "loadbalancer-6" + clusterIPv4Route1 = bgp.Route{Prefix: ipStrToPrefix(clusterIPv4s[0])} + clusterIPv6Route1 = bgp.Route{Prefix: ipStrToPrefix(clusterIPv6s[0])} + clusterIPv4Route2 = bgp.Route{Prefix: ipStrToPrefix(clusterIPv4s[1])} + clusterIPv6Route2 = bgp.Route{Prefix: ipStrToPrefix(clusterIPv6s[1])} + externalIPv4Route1 = bgp.Route{Prefix: ipStrToPrefix(externalIPv4s[0])} + externalIPv6Route1 = bgp.Route{Prefix: ipStrToPrefix(externalIPv6s[0])} + externalIPv4Route2 = bgp.Route{Prefix: ipStrToPrefix(externalIPv4s[1])} + externalIPv6Route2 = bgp.Route{Prefix: ipStrToPrefix(externalIPv6s[1])} + loadBalancerIPv4Route = bgp.Route{Prefix: ipStrToPrefix(loadBalancerIPv4)} + loadBalancerIPv6Route = bgp.Route{Prefix: ipStrToPrefix(loadBalancerIPv6)} + + allRoutes = map[bgp.Route]RouteMetadata{ + clusterIPv4Route1: {Type: ServiceClusterIP, K8sObjRef: getServiceName(ipv4ClusterIPName1)}, + clusterIPv6Route1: {Type: ServiceClusterIP, K8sObjRef: getServiceName(ipv6ClusterIPName1)}, + clusterIPv4Route2: {Type: ServiceClusterIP, K8sObjRef: getServiceName(ipv4LoadBalancerName)}, + clusterIPv6Route2: {Type: ServiceClusterIP, K8sObjRef: getServiceName(ipv6LoadBalancerName)}, + externalIPv4Route1: {Type: ServiceExternalIP, K8sObjRef: getServiceName(ipv4ClusterIPName1)}, + externalIPv6Route1: {Type: ServiceExternalIP, K8sObjRef: getServiceName(ipv6ClusterIPName1)}, + externalIPv4Route2: {Type: ServiceExternalIP, K8sObjRef: getServiceName(ipv4LoadBalancerName)}, + externalIPv6Route2: {Type: ServiceExternalIP, K8sObjRef: getServiceName(ipv6LoadBalancerName)}, + loadBalancerIPv4Route: {Type: ServiceLoadBalancerIP, K8sObjRef: getServiceName(ipv4LoadBalancerName)}, + loadBalancerIPv6Route: {Type: ServiceLoadBalancerIP, K8sObjRef: getServiceName(ipv6LoadBalancerName)}, + ipv4EgressIP1Route: {Type: EgressIP, K8sObjRef: "eg1-4"}, + ipv6EgressIP1Route: {Type: EgressIP, K8sObjRef: "eg1-6"}, + podIPv4CIDRRoute: {Type: NodeIPAMPodCIDR}, + podIPv6CIDRRoute: {Type: NodeIPAMPodCIDR}, + } + endpointSliceSuffix = rand.String(5) - ipv4ClusterIP1 = generateService(ipv4ClusterIPName1, corev1.ServiceTypeClusterIP, clusterIPv4, externalIPv4, "", false, false) + ipv4ClusterIP1 = generateService(ipv4ClusterIPName1, corev1.ServiceTypeClusterIP, clusterIPv4s[0], externalIPv4s[0], "", false, false) ipv4ClusterIP1Eps = generateEndpointSlice(ipv4ClusterIPName1, endpointSliceSuffix, false, false, endpointIPv4) - ipv4ClusterIP2 = generateService(ipv4ClusterIPName2, corev1.ServiceTypeClusterIP, clusterIPv4, externalIPv4, "", true, true) + ipv4ClusterIP2 = generateService(ipv4ClusterIPName2, corev1.ServiceTypeClusterIP, clusterIPv4s[0], externalIPv4s[0], "", true, true) ipv4ClusterIP2Eps = generateEndpointSlice(ipv4ClusterIPName2, endpointSliceSuffix, false, false, endpointIPv4) - ipv6ClusterIP1 = generateService(ipv6ClusterIPName1, corev1.ServiceTypeClusterIP, clusterIPv6, externalIPv6, "", false, false) + ipv6ClusterIP1 = generateService(ipv6ClusterIPName1, corev1.ServiceTypeClusterIP, clusterIPv6s[0], externalIPv6s[0], "", false, false) ipv6ClusterIP1Eps = generateEndpointSlice(ipv6ClusterIPName1, endpointSliceSuffix, false, false, endpointIPv6) - ipv6ClusterIP2 = generateService(ipv6ClusterIPName2, corev1.ServiceTypeClusterIP, clusterIPv6, externalIPv6, "", true, true) + ipv6ClusterIP2 = generateService(ipv6ClusterIPName2, corev1.ServiceTypeClusterIP, clusterIPv6s[0], externalIPv6s[0], "", true, true) ipv6ClusterIP2Eps = generateEndpointSlice(ipv6ClusterIPName2, endpointSliceSuffix, false, false, endpointIPv6) - ipv4LoadBalancer = generateService(ipv4LoadBalancerName, corev1.ServiceTypeLoadBalancer, clusterIPv4, externalIPv4, loadBalancerIPv4, false, false) + ipv4LoadBalancer = generateService(ipv4LoadBalancerName, corev1.ServiceTypeLoadBalancer, clusterIPv4s[1], externalIPv4s[1], loadBalancerIPv4, false, false) ipv4LoadBalancerEps = generateEndpointSlice(ipv4LoadBalancerName, endpointSliceSuffix, false, false, endpointIPv4) - ipv6LoadBalancer = generateService(ipv6LoadBalancerName, corev1.ServiceTypeLoadBalancer, clusterIPv6, externalIPv6, loadBalancerIPv6, false, false) + ipv6LoadBalancer = generateService(ipv6LoadBalancerName, corev1.ServiceTypeLoadBalancer, clusterIPv6s[1], externalIPv6s[1], loadBalancerIPv6, false, false) ipv6LoadBalancerEps = generateEndpointSlice(ipv6LoadBalancerName, endpointSliceSuffix, false, false, endpointIPv6) bgpPeerPasswords = map[string]string{ @@ -287,13 +318,13 @@ func TestBGPPolicyAdd(t *testing.T) { 179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{ipStrToPrefix(clusterIPv4)}, + []bgp.Route{clusterIPv4Route1}, []bgp.PeerConfig{ipv4Peer1Config}, ), expectedCalls: func(mockBGPServer *bgptest.MockInterfaceMockRecorder) { mockBGPServer.Start(gomock.Any()) mockBGPServer.AddPeer(gomock.Any(), ipv4Peer1Config) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(clusterIPv4)}}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{clusterIPv4Route1}) }, }, { @@ -319,13 +350,13 @@ func TestBGPPolicyAdd(t *testing.T) { 179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{ipStrToPrefix(externalIPv6)}, + []bgp.Route{externalIPv6Route1}, []bgp.PeerConfig{ipv6Peer1Config}, ), expectedCalls: func(mockBGPServer *bgptest.MockInterfaceMockRecorder) { mockBGPServer.Start(gomock.Any()) mockBGPServer.AddPeer(gomock.Any(), ipv6Peer1Config) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(externalIPv6)}}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{externalIPv6Route1}) }, }, { @@ -354,15 +385,15 @@ func TestBGPPolicyAdd(t *testing.T) { 179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{ipStrToPrefix(loadBalancerIPv4), ipStrToPrefix(loadBalancerIPv6)}, + []bgp.Route{loadBalancerIPv4Route, loadBalancerIPv6Route}, []bgp.PeerConfig{ipv4Peer1Config, ipv6Peer1Config}, ), expectedCalls: func(mockBGPServer *bgptest.MockInterfaceMockRecorder) { mockBGPServer.Start(gomock.Any()) mockBGPServer.AddPeer(gomock.Any(), ipv4Peer1Config) mockBGPServer.AddPeer(gomock.Any(), ipv6Peer1Config) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(loadBalancerIPv4)}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(loadBalancerIPv6)}}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{loadBalancerIPv4Route}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{loadBalancerIPv6Route}) }, }, { @@ -388,13 +419,13 @@ func TestBGPPolicyAdd(t *testing.T) { 179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{ipStrToPrefix(ipv4EgressIP1)}, + []bgp.Route{ipv4EgressIP1Route}, []bgp.PeerConfig{ipv4Peer1Config}, ), expectedCalls: func(mockBGPServer *bgptest.MockInterfaceMockRecorder) { mockBGPServer.Start(gomock.Any()) mockBGPServer.AddPeer(gomock.Any(), ipv4Peer1Config) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(ipv4EgressIP1)}}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{ipv4EgressIP1Route}) }, }, { @@ -416,13 +447,13 @@ func TestBGPPolicyAdd(t *testing.T) { 179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{podIPv6CIDR.String()}, + []bgp.Route{podIPv6CIDRRoute}, []bgp.PeerConfig{ipv6Peer1Config}, ), expectedCalls: func(mockBGPServer *bgptest.MockInterfaceMockRecorder) { mockBGPServer.Start(gomock.Any()) mockBGPServer.AddPeer(gomock.Any(), ipv6Peer1Config) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: podIPv6CIDR.String()}}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{podIPv6CIDRRoute}) }, }, { @@ -490,14 +521,14 @@ func TestBGPPolicyAdd(t *testing.T) { 179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{ipStrToPrefix(clusterIPv4)}, + []bgp.Route{clusterIPv4Route1}, []bgp.PeerConfig{ipv4Peer1Config}, ), expectedState: generateBGPPolicyState(bgpPolicyName2, 179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{ipStrToPrefix(clusterIPv4)}, + []bgp.Route{clusterIPv4Route1}, []bgp.PeerConfig{ipv4Peer1Config}, ), }, @@ -555,13 +586,7 @@ func TestBGPPolicyUpdate(t *testing.T) { 179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{ipStrToPrefix(clusterIPv4), - ipStrToPrefix(clusterIPv6), - ipStrToPrefix(loadBalancerIPv4), - ipStrToPrefix(loadBalancerIPv6), - podIPv4CIDR.String(), - podIPv6CIDR.String(), - }, + []bgp.Route{clusterIPv4Route2, clusterIPv6Route2, loadBalancerIPv4Route, loadBalancerIPv6Route, podIPv4CIDRRoute, podIPv6CIDRRoute}, []bgp.PeerConfig{ipv4Peer1Config, ipv6Peer1Config, ipv4Peer2Config, @@ -649,23 +674,24 @@ func TestBGPPolicyUpdate(t *testing.T) { mockBGPServer.AddPeer(gomock.Any(), ipv4Peer2Config) mockBGPServer.AddPeer(gomock.Any(), ipv6Peer1Config) mockBGPServer.AddPeer(gomock.Any(), ipv6Peer2Config) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(clusterIPv4)}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(clusterIPv6)}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(loadBalancerIPv4)}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(loadBalancerIPv6)}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: podIPv4CIDR.String()}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: podIPv6CIDR.String()}}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{clusterIPv4Route2}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{clusterIPv6Route2}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{loadBalancerIPv4Route}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{loadBalancerIPv6Route}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{podIPv4CIDRRoute}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{podIPv6CIDRRoute}) }, expectedState: generateBGPPolicyState(bgpPolicyName2, 1179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{ipStrToPrefix(clusterIPv4), - ipStrToPrefix(clusterIPv6), - ipStrToPrefix(loadBalancerIPv4), - ipStrToPrefix(loadBalancerIPv6), - podIPv4CIDR.String(), - podIPv6CIDR.String(), + []bgp.Route{ + clusterIPv4Route2, + clusterIPv6Route2, + loadBalancerIPv4Route, + loadBalancerIPv6Route, + podIPv4CIDRRoute, + podIPv6CIDRRoute, }, []bgp.PeerConfig{ipv4Peer1Config, ipv6Peer1Config, @@ -718,10 +744,11 @@ func TestBGPPolicyUpdate(t *testing.T) { 179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{ipStrToPrefix(externalIPv4), - ipStrToPrefix(externalIPv6), - ipStrToPrefix(ipv4EgressIP1), - ipStrToPrefix(ipv6EgressIP1), + []bgp.Route{ + externalIPv4Route2, + externalIPv6Route2, + ipv4EgressIP1Route, + ipv6EgressIP1Route, }, []bgp.PeerConfig{ipv4Peer1Config, ipv6Peer1Config, @@ -730,17 +757,17 @@ func TestBGPPolicyUpdate(t *testing.T) { }, ), expectedCalls: func(mockBGPServer *bgptest.MockInterfaceMockRecorder) { - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(externalIPv4)}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(ipv4EgressIP1)}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(externalIPv6)}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(ipv6EgressIP1)}}) - - mockBGPServer.WithdrawRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(clusterIPv4)}}) - mockBGPServer.WithdrawRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(loadBalancerIPv4)}}) - mockBGPServer.WithdrawRoutes(gomock.Any(), []bgp.Route{{Prefix: podIPv4CIDR.String()}}) - mockBGPServer.WithdrawRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(clusterIPv6)}}) - mockBGPServer.WithdrawRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(loadBalancerIPv6)}}) - mockBGPServer.WithdrawRoutes(gomock.Any(), []bgp.Route{{Prefix: podIPv6CIDR.String()}}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{externalIPv4Route2}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{ipv4EgressIP1Route}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{externalIPv6Route2}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{ipv6EgressIP1Route}) + + mockBGPServer.WithdrawRoutes(gomock.Any(), []bgp.Route{clusterIPv4Route2}) + mockBGPServer.WithdrawRoutes(gomock.Any(), []bgp.Route{loadBalancerIPv4Route}) + mockBGPServer.WithdrawRoutes(gomock.Any(), []bgp.Route{podIPv4CIDRRoute}) + mockBGPServer.WithdrawRoutes(gomock.Any(), []bgp.Route{clusterIPv6Route2}) + mockBGPServer.WithdrawRoutes(gomock.Any(), []bgp.Route{loadBalancerIPv6Route}) + mockBGPServer.WithdrawRoutes(gomock.Any(), []bgp.Route{podIPv6CIDRRoute}) }, }, { @@ -764,10 +791,11 @@ func TestBGPPolicyUpdate(t *testing.T) { 179, 65001, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{ipStrToPrefix(externalIPv4), - ipStrToPrefix(externalIPv6), - ipStrToPrefix(ipv4EgressIP1), - ipStrToPrefix(ipv6EgressIP1), + []bgp.Route{ + externalIPv4Route2, + externalIPv6Route2, + ipv4EgressIP1Route, + ipv6EgressIP1Route, }, []bgp.PeerConfig{ipv4Peer1Config, ipv6Peer1Config, @@ -782,10 +810,10 @@ func TestBGPPolicyUpdate(t *testing.T) { mockBGPServer.AddPeer(gomock.Any(), ipv4Peer2Config) mockBGPServer.AddPeer(gomock.Any(), ipv6Peer1Config) mockBGPServer.AddPeer(gomock.Any(), ipv6Peer2Config) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(externalIPv4)}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(ipv4EgressIP1)}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(externalIPv6)}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(ipv6EgressIP1)}}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{externalIPv4Route2}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{ipv4EgressIP1Route}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{externalIPv6Route2}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{ipv6EgressIP1Route}) }, }, { @@ -809,12 +837,13 @@ func TestBGPPolicyUpdate(t *testing.T) { 1179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{ipStrToPrefix(clusterIPv4), - ipStrToPrefix(clusterIPv6), - ipStrToPrefix(loadBalancerIPv4), - ipStrToPrefix(loadBalancerIPv6), - podIPv4CIDR.String(), - podIPv6CIDR.String(), + []bgp.Route{ + clusterIPv4Route2, + clusterIPv6Route2, + loadBalancerIPv4Route, + loadBalancerIPv6Route, + podIPv4CIDRRoute, + podIPv6CIDRRoute, }, []bgp.PeerConfig{ipv4Peer1Config, ipv6Peer1Config, @@ -829,12 +858,12 @@ func TestBGPPolicyUpdate(t *testing.T) { mockBGPServer.AddPeer(gomock.Any(), ipv4Peer2Config) mockBGPServer.AddPeer(gomock.Any(), ipv6Peer1Config) mockBGPServer.AddPeer(gomock.Any(), ipv6Peer2Config) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(clusterIPv4)}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(loadBalancerIPv4)}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: podIPv4CIDR.String()}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(clusterIPv6)}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(loadBalancerIPv6)}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: podIPv6CIDR.String()}}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{clusterIPv4Route2}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{loadBalancerIPv4Route}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{podIPv4CIDRRoute}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{clusterIPv6Route2}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{loadBalancerIPv6Route}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{podIPv6CIDRRoute}) }, }, { @@ -857,12 +886,13 @@ func TestBGPPolicyUpdate(t *testing.T) { 179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{ipStrToPrefix(clusterIPv4), - ipStrToPrefix(clusterIPv6), - ipStrToPrefix(loadBalancerIPv4), - ipStrToPrefix(loadBalancerIPv6), - podIPv4CIDR.String(), - podIPv6CIDR.String(), + []bgp.Route{ + clusterIPv4Route2, + clusterIPv6Route2, + loadBalancerIPv4Route, + loadBalancerIPv6Route, + podIPv4CIDRRoute, + podIPv6CIDRRoute, }, []bgp.PeerConfig{updatedIPv4Peer2Config, updatedIPv6Peer2Config, @@ -981,10 +1011,7 @@ func TestBGPPolicyDelete(t *testing.T) { 179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{ - ipStrToPrefix(loadBalancerIPv4), - ipStrToPrefix(loadBalancerIPv6), - }, + []bgp.Route{loadBalancerIPv4Route, loadBalancerIPv6Route}, []bgp.PeerConfig{ ipv4Peer1Config, ipv6Peer1Config, @@ -1008,10 +1035,7 @@ func TestBGPPolicyDelete(t *testing.T) { 179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{ - ipStrToPrefix(externalIPv4), - ipStrToPrefix(externalIPv6), - }, + []bgp.Route{externalIPv4Route2, externalIPv6Route2}, []bgp.PeerConfig{ ipv4Peer2Config, ipv6Peer2Config}, @@ -1034,10 +1058,7 @@ func TestBGPPolicyDelete(t *testing.T) { 1179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{ - ipStrToPrefix(externalIPv4), - ipStrToPrefix(externalIPv6), - }, + []bgp.Route{externalIPv4Route2, externalIPv6Route2}, []bgp.PeerConfig{ ipv4Peer2Config, ipv6Peer2Config}, @@ -1078,10 +1099,10 @@ func TestBGPPolicyDelete(t *testing.T) { mockBGPServer.RemovePeer(gomock.Any(), ipv6Peer1Config) mockBGPServer.AddPeer(gomock.Any(), ipv4Peer2Config) mockBGPServer.AddPeer(gomock.Any(), ipv6Peer2Config) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(externalIPv4)}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(externalIPv6)}}) - mockBGPServer.WithdrawRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(loadBalancerIPv4)}}) - mockBGPServer.WithdrawRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(loadBalancerIPv6)}}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{externalIPv4Route2}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{externalIPv6Route2}) + mockBGPServer.WithdrawRoutes(gomock.Any(), []bgp.Route{loadBalancerIPv4Route}) + mockBGPServer.WithdrawRoutes(gomock.Any(), []bgp.Route{loadBalancerIPv6Route}) }, }, { @@ -1095,8 +1116,8 @@ func TestBGPPolicyDelete(t *testing.T) { mockBGPServer.Start(gomock.Any()) mockBGPServer.AddPeer(gomock.Any(), ipv4Peer2Config) mockBGPServer.AddPeer(gomock.Any(), ipv6Peer2Config) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(externalIPv4)}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(externalIPv6)}}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{externalIPv4Route2}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{externalIPv6Route2}) }, }, { @@ -1158,7 +1179,7 @@ func TestNodeUpdate(t *testing.T) { 179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{podIPv4CIDR.String(), podIPv6CIDR.String()}, + []bgp.Route{podIPv4CIDRRoute, podIPv6CIDRRoute}, []bgp.PeerConfig{ipv4Peer1Config, ipv6Peer1Config}) policy2 := generateBGPPolicy(bgpPolicyName2, creationTimestampAdd1s, @@ -1175,7 +1196,7 @@ func TestNodeUpdate(t *testing.T) { 1179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{podIPv4CIDR.String(), podIPv6CIDR.String()}, + []bgp.Route{podIPv4CIDRRoute, podIPv6CIDRRoute}, []bgp.PeerConfig{ipv4Peer1Config, ipv6Peer1Config}) policy3 := generateBGPPolicy(bgpPolicyName3, creationTimestampAdd2s, @@ -1234,8 +1255,8 @@ func TestNodeUpdate(t *testing.T) { mockBGPServer.Stop(gomock.Any()) mockBGPServer.AddPeer(gomock.Any(), ipv4Peer1Config) mockBGPServer.AddPeer(gomock.Any(), ipv6Peer1Config) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: podIPv4CIDR.String()}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: podIPv6CIDR.String()}}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{podIPv4CIDRRoute}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{podIPv6CIDRRoute}) }, }, { @@ -1260,15 +1281,15 @@ func TestNodeUpdate(t *testing.T) { 179, 65000, nodeAnnotations2[types.NodeBGPRouterIDAnnotationKey], - []string{podIPv4CIDR.String(), podIPv6CIDR.String()}, + []bgp.Route{podIPv4CIDRRoute, podIPv6CIDRRoute}, []bgp.PeerConfig{ipv4Peer1Config, ipv6Peer1Config}), expectedCalls: func(mockBGPServer *bgptest.MockInterfaceMockRecorder) { mockBGPServer.Start(gomock.Any()) mockBGPServer.Stop(gomock.Any()) mockBGPServer.AddPeer(gomock.Any(), ipv4Peer1Config) mockBGPServer.AddPeer(gomock.Any(), ipv6Peer1Config) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: podIPv4CIDR.String()}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: podIPv6CIDR.String()}}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{podIPv4CIDRRoute}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{podIPv6CIDRRoute}) }, }, { @@ -1282,15 +1303,15 @@ func TestNodeUpdate(t *testing.T) { 179, 65000, nodeIPv4Addr.IP.String(), - []string{podIPv4CIDR.String(), podIPv6CIDR.String()}, + []bgp.Route{podIPv4CIDRRoute, podIPv6CIDRRoute}, []bgp.PeerConfig{ipv4Peer1Config, ipv6Peer1Config}), expectedCalls: func(mockBGPServer *bgptest.MockInterfaceMockRecorder) { mockBGPServer.Start(gomock.Any()) mockBGPServer.Stop(gomock.Any()) mockBGPServer.AddPeer(gomock.Any(), ipv4Peer1Config) mockBGPServer.AddPeer(gomock.Any(), ipv6Peer1Config) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: podIPv4CIDR.String()}}) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: podIPv6CIDR.String()}}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{podIPv4CIDRRoute}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{podIPv6CIDRRoute}) }, }, { @@ -1302,19 +1323,19 @@ func TestNodeUpdate(t *testing.T) { 179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{podIPv6CIDR.String()}, + []bgp.Route{podIPv6CIDRRoute}, []bgp.PeerConfig{ipv6Peer1Config}), expectedState: generateBGPPolicyState(bgpPolicyName1, 179, 65000, nodeAnnotations2[types.NodeBGPRouterIDAnnotationKey], - []string{podIPv6CIDR.String()}, + []bgp.Route{podIPv6CIDRRoute}, []bgp.PeerConfig{ipv6Peer1Config}), expectedCalls: func(mockBGPServer *bgptest.MockInterfaceMockRecorder) { mockBGPServer.Start(gomock.Any()) mockBGPServer.Stop(gomock.Any()) mockBGPServer.AddPeer(gomock.Any(), ipv6Peer1Config) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: podIPv6CIDR.String()}}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{podIPv6CIDRRoute}) }, }, { @@ -1326,19 +1347,21 @@ func TestNodeUpdate(t *testing.T) { 179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{podIPv6CIDR.String()}, + []bgp.Route{ + podIPv6CIDRRoute, + }, []bgp.PeerConfig{ipv6Peer1Config}), expectedState: generateBGPPolicyState(bgpPolicyName1, 179, 65000, "156.67.103.8", - []string{podIPv6CIDR.String()}, + []bgp.Route{podIPv6CIDRRoute}, []bgp.PeerConfig{ipv6Peer1Config}), expectedCalls: func(mockBGPServer *bgptest.MockInterfaceMockRecorder) { mockBGPServer.Start(gomock.Any()) mockBGPServer.Stop(gomock.Any()) mockBGPServer.AddPeer(gomock.Any(), ipv6Peer1Config) - mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: podIPv6CIDR.String()}}) + mockBGPServer.AdvertiseRoutes(gomock.Any(), []bgp.Route{podIPv6CIDRRoute}) }, }, } @@ -1644,10 +1667,7 @@ func TestBGPSecretUpdate(t *testing.T) { require.Eventually(t, func() bool { c.bgpPeerPasswordsMutex.RLock() defer c.bgpPeerPasswordsMutex.RUnlock() - if reflect.DeepEqual(c.bgpPeerPasswords, bgpPeerPasswords) { - return true - } - return false + return reflect.DeepEqual(c.bgpPeerPasswords, bgpPeerPasswords) }, 5*time.Second, 10*time.Millisecond) // Wait for the dummy event triggered by BGPPolicy add events. @@ -1656,7 +1676,7 @@ func TestBGPSecretUpdate(t *testing.T) { mockBGPServer.EXPECT().AddPeer(gomock.Any(), ipv4Peer1Config) mockBGPServer.EXPECT().AddPeer(gomock.Any(), ipv4Peer2Config) mockBGPServer.EXPECT().AddPeer(gomock.Any(), ipv4Peer3Config) - mockBGPServer.EXPECT().AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: podIPv4CIDR.String()}}) + mockBGPServer.EXPECT().AdvertiseRoutes(gomock.Any(), []bgp.Route{podIPv4CIDRRoute}) require.NoError(t, c.syncBGPPolicy(ctx)) // Done with the dummy event. doneDummyEvent(t, c) @@ -1673,10 +1693,7 @@ func TestBGPSecretUpdate(t *testing.T) { require.Eventually(t, func() bool { c.bgpPeerPasswordsMutex.RLock() defer c.bgpPeerPasswordsMutex.RUnlock() - if reflect.DeepEqual(c.bgpPeerPasswords, updatedBGPPeerPasswords) { - return true - } - return false + return reflect.DeepEqual(c.bgpPeerPasswords, updatedBGPPeerPasswords) }, 5*time.Second, 10*time.Millisecond) // Wait for the dummy event triggered by Secret update event, and mark it done. @@ -1767,7 +1784,7 @@ func TestSyncBGPPolicyFailures(t *testing.T) { mockBGPServer.EXPECT().Start(gomock.Any()) mockBGPServer.EXPECT().AddPeer(gomock.Any(), ipv4Peer2Config) - mockBGPServer.EXPECT().AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(loadBalancerIPv4)}}) + mockBGPServer.EXPECT().AdvertiseRoutes(gomock.Any(), []bgp.Route{loadBalancerIPv4Route}) require.NoError(t, c.syncBGPPolicy(ctx)) // Done with the dummy event. doneDummyEvent(t, c) @@ -1776,7 +1793,7 @@ func TestSyncBGPPolicyFailures(t *testing.T) { 179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{ipStrToPrefix(loadBalancerIPv4)}, + []bgp.Route{loadBalancerIPv4Route}, []bgp.PeerConfig{ipv4Peer2Config}), c.bgpPolicyState) @@ -1793,7 +1810,7 @@ func TestSyncBGPPolicyFailures(t *testing.T) { 179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{ipStrToPrefix(loadBalancerIPv4)}, + []bgp.Route{loadBalancerIPv4Route}, []bgp.PeerConfig{ipv4Peer2Config}), c.bgpPolicyState) @@ -1811,13 +1828,13 @@ func TestSyncBGPPolicyFailures(t *testing.T) { 1179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{}, + []bgp.Route{}, []bgp.PeerConfig{}), c.bgpPolicyState) // Mock the retry. Add the BGP peer successfully, but fail in advertising routes. mockBGPServer.EXPECT().AddPeer(gomock.Any(), ipv4Peer1Config) - mockBGPServer.EXPECT().AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: podIPv4CIDR.String()}}).Return(fmt.Errorf("failed to advertise routes")) + mockBGPServer.EXPECT().AdvertiseRoutes(gomock.Any(), []bgp.Route{podIPv4CIDRRoute}).Return(fmt.Errorf("failed to advertise routes")) require.EqualError(t, c.syncBGPPolicy(ctx), "failed to advertise routes") // Done with the dummy event. doneDummyEvent(t, c) @@ -1825,7 +1842,7 @@ func TestSyncBGPPolicyFailures(t *testing.T) { 1179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{}, + []bgp.Route{}, []bgp.PeerConfig{ipv4Peer1Config}), c.bgpPolicyState) @@ -1836,7 +1853,7 @@ func TestSyncBGPPolicyFailures(t *testing.T) { waitAndGetDummyEvent(t, c) mockBGPServer.EXPECT().AddPeer(gomock.Any(), ipv4Peer2Config) mockBGPServer.EXPECT().RemovePeer(gomock.Any(), ipv4Peer1Config) - mockBGPServer.EXPECT().AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(externalIPv4)}}) + mockBGPServer.EXPECT().AdvertiseRoutes(gomock.Any(), []bgp.Route{externalIPv4Route2}) require.NoError(t, c.syncBGPPolicy(ctx)) doneDummyEvent(t, c) @@ -1844,7 +1861,7 @@ func TestSyncBGPPolicyFailures(t *testing.T) { 1179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{ipStrToPrefix(externalIPv4)}, + []bgp.Route{externalIPv4Route2}, []bgp.PeerConfig{ipv4Peer2Config}), c.bgpPolicyState) @@ -1854,8 +1871,8 @@ func TestSyncBGPPolicyFailures(t *testing.T) { waitAndGetDummyEvent(t, c) mockBGPServer.EXPECT().UpdatePeer(gomock.Any(), updatedIPv4Peer2Config) - mockBGPServer.EXPECT().WithdrawRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(externalIPv4)}}) - mockBGPServer.EXPECT().AdvertiseRoutes(gomock.Any(), []bgp.Route{{Prefix: ipStrToPrefix(loadBalancerIPv4)}}) + mockBGPServer.EXPECT().WithdrawRoutes(gomock.Any(), []bgp.Route{externalIPv4Route2}) + mockBGPServer.EXPECT().AdvertiseRoutes(gomock.Any(), []bgp.Route{loadBalancerIPv4Route}) require.NoError(t, c.syncBGPPolicy(ctx)) doneDummyEvent(t, c) @@ -1863,7 +1880,7 @@ func TestSyncBGPPolicyFailures(t *testing.T) { 1179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{ipStrToPrefix(loadBalancerIPv4)}, + []bgp.Route{loadBalancerIPv4Route}, []bgp.PeerConfig{updatedIPv4Peer2Config}), c.bgpPolicyState) } @@ -1872,12 +1889,12 @@ func generateBGPPolicyState(bgpPolicyName string, listenPort int32, localASN int32, routerID string, - prefixes []string, + bgpRoutes []bgp.Route, peerConfigs []bgp.PeerConfig) *bgpPolicyState { - routes := sets.New[bgp.Route]() + routes := map[bgp.Route]RouteMetadata{} peerConfigMap := make(map[string]bgp.PeerConfig) - for _, prefix := range prefixes { - routes.Insert(bgp.Route{Prefix: prefix}) + for _, route := range bgpRoutes { + routes[route] = allRoutes[route] } for _, peerConfig := range peerConfigs { peerKey := generateBGPPeerKey(peerConfig.Address, peerConfig.ASN) @@ -1899,13 +1916,17 @@ func deepCopyBGPPolicyState(in *bgpPolicyState) *bgpPolicyState { peerKey := generateBGPPeerKey(peerConfig.Address, peerConfig.ASN) peerConfigMap[peerKey] = peerConfig } + routes := make(map[bgp.Route]RouteMetadata) + for routeType := range in.routes { + routes[routeType] = in.routes[routeType] + } return &bgpPolicyState{ bgpPolicyName: in.bgpPolicyName, listenPort: in.listenPort, localASN: in.localASN, routerID: in.routerID, - routes: in.routes.Union(nil), + routes: routes, peerConfigs: peerConfigMap, } } @@ -2137,6 +2158,9 @@ func waitAndGetDummyEvent(t *testing.T, c *fakeController) { func doneDummyEvent(t *testing.T, c *fakeController) { c.queue.Done(dummyKey) } +func getServiceName(name string) string { + return namespaceDefault + "/" + name +} func TestGetBGPPolicyInfo(t *testing.T) { testCases := []struct { @@ -2259,13 +2283,8 @@ func TestGetBGPRoutes(t *testing.T) { 179, 65000, nodeAnnotations1[types.NodeBGPRouterIDAnnotationKey], - []string{ipStrToPrefix(clusterIPv4), - ipStrToPrefix(loadBalancerIPv4), - podIPv4CIDR.String(), - ipStrToPrefix(clusterIPv6), - ipStrToPrefix(loadBalancerIPv6), - podIPv6CIDR.String(), - }, + []bgp.Route{clusterIPv4Route1, clusterIPv6Route1, loadBalancerIPv4Route, + loadBalancerIPv6Route, podIPv4CIDRRoute, podIPv6CIDRRoute}, []bgp.PeerConfig{}, ) ctx := context.Background() @@ -2274,7 +2293,7 @@ func TestGetBGPRoutes(t *testing.T) { ipv4Routes bool ipv6Routes bool existingState *bgpPolicyState - expectedBgpRoutes []string + expectedBgpRoutes map[bgp.Route]RouteMetadata expectedErr string }{ { @@ -2282,33 +2301,33 @@ func TestGetBGPRoutes(t *testing.T) { ipv4Routes: true, ipv6Routes: true, existingState: effectivePolicyState, - expectedBgpRoutes: []string{ - ipStrToPrefix(clusterIPv4), - ipStrToPrefix(loadBalancerIPv4), - podIPv4CIDR.String(), - ipStrToPrefix(clusterIPv6), - ipStrToPrefix(loadBalancerIPv6), - podIPv6CIDR.String(), + expectedBgpRoutes: map[bgp.Route]RouteMetadata{ + clusterIPv4Route1: allRoutes[clusterIPv4Route1], + clusterIPv6Route1: allRoutes[clusterIPv6Route1], + loadBalancerIPv4Route: allRoutes[loadBalancerIPv4Route], + loadBalancerIPv6Route: allRoutes[loadBalancerIPv6Route], + podIPv4CIDRRoute: allRoutes[podIPv4CIDRRoute], + podIPv6CIDRRoute: allRoutes[podIPv6CIDRRoute], }, }, { name: "get advertised ipv4 routes only", ipv4Routes: true, existingState: effectivePolicyState, - expectedBgpRoutes: []string{ - ipStrToPrefix(clusterIPv4), - ipStrToPrefix(loadBalancerIPv4), - podIPv4CIDR.String(), + expectedBgpRoutes: map[bgp.Route]RouteMetadata{ + clusterIPv4Route1: allRoutes[clusterIPv4Route1], + loadBalancerIPv4Route: allRoutes[loadBalancerIPv4Route], + podIPv4CIDRRoute: allRoutes[podIPv4CIDRRoute], }, }, { name: "get advertised ipv6 routes only", ipv6Routes: true, existingState: effectivePolicyState, - expectedBgpRoutes: []string{ - ipStrToPrefix(clusterIPv6), - ipStrToPrefix(loadBalancerIPv6), - podIPv6CIDR.String(), + expectedBgpRoutes: map[bgp.Route]RouteMetadata{ + clusterIPv6Route1: allRoutes[clusterIPv6Route1], + loadBalancerIPv6Route: allRoutes[loadBalancerIPv6Route], + podIPv6CIDRRoute: allRoutes[podIPv6CIDRRoute], }, }, { @@ -2329,7 +2348,7 @@ func TestGetBGPRoutes(t *testing.T) { assert.EqualError(t, err, tt.expectedErr) } else { require.NoError(t, err) - assert.ElementsMatch(t, tt.expectedBgpRoutes, actualBgpRoutes) + assert.Equal(t, tt.expectedBgpRoutes, actualBgpRoutes) } }) } diff --git a/pkg/querier/querier.go b/pkg/querier/querier.go index f3153d65d90..156e645f66f 100644 --- a/pkg/querier/querier.go +++ b/pkg/querier/querier.go @@ -23,6 +23,7 @@ import ( "antrea.io/antrea/pkg/agent/apis" "antrea.io/antrea/pkg/agent/bgp" + bgpcontroller "antrea.io/antrea/pkg/agent/controller/bgp" "antrea.io/antrea/pkg/agent/interfacestore" "antrea.io/antrea/pkg/agent/multicast" "antrea.io/antrea/pkg/agent/types" @@ -148,5 +149,5 @@ type AgentBGPPolicyInfoQuerier interface { // GetBGPPeerStatus returns current status of BGP Peers of effective BGP Policy applied on the Node. GetBGPPeerStatus(ctx context.Context, ipv4Peers, ipv6Peers bool) ([]bgp.PeerStatus, error) // GetBGPRoutes returns the advertised BGP routes. - GetBGPRoutes(ctx context.Context, ipv4Routes, ipv6Routes bool) ([]string, error) + GetBGPRoutes(ctx context.Context, ipv4Routes, ipv6Routes bool) (map[bgp.Route]bgpcontroller.RouteMetadata, error) } diff --git a/pkg/querier/testing/mock_querier.go b/pkg/querier/testing/mock_querier.go index 7ce0fa0d8e2..fa27dc7f9a5 100644 --- a/pkg/querier/testing/mock_querier.go +++ b/pkg/querier/testing/mock_querier.go @@ -29,6 +29,7 @@ import ( reflect "reflect" bgp "antrea.io/antrea/pkg/agent/bgp" + bgp0 "antrea.io/antrea/pkg/agent/controller/bgp" interfacestore "antrea.io/antrea/pkg/agent/interfacestore" multicast "antrea.io/antrea/pkg/agent/multicast" types "antrea.io/antrea/pkg/agent/types" @@ -396,10 +397,10 @@ func (mr *MockAgentBGPPolicyInfoQuerierMockRecorder) GetBGPPolicyInfo() *gomock. } // GetBGPRoutes mocks base method. -func (m *MockAgentBGPPolicyInfoQuerier) GetBGPRoutes(ctx context.Context, ipv4Routes, ipv6Routes bool) ([]string, error) { +func (m *MockAgentBGPPolicyInfoQuerier) GetBGPRoutes(ctx context.Context, ipv4Routes, ipv6Routes bool) (map[bgp.Route]bgp0.RouteMetadata, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetBGPRoutes", ctx, ipv4Routes, ipv6Routes) - ret0, _ := ret[0].([]string) + ret0, _ := ret[0].(map[bgp.Route]bgp0.RouteMetadata) ret1, _ := ret[1].(error) return ret0, ret1 }