Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
Signed-off-by: Hongliang Liu <[email protected]>
  • Loading branch information
hongliangl committed May 11, 2024
1 parent 3a157cf commit 0619ea0
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 114 deletions.
31 changes: 17 additions & 14 deletions pkg/agent/route/route_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,8 @@ func TestSyncRoutes(t *testing.T) {
})

c := &Client{
winnet: mockWinnet,
proxyAll: true,
nodeRoutes: sync.Map{},
serviceRoutes: sync.Map{},
winnet: mockWinnet,
proxyAll: true,
nodeConfig: &config.NodeConfig{
GatewayConfig: &config.GatewayConfig{LinkIndex: 10, IPv4: net.ParseIP("192.168.0.1")},
PodIPv4CIDR: ip.MustParseCIDR("192.168.0.0/24"),
Expand All @@ -97,7 +95,8 @@ func TestInitServiceIPRoutes(t *testing.T) {
ctrl := gomock.NewController(t)
mockWinnet := winnettesting.NewMockInterface(ctrl)
mockServiceCIDRProvider := servicecidrtesting.NewMockInterface(ctrl)
c := &Client{winnet: mockWinnet,
c := &Client{
winnet: mockWinnet,
networkConfig: &config.NetworkConfig{
TrafficEncapMode: config.TrafficEncapModeEncap,
IPv4Enabled: true,
Expand All @@ -117,7 +116,8 @@ func TestInitServiceIPRoutes(t *testing.T) {
func TestReconcile(t *testing.T) {
ctrl := gomock.NewController(t)
mockWinnet := winnettesting.NewMockInterface(ctrl)
c := &Client{winnet: mockWinnet,
c := &Client{
winnet: mockWinnet,
proxyAll: true,
networkConfig: &config.NetworkConfig{},
nodeConfig: &config.NodeConfig{
Expand Down Expand Up @@ -230,7 +230,8 @@ func TestAddRoutes(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
netutil := winnettesting.NewMockInterface(ctrl)
c := &Client{winnet: netutil,
c := &Client{
winnet: netutil,
networkConfig: tt.networkConfig,
nodeConfig: tt.nodeConfig,
}
Expand All @@ -248,7 +249,8 @@ func TestDeleteRoutes(t *testing.T) {
podCIDR := ip.MustParseCIDR("192.168.10.0/24")
ctrl := gomock.NewController(t)
mockWinnet := winnettesting.NewMockInterface(ctrl)
c := &Client{winnet: mockWinnet,
c := &Client{
winnet: mockWinnet,
nodeRoutes: sync.Map{},
}
for podCIDRStr, nodeRoute := range existingNodeRoutes {
Expand All @@ -262,8 +264,7 @@ func TestAddNodePort(t *testing.T) {
ctrl := gomock.NewController(t)
mockWinnet := winnettesting.NewMockInterface(ctrl)
c := &Client{
winnet: mockWinnet,
netNatStaticMappings: sync.Map{},
winnet: mockWinnet,
}
mockWinnet.EXPECT().ReplaceNetNatStaticMapping(nodePortNetNatStaticMapping)
assert.NoError(t, c.AddNodePort(nil, nodePort, protocol))
Expand All @@ -273,8 +274,7 @@ func TestDeleteNodePort(t *testing.T) {
ctrl := gomock.NewController(t)
mockWinnet := winnettesting.NewMockInterface(ctrl)
c := &Client{
winnet: mockWinnet,
netNatStaticMappings: sync.Map{},
winnet: mockWinnet,
}
c.netNatStaticMappings.Store(fmt.Sprintf("%d-%s", nodePort, protocol), nodePortNetNatStaticMapping)
mockWinnet.EXPECT().RemoveNetNatStaticMapping(nodePortNetNatStaticMapping)
Expand Down Expand Up @@ -404,7 +404,8 @@ func TestAddExternalIPRoute(t *testing.T) {

ctrl := gomock.NewController(t)
mockWinnet := winnettesting.NewMockInterface(ctrl)
c := &Client{winnet: mockWinnet,
c := &Client{
winnet: mockWinnet,
nodeConfig: &config.NodeConfig{
GatewayConfig: &config.GatewayConfig{
LinkIndex: 10,
Expand All @@ -424,7 +425,9 @@ func TestDeleteExternalIPRoute(t *testing.T) {

ctrl := gomock.NewController(t)
mockWinnet := winnettesting.NewMockInterface(ctrl)
c := &Client{winnet: mockWinnet}
c := &Client{
winnet: mockWinnet,
}
for ipStr, route := range map[string]*winnet.Route{externalIPv4Addr1: ipv4Route1, externalIPv4Addr2: ipv4Route2} {
c.serviceRoutes.Store(ipStr, route)
}
Expand Down
7 changes: 0 additions & 7 deletions pkg/agent/util/net_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (

antreasyscall "antrea.io/antrea/pkg/agent/util/syscall"
"antrea.io/antrea/pkg/agent/util/winnet"
"antrea.io/antrea/pkg/ovs/ovsconfig"
)

const (
Expand Down Expand Up @@ -436,9 +435,3 @@ func RenameInterface(from, to string) error {
func GenHostInterfaceName(upLinkIfName string) string {
return strings.TrimSuffix(upLinkIfName, bridgedUplinkSuffix)
}

func PrepareHostInterfaceConnection(_ ovsconfig.OVSBridgeClient, ifaceName string, _ int32, _ map[string]interface{}) (string, bool, error) {
return ifaceName, false, nil
}

func RestoreHostInterfaceConfiguration(_ string, _ string) {}
40 changes: 18 additions & 22 deletions pkg/agent/util/winnet/net_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (h *Handle) RemoveNetAdapterIPAddress(adapterName string, ipAddr net.IP) er
func (h *Handle) EnableIPForwarding(adapterName string) error {
adapter, err := getAdapterInAllCompartmentsByName(adapterName)
if err != nil {
return fmt.Errorf("unable to find NetAdapter on host in all compartments with name %s: %v", adapterName, err)
return fmt.Errorf("unable to find NetAdapter on host in all compartments with name %s: %w", adapterName, err)
}
return adapter.setForwarding(true, antreasyscall.AF_INET)
}
Expand Down Expand Up @@ -274,7 +274,7 @@ func (h *Handle) IsNetAdapterIPv4DHCPEnabled(adapterName string) (bool, error) {
func (h *Handle) SetNetAdapterMTU(adapterName string, mtu int) error {
adapter, err := getAdapterInAllCompartmentsByName(adapterName)
if err != nil {
return fmt.Errorf("unable to find NetAdapter on host in all compartments with name %s: %v", adapterName, err)
return fmt.Errorf("unable to find NetAdapter on host in all compartments with name %s: %w", adapterName, err)
}
return adapter.setMTU(mtu, antreasyscall.AF_INET)
}
Expand Down Expand Up @@ -305,7 +305,7 @@ func (h *Handle) AddNetRoute(route *Route) error {
}
row := toMibIPForwardRow(route)
if err := antreaNetIO.CreateIPForwardEntry(row); err != nil {
return fmt.Errorf("failed to create new IPForward row: %v", err)
return fmt.Errorf("failed to create new IPForward row: %w", err)
}
return nil
}
Expand All @@ -317,13 +317,13 @@ func (h *Handle) RemoveNetRoute(route *Route) error {
family := AddressFamilyByIP(route.DestinationSubnet.IP)
rows, err := antreaNetIO.ListIPForwardRows(family)
if err != nil {
return fmt.Errorf("unable to list Windows IPForward rows: %v", err)
return fmt.Errorf("unable to list Windows IPForward rows: %w", err)
}
for i := range rows {
row := rows[i]
if row.DestinationPrefix.EqualsTo(route.DestinationSubnet) && row.Index == uint32(route.LinkIndex) && row.NextHop.IP().Equal(route.GatewayAddress) {
if err := antreaNetIO.DeleteIPForwardEntry(&row); err != nil {
return fmt.Errorf("failed to delete existing route with nextHop %s: %v", route.GatewayAddress, err)
return fmt.Errorf("failed to delete existing route with nextHop %s: %w", route.GatewayAddress, err)
}
}
}
Expand All @@ -337,7 +337,7 @@ func (h *Handle) ReplaceNetRoute(route *Route) error {
family := AddressFamilyByIP(route.DestinationSubnet.IP)
rows, err := antreaNetIO.ListIPForwardRows(family)
if err != nil {
return fmt.Errorf("unable to list Windows IPForward rows: %v", err)
return fmt.Errorf("unable to list Windows IPForward rows: %w", err)
}
for i := range rows {
row := rows[i]
Expand All @@ -346,7 +346,7 @@ func (h *Handle) ReplaceNetRoute(route *Route) error {
return nil
} else {
if err := antreaNetIO.DeleteIPForwardEntry(&row); err != nil {
return fmt.Errorf("failed to delete existing route with nextHop %s: %v", route.GatewayAddress, err)
return fmt.Errorf("failed to delete existing route with nextHop %s: %w", route.GatewayAddress, err)
}
}
}
Expand All @@ -357,7 +357,7 @@ func (h *Handle) ReplaceNetRoute(route *Route) error {
func (h *Handle) RouteListFiltered(family uint16, filter *Route, filterMask uint64) ([]Route, error) {
rows, err := antreaNetIO.ListIPForwardRows(family)
if err != nil {
return nil, fmt.Errorf("unable to list Windows IPForward rows: %v", err)
return nil, fmt.Errorf("unable to list Windows IPForward rows: %w", err)
}
rts := make([]Route, 0, len(rows))
for i := range rows {
Expand Down Expand Up @@ -399,8 +399,7 @@ func (h *Handle) AddNetNat(netNatName string, subnetCIDR *net.IPNet) error {
cmd := fmt.Sprintf("Get-NetNat -Name %s | Select-Object InternalIPInterfaceAddressPrefix | Format-Table -HideTableHeaders", netNatName)
if internalNet, err := runCommand(cmd); err != nil {
if !strings.Contains(err.Error(), "No MSFT_NetNat objects found") {
klog.ErrorS(err, "Failed to check the existing netnat", "name", netNatName)
return err
return fmt.Errorf("failed to check the existing netnat '%s': %w", netNatName, err)
}
} else {
if strings.Contains(internalNet, subnetCIDR.String()) {
Expand All @@ -410,15 +409,13 @@ func (h *Handle) AddNetNat(netNatName string, subnetCIDR *net.IPNet) error {
klog.InfoS("Removing the existing NetNat", "name", netNatName, "internalIPInterfaceAddressPrefix", internalNet)
cmd = fmt.Sprintf("Remove-NetNat -Name %s -Confirm:$false", netNatName)
if _, err := runCommand(cmd); err != nil {
klog.ErrorS(err, "Failed to remove the existing netnat", "name", netNatName, "internalIPInterfaceAddressPrefix", internalNet)
return err
return fmt.Errorf("failed to remove the existing netnat '%s' with internalIPInterfaceAddressPrefix '%s': %w", netNatName, internalNet, err)
}
}
cmd = fmt.Sprintf("New-NetNat -Name %s -InternalIPInterfaceAddressPrefix %s", netNatName, subnetCIDR.String())
_, err := runCommand(cmd)
if err != nil {
klog.ErrorS(err, "Failed to add netnat", "name", netNatName, "internalIPInterfaceAddressPrefix", subnetCIDR.String())
return err
return fmt.Errorf("failed to add netnat '%s' with internalIPInterfaceAddressPrefix '%s': %w", netNatName, subnetCIDR.String(), err)
}
return nil
}
Expand Down Expand Up @@ -513,16 +510,16 @@ func getNetNeighbor(neighbor *Neighbor) ([]Neighbor, error) {
for _, items := range parsed {
idx, err := strconv.Atoi(items[0])
if err != nil {
return nil, fmt.Errorf("failed to parse the LinkIndex '%s': %v", items[0], err)
return nil, fmt.Errorf("failed to parse the LinkIndex '%s': %w", items[0], err)
}
dstIP := net.ParseIP(items[1])
if err != nil {
return nil, fmt.Errorf("failed to parse the DestinationIP '%s': %v", items[1], err)
return nil, fmt.Errorf("failed to parse the DestinationIP '%s': %w", items[1], err)
}
// Get-NetNeighbor returns LinkLayerAddress like "AA-BB-CC-DD-EE-FF".
mac, err := net.ParseMAC(strings.ReplaceAll(items[2], "-", ":"))
if err != nil {
return nil, fmt.Errorf("failed to parse the Gateway MAC '%s': %v", items[2], err)
return nil, fmt.Errorf("failed to parse the Gateway MAC '%s': %w", items[2], err)
}
neighbor := Neighbor{
LinkIndex: idx,
Expand Down Expand Up @@ -643,7 +640,7 @@ func (a *adapter) setMTU(mtu int, family uint16) error {
newEntry.NlMtu = uint32(mtu)
return &newEntry
}); err != nil {
return fmt.Errorf("unable to set IPInterface with MTU %d: %v", mtu, err)
return fmt.Errorf("unable to set IPInterface with MTU %d: %w", mtu, err)
}
return nil
}
Expand All @@ -654,7 +651,7 @@ func (a *adapter) setForwarding(enabledForwarding bool, family uint16) error {
newEntry.ForwardingEnabled = enabledForwarding
return &newEntry
}); err != nil {
return fmt.Errorf("unable to enable IPForwarding on network adapter: %v", err)
return fmt.Errorf("unable to enable IPForwarding on network adapter: %w", err)
}
return nil
}
Expand All @@ -667,13 +664,12 @@ func (a *adapter) setIPInterfaceEntry(family uint16, updateFunc updateIPInterfac
runtime.UnlockOSThread()
}()
if err := hcsshim.SetCurrentThreadCompartmentId(a.compartmentID); err != nil {
klog.ErrorS(err, "Failed to change current thread's compartment", "compartment", a.compartmentID)
return err
return fmt.Errorf("failed to change current thread's compartment '%d': %w", a.compartmentID, err)
}
}
ipInterfaceRow := &antreasyscall.MibIPInterfaceRow{Family: family, Index: uint32(a.Index)}
if err := antreaNetIO.GetIPInterfaceEntry(ipInterfaceRow); err != nil {
return fmt.Errorf("unable to get IPInterface entry with Index %d: %v", a.Index, err)
return fmt.Errorf("unable to get IPInterface entry with Index %d: %w", a.Index, err)
}
updatedRow := updateFunc(ipInterfaceRow)
updatedRow.SitePrefixLength = 0
Expand Down
Loading

0 comments on commit 0619ea0

Please sign in to comment.