-
Notifications
You must be signed in to change notification settings - Fork 371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add 'type' arg for antctl get bgproutes to filter bgproutes #6835
Open
Atish-iaf
wants to merge
1
commit into
antrea-io:main
Choose a base branch
from
Atish-iaf:add-type-arg-for-antctl-get-bgproutes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+177
−166
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ import ( | |
"strings" | ||
|
||
"k8s.io/klog/v2" | ||
"k8s.io/utils/net" | ||
|
||
"antrea.io/antrea/pkg/agent/apis" | ||
"antrea.io/antrea/pkg/agent/controller/bgp" | ||
|
@@ -40,6 +41,7 @@ func HandleFunc(bq querier.AgentBGPPolicyInfoQuerier) http.HandlerFunc { | |
} | ||
|
||
values := r.URL.Query() | ||
bgpRouteType := values.Get("type") | ||
var ipv4Only, ipv6Only bool | ||
if values.Has("ipv4-only") { | ||
if values.Get("ipv4-only") != "" { | ||
|
@@ -60,7 +62,7 @@ func HandleFunc(bq querier.AgentBGPPolicyInfoQuerier) http.HandlerFunc { | |
return | ||
} | ||
|
||
bgpRoutes, err := bq.GetBGPRoutes(r.Context(), !ipv6Only, !ipv4Only) | ||
bgpRoutes, err := bq.GetBGPRoutes(r.Context()) | ||
if err != nil { | ||
if errors.Is(err, bgp.ErrBGPPolicyNotFound) { | ||
http.Error(w, "there is no effective bgp policy applied to the Node", http.StatusNotFound) | ||
|
@@ -71,12 +73,17 @@ func HandleFunc(bq querier.AgentBGPPolicyInfoQuerier) http.HandlerFunc { | |
} | ||
|
||
var bgpRoutesResp []apis.BGPRouteResponse | ||
for bgpRoute := range bgpRoutes { | ||
bgpRoutesResp = append(bgpRoutesResp, apis.BGPRouteResponse{ | ||
Route: bgpRoute.Prefix, | ||
Type: string(bgpRoutes[bgpRoute].Type), | ||
K8sObjRef: bgpRoutes[bgpRoute].K8sObjRef, | ||
}) | ||
for bgpRoute, routeMetadata := range bgpRoutes { | ||
if ((!ipv4Only && !ipv6Only) || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto: if ipv4Only && !utilnet.IsIPv4String(peer.Address)) {
continue
}
if ipv6Only && !utilnet.IsIPv6String(peer.Address)) {
continue
}
if bgpRouteType != "" && bgpRouteType != string(routeMetadata.Type) {
continue
}
// add to response |
||
(ipv4Only && net.IsIPv4CIDRString(bgpRoute.Prefix)) || | ||
(ipv6Only && net.IsIPv6CIDRString(bgpRoute.Prefix))) && | ||
(bgpRouteType == "" || bgpRouteType == string(routeMetadata.Type)) { | ||
bgpRoutesResp = append(bgpRoutesResp, apis.BGPRouteResponse{ | ||
Route: bgpRoute.Prefix, | ||
Type: string(routeMetadata.Type), | ||
K8sObjRef: routeMetadata.K8sObjRef, | ||
}) | ||
} | ||
} | ||
// make sure that we provide a stable order for the API response | ||
slices.SortFunc(bgpRoutesResp, func(a, b apis.BGPRouteResponse) int { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,17 +43,21 @@ var ( | |
podIPv4CIDRRoute = bgp.Route{Prefix: podIPv4CIDR} | ||
clusterIPv4 = "10.96.10.10" | ||
clusterIPv4Route = bgp.Route{Prefix: ipStrToPrefix(clusterIPv4)} | ||
egressIPv4 = "10.96.11.10" | ||
egressIPv4Route = bgp.Route{Prefix: ipStrToPrefix(egressIPv4)} | ||
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" | ||
ipv4EgressName = "egress-4" | ||
ipv6LoadBalancerName = "loadbalancer-6" | ||
ipv6EgressName = "egress-6" | ||
|
||
allRoutes = map[bgp.Route]bgpcontroller.RouteMetadata{ | ||
clusterIPv4Route: {Type: bgpcontroller.ServiceClusterIP, K8sObjRef: getServiceName(ipv4ClusterIPName)}, | ||
egressIPv4Route: {Type: bgpcontroller.EgressIP, K8sObjRef: ipv4EgressName}, | ||
loadBalancerIPv6Route: {Type: bgpcontroller.ServiceLoadBalancerIP, K8sObjRef: getServiceName(ipv6LoadBalancerName)}, | ||
egressIPv6Route: {Type: bgpcontroller.EgressIP, K8sObjRef: ipv6EgressName}, | ||
podIPv4CIDRRoute: {Type: bgpcontroller.NodeIPAMPodCIDR}, | ||
|
@@ -72,22 +76,22 @@ func TestBGPRouteQuery(t *testing.T) { | |
{ | ||
name: "bgpPolicyState does not exist", | ||
expectedCalls: func(mockBGPServer *queriertest.MockAgentBGPPolicyInfoQuerier) { | ||
mockBGPServer.EXPECT().GetBGPRoutes(context.Background(), true, true).Return(nil, bgpcontroller.ErrBGPPolicyNotFound) | ||
mockBGPServer.EXPECT().GetBGPRoutes(context.Background()).Return(nil, bgpcontroller.ErrBGPPolicyNotFound) | ||
}, | ||
expectedStatus: http.StatusNotFound, | ||
}, | ||
{ | ||
name: "get all advertised routes", | ||
expectedCalls: func(mockBGPServer *queriertest.MockAgentBGPPolicyInfoQuerier) { | ||
mockBGPServer.EXPECT().GetBGPRoutes(ctx, true, true).Return( | ||
map[bgp.Route]bgpcontroller.RouteMetadata{ | ||
clusterIPv4Route: allRoutes[clusterIPv4Route], | ||
podIPv4CIDRRoute: allRoutes[podIPv4CIDRRoute], | ||
egressIPv6Route: allRoutes[egressIPv6Route], | ||
}, nil) | ||
mockBGPServer.EXPECT().GetBGPRoutes(ctx).Return(allRoutes, nil) | ||
}, | ||
expectedStatus: http.StatusOK, | ||
expectedResponse: []apis.BGPRouteResponse{ | ||
{ | ||
Route: egressIPv4Route.Prefix, | ||
Type: string(allRoutes[egressIPv4Route].Type), | ||
K8sObjRef: allRoutes[egressIPv4Route].K8sObjRef, | ||
}, | ||
{ | ||
Route: podIPv4CIDR, | ||
Type: string(bgpcontroller.NodeIPAMPodCIDR), | ||
|
@@ -102,20 +106,26 @@ func TestBGPRouteQuery(t *testing.T) { | |
Type: string(allRoutes[egressIPv6Route].Type), | ||
K8sObjRef: allRoutes[egressIPv6Route].K8sObjRef, | ||
}, | ||
{ | ||
Route: loadBalancerIPv6Route.Prefix, | ||
Type: string(allRoutes[loadBalancerIPv6Route].Type), | ||
K8sObjRef: allRoutes[loadBalancerIPv6Route].K8sObjRef, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "get advertised ipv4 routes only", | ||
url: "?ipv4-only", | ||
expectedCalls: func(mockBGPServer *queriertest.MockAgentBGPPolicyInfoQuerier) { | ||
mockBGPServer.EXPECT().GetBGPRoutes(ctx, true, false).Return( | ||
map[bgp.Route]bgpcontroller.RouteMetadata{ | ||
clusterIPv4Route: allRoutes[clusterIPv4Route], | ||
podIPv4CIDRRoute: allRoutes[podIPv4CIDRRoute], | ||
}, nil) | ||
mockBGPServer.EXPECT().GetBGPRoutes(ctx).Return(allRoutes, nil) | ||
}, | ||
expectedStatus: http.StatusOK, | ||
expectedResponse: []apis.BGPRouteResponse{ | ||
{ | ||
Route: egressIPv4Route.Prefix, | ||
Type: string(allRoutes[egressIPv4Route].Type), | ||
K8sObjRef: allRoutes[egressIPv4Route].K8sObjRef, | ||
}, | ||
{ | ||
Route: podIPv4CIDRRoute.Prefix, | ||
Type: string(allRoutes[podIPv4CIDRRoute].Type), | ||
|
@@ -131,11 +141,7 @@ func TestBGPRouteQuery(t *testing.T) { | |
name: "get advertised ipv6 routes only", | ||
url: "?ipv6-only=", | ||
expectedCalls: func(mockBGPServer *queriertest.MockAgentBGPPolicyInfoQuerier) { | ||
mockBGPServer.EXPECT().GetBGPRoutes(ctx, false, true).Return( | ||
map[bgp.Route]bgpcontroller.RouteMetadata{ | ||
loadBalancerIPv6Route: allRoutes[loadBalancerIPv6Route], | ||
egressIPv6Route: allRoutes[egressIPv6Route], | ||
}, nil) | ||
mockBGPServer.EXPECT().GetBGPRoutes(ctx).Return(allRoutes, nil) | ||
}, | ||
expectedStatus: http.StatusOK, | ||
expectedResponse: []apis.BGPRouteResponse{ | ||
|
@@ -161,6 +167,41 @@ func TestBGPRouteQuery(t *testing.T) { | |
url: "?ipv4-only&ipv6-only", | ||
expectedStatus: http.StatusBadRequest, | ||
}, | ||
{ | ||
name: "get all advertised egressIP routes", | ||
url: "?type=EgressIP", | ||
expectedCalls: func(mockBGPServer *queriertest.MockAgentBGPPolicyInfoQuerier) { | ||
mockBGPServer.EXPECT().GetBGPRoutes(ctx).Return(allRoutes, nil) | ||
}, | ||
expectedStatus: http.StatusOK, | ||
expectedResponse: []apis.BGPRouteResponse{ | ||
{ | ||
Route: egressIPv4Route.Prefix, | ||
Type: string(allRoutes[egressIPv4Route].Type), | ||
K8sObjRef: allRoutes[egressIPv4Route].K8sObjRef, | ||
}, | ||
{ | ||
Route: egressIPv6Route.Prefix, | ||
Type: string(allRoutes[egressIPv6Route].Type), | ||
K8sObjRef: allRoutes[egressIPv6Route].K8sObjRef, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "get advertised egress ipv4 routes only", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. actually for consistency with the name of the previous sub-test: "get advertised IPv4 egressIP routes" |
||
url: "?ipv4-only&type=EgressIP", | ||
expectedCalls: func(mockBGPServer *queriertest.MockAgentBGPPolicyInfoQuerier) { | ||
mockBGPServer.EXPECT().GetBGPRoutes(ctx).Return(allRoutes, nil) | ||
}, | ||
expectedStatus: http.StatusOK, | ||
expectedResponse: []apis.BGPRouteResponse{ | ||
{ | ||
Route: egressIPv4Route.Prefix, | ||
Type: string(allRoutes[egressIPv4Route].Type), | ||
K8sObjRef: allRoutes[egressIPv4Route].K8sObjRef, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it may be more readable to test the opposite: