Skip to content

Commit

Permalink
Add unit tests for pkg/agent/route/route_windows.go
Browse files Browse the repository at this point in the history
Signed-off-by: Hongliang Liu <lhongliang@vmware.com>
  • Loading branch information
hongliangl committed Sep 21, 2023
1 parent d863102 commit ec0ee7c
Show file tree
Hide file tree
Showing 9 changed files with 709 additions and 145 deletions.
1 change: 1 addition & 0 deletions hack/update-codegen-dockerized.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ MOCKGEN_TARGETS=(
"pkg/agent/util/ipset Interface testing"
"pkg/agent/util/iptables Interface testing mock_iptables_linux.go" # Must specify linux.go suffix, otherwise compilation would fail on windows platform as source file has linux build tag.
"pkg/agent/util/netlink Interface testing mock_netlink_linux.go"
"pkg/agent/util/winnet Interface testing mock_net_windows.go"
"pkg/antctl AntctlClient ."
"pkg/controller/networkpolicy EndpointQuerier testing"
"pkg/controller/querier ControllerQuerier testing"
Expand Down
17 changes: 2 additions & 15 deletions pkg/agent/route/route_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -926,8 +926,6 @@ func TestAddRoutes(t *testing.T) {
func TestDeleteRoutes(t *testing.T) {
tests := []struct {
name string
networkConfig *config.NetworkConfig
nodeConfig *config.NodeConfig
podCIDR *net.IPNet
existingNodeRoutes map[string][]*netlink.Route
existingNodeNeighbors map[string]*netlink.Neigh
Expand Down Expand Up @@ -972,8 +970,6 @@ func TestDeleteRoutes(t *testing.T) {
mockIPSet := ipsettest.NewMockInterface(ctrl)
c := &Client{netlink: mockNetlink,
ipset: mockIPSet,
networkConfig: tt.networkConfig,
nodeConfig: tt.nodeConfig,
nodeRoutes: sync.Map{},
nodeNeighbors: sync.Map{},
}
Expand Down Expand Up @@ -1462,27 +1458,18 @@ func TestAddExternalIPRoute(t *testing.T) {
tests := []struct {
name string
externalIPs []string
serviceRoutes map[string]*netlink.Route
expectedCalls func(mockNetlink *netlinktest.MockInterfaceMockRecorder)
}{
{
name: "IPv4",
serviceRoutes: map[string]*netlink.Route{
externalIPv4Addr1: ipv4Route1,
externalIPv4Addr2: ipv4Route2,
},
name: "IPv4",
externalIPs: []string{externalIPv4Addr1, externalIPv4Addr2},
expectedCalls: func(mockNetlink *netlinktest.MockInterfaceMockRecorder) {
mockNetlink.RouteReplace(ipv4Route1)
mockNetlink.RouteReplace(ipv4Route2)
},
},
{
name: "IPv6",
serviceRoutes: map[string]*netlink.Route{
externalIPv6Addr1: ipv6Route1,
externalIPv6Addr2: ipv6Route2,
},
name: "IPv6",
externalIPs: []string{externalIPv6Addr1, externalIPv6Addr2},
expectedCalls: func(mockNetlink *netlinktest.MockInterfaceMockRecorder) {
mockNetlink.RouteReplace(ipv6Route1)
Expand Down
54 changes: 27 additions & 27 deletions pkg/agent/route/route_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"antrea.io/antrea/pkg/agent/util"
antreasyscall "antrea.io/antrea/pkg/agent/util/syscall"
"antrea.io/antrea/pkg/agent/util/winfirewall"
"antrea.io/antrea/pkg/agent/util/winnet"
binding "antrea.io/antrea/pkg/ovs/openflow"
iputil "antrea.io/antrea/pkg/util/ip"
)
Expand All @@ -56,12 +57,13 @@ var (
type Client struct {
nodeConfig *config.NodeConfig
networkConfig *config.NetworkConfig
netUtil winnet.Interface
// nodeRoutes caches ip routes to remote Pods. It's a map of podCIDR to routes.
nodeRoutes *sync.Map
nodeRoutes sync.Map
// serviceRoutes caches ip routes about Services.
serviceRoutes *sync.Map
serviceRoutes sync.Map
// netNatStaticMappings caches Windows NetNat for NodePort.
netNatStaticMappings *sync.Map
netNatStaticMappings sync.Map
fwClient *winfirewall.Client
bridgeInfIndex int
noSNAT bool
Expand All @@ -73,14 +75,12 @@ type Client struct {
// NewClient returns a route client.
func NewClient(networkConfig *config.NetworkConfig, noSNAT, proxyAll, connectUplinkToBridge, multicastEnabled bool, serviceCIDRProvider servicecidr.Interface) (*Client, error) {
return &Client{
networkConfig: networkConfig,
nodeRoutes: &sync.Map{},
serviceRoutes: &sync.Map{},
netNatStaticMappings: &sync.Map{},
fwClient: winfirewall.NewClient(),
noSNAT: noSNAT,
proxyAll: proxyAll,
serviceCIDRProvider: serviceCIDRProvider,
networkConfig: networkConfig,
netUtil: &util.Handle{},
fwClient: winfirewall.NewClient(),
noSNAT: noSNAT,
proxyAll: proxyAll,
serviceCIDRProvider: serviceCIDRProvider,
}, nil
}

Expand Down Expand Up @@ -187,7 +187,7 @@ func (c *Client) Reconcile(podCIDRs []string) error {
if c.proxyAll && c.isServiceRoute(&routes[i]) {
continue
}
err = util.RemoveNetRoute(&routes[i])
err = c.netUtil.RemoveNetRoute(&routes[i])
if err != nil {
return err
}
Expand Down Expand Up @@ -222,7 +222,7 @@ func (c *Client) AddRoutes(podCIDR *net.IPNet, nodeName string, peerNodeIP, peer
return nil
}
// Remove the existing route entry if the gateway address is not as expected.
if err := util.RemoveNetRoute(existingRoute); err != nil {
if err := c.netUtil.RemoveNetRoute(existingRoute); err != nil {
klog.Errorf("Failed to delete existing route entry with destination %s gateway %s on %s (%s)", podCIDR.String(), peerGwIP.String(), nodeName, peerNodeIP)
return err
}
Expand All @@ -232,7 +232,7 @@ func (c *Client) AddRoutes(podCIDR *net.IPNet, nodeName string, peerNodeIP, peer
return nil
}

if err := util.ReplaceNetRoute(route); err != nil {
if err := c.netUtil.ReplaceNetRoute(route); err != nil {
return err
}

Expand All @@ -251,7 +251,7 @@ func (c *Client) DeleteRoutes(podCIDR *net.IPNet) error {
}

rt := obj.(*util.Route)
if err := util.RemoveNetRoute(rt); err != nil {
if err := c.netUtil.RemoveNetRoute(rt); err != nil {
return err
}
c.nodeRoutes.Delete(podCIDR.String())
Expand All @@ -266,13 +266,13 @@ func (c *Client) addVirtualServiceIPRoute(isIPv6 bool) error {
svcIP := config.VirtualServiceIPv4

neigh := generateNeigh(svcIP, linkIndex)
if err := util.ReplaceNetNeighbor(neigh); err != nil {
if err := c.netUtil.ReplaceNetNeighbor(neigh); err != nil {
return fmt.Errorf("failed to add new IP neighbour for %s: %w", svcIP, err)
}
klog.InfoS("Added virtual Service IP neighbor", "neighbor", neigh)

route := generateRoute(virtualServiceIPv4Net, net.IPv4zero, linkIndex, util.MetricHigh)
if err := util.ReplaceNetRoute(route); err != nil {
if err := c.netUtil.ReplaceNetRoute(route); err != nil {
return fmt.Errorf("failed to install route for virtual Service IP %s: %w", svcIP.String(), err)
}
c.serviceRoutes.Store(svcIP.String(), route)
Expand All @@ -289,7 +289,7 @@ func (c *Client) addServiceCIDRRoute(serviceCIDR *net.IPNet) error {
oldServiceCIDRRoute, serviceCIDRRouteExists := c.serviceRoutes.Load(serviceIPv4CIDRKey)
// Generate a route with the new ClusterIP CIDR and install it.
route := generateRoute(serviceCIDR, gw, linkIndex, metric)
if err := util.ReplaceNetRoute(route); err != nil {
if err := c.netUtil.ReplaceNetRoute(route); err != nil {
return fmt.Errorf("failed to install a new Service CIDR route: %w", err)
}

Expand Down Expand Up @@ -329,7 +329,7 @@ func (c *Client) addServiceCIDRRoute(serviceCIDR *net.IPNet) error {

// Remove stale routes.
for _, rt := range staleRoutes {
if err := util.RemoveNetRoute(rt); err != nil {
if err := c.netUtil.RemoveNetRoute(rt); err != nil {
return fmt.Errorf("failed to delete stale Service CIDR route %s: %w", rt.String(), err)
} else {
klog.V(4).InfoS("Deleted stale Service CIDR route successfully", "route", rt)
Expand All @@ -346,7 +346,7 @@ func (c *Client) addVirtualNodePortDNATIPRoute(isIPv6 bool) error {
gw := config.VirtualServiceIPv4

route := generateRoute(virtualNodePortDNATIPv4Net, gw, linkIndex, util.MetricHigh)
if err := util.ReplaceNetRoute(route); err != nil {
if err := c.netUtil.ReplaceNetRoute(route); err != nil {
return fmt.Errorf("failed to install route for NodePort DNAT IP %s: %w", vIP.String(), err)
}
c.serviceRoutes.Store(vIP.String(), route)
Expand Down Expand Up @@ -387,7 +387,7 @@ func (c *Client) syncIPInfra() {

func (c *Client) syncRoute() error {
restoreRoute := func(route *util.Route) bool {
if err := util.ReplaceNetRoute(route); err != nil {
if err := c.netUtil.ReplaceNetRoute(route); err != nil {
klog.ErrorS(err, "Failed to sync route", "Route", route)
return false
}
Expand Down Expand Up @@ -423,7 +423,7 @@ func (c *Client) syncNetNatStaticMapping() error {

c.netNatStaticMappings.Range(func(_, v interface{}) bool {
mapping := v.(*util.NetNatStaticMapping)
if err := util.ReplaceNetNatStaticMapping(mapping); err != nil {
if err := c.netUtil.ReplaceNetNatStaticMapping(mapping); err != nil {
klog.ErrorS(err, "Failed to add netNatStaticMapping", "netNatStaticMapping", mapping)
return false
}
Expand All @@ -445,7 +445,7 @@ func (c *Client) isServiceRoute(route *util.Route) bool {
func (c *Client) listIPRoutesOnGW() ([]util.Route, error) {
family := antreasyscall.AF_INET
filter := &util.Route{LinkIndex: c.nodeConfig.GatewayConfig.LinkIndex}
return util.RouteListFiltered(family, filter, util.RT_FILTER_IF)
return c.netUtil.RouteListFiltered(family, filter, util.RT_FILTER_IF)
}

// initFwRules adds Windows Firewall rules to accept the traffic that is sent to or from local Pods.
Expand Down Expand Up @@ -479,7 +479,7 @@ func (c *Client) AddNodePort(nodePortAddresses []net.IP, port uint16, protocol b
InternalPort: port,
Protocol: protocol,
}
if err := util.ReplaceNetNatStaticMapping(netNatStaticMapping); err != nil {
if err := c.netUtil.ReplaceNetNatStaticMapping(netNatStaticMapping); err != nil {
return err
}
c.netNatStaticMappings.Store(fmt.Sprintf("%d-%s", port, protocol), netNatStaticMapping)
Expand All @@ -495,7 +495,7 @@ func (c *Client) DeleteNodePort(nodePortAddresses []net.IP, port uint16, protoco
return nil
}
netNatStaticMapping := obj.(*util.NetNatStaticMapping)
if err := util.RemoveNetNatStaticMapping(netNatStaticMapping); err != nil {
if err := c.netUtil.RemoveNetNatStaticMapping(netNatStaticMapping); err != nil {
return err
}
c.netNatStaticMappings.Delete(key)
Expand All @@ -512,7 +512,7 @@ func (c *Client) AddExternalIPRoute(externalIP net.IP) error {
svcIPNet := util.NewIPNet(externalIP)

route := generateRoute(svcIPNet, gw, linkIndex, metric)
if err := util.ReplaceNetRoute(route); err != nil {
if err := c.netUtil.ReplaceNetRoute(route); err != nil {
return fmt.Errorf("failed to install route for external IP %s: %w", externalIPStr, err)
}
c.serviceRoutes.Store(externalIPStr, route)
Expand All @@ -528,7 +528,7 @@ func (c *Client) DeleteExternalIPRoute(externalIP net.IP) error {
klog.V(2).InfoS("Didn't find route for external IP", "IP", externalIPStr)
return nil
}
if err := util.RemoveNetRoute(route.(*util.Route)); err != nil {
if err := c.netUtil.RemoveNetRoute(route.(*util.Route)); err != nil {
return fmt.Errorf("failed to delete route for external IP %s: %w", externalIPStr, err)
}
c.serviceRoutes.Delete(externalIPStr)
Expand Down
Loading

0 comments on commit ec0ee7c

Please sign in to comment.