Skip to content

Commit

Permalink
pkg/agent: use GetInterfaceConfig in PrepareOVSBridgeForK8sNode
Browse files Browse the repository at this point in the history
Signed-off-by: Daman Arora <aroradaman@gmail.com>
  • Loading branch information
aroradaman committed Apr 15, 2024
1 parent 3552cd9 commit 7007314
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
23 changes: 13 additions & 10 deletions pkg/agent/agent_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ var (
// getInterfaceByName is meant to be overridden for testing.
getInterfaceByName = net.InterfaceByName

// getAllIPNetsByName is meant to be overridden for testing.
getAllIPNetsByName = util.GetAllIPNetsByName
// getInterfaceConfig is meant to be overridden for testing.
getInterfaceConfig = util.GetInterfaceConfig

// setInterfaceARPAnnounce is meant to be overridden for testing.
setInterfaceARPAnnounce = util.EnsureARPAnnounceOnInterface
Expand Down Expand Up @@ -70,22 +70,25 @@ func (i *Initializer) prepareOVSBridgeForK8sNode() error {
uplinkNetConfig := i.nodeConfig.UplinkNetConfig
uplinkNetConfig.Name = adapter.Name
uplinkNetConfig.MAC = adapter.HardwareAddr
uplinkIPs, err := getAllIPNetsByName(adapter.Name)
_, uplinkIPs, uplinkRoutes, err := getInterfaceConfig(adapter.Name)
if err != nil {
return fmt.Errorf("failed to get uplink IPs: %w", err)
return fmt.Errorf("failed to get uplink net config: %w", err)
}
var uplinkV4Routes []interface{}
for _, route := range uplinkRoutes {
if route.(netlink.Route).Gw.To4() == nil {
klog.V(2).Infof("Skipped IPv6 host route: %+v", route)
continue
}
uplinkV4Routes = append(uplinkV4Routes, route)
}
uplinkNetConfig.IPs = uplinkIPs
uplinkNetConfig.Routes = uplinkV4Routes
uplinkNetConfig.Index = adapter.Index
// Gateway and DNSServers are not configured at adapter in Linux
// Limitation: dynamic DNS servers will be lost after DHCP lease expired
uplinkNetConfig.Gateway = ""
uplinkNetConfig.DNSServers = ""
// Save routes which are configured on the uplink interface.
// The routes on the host will be lost when moving the network configuration of the uplink interface
// to the OVS bridge local interface. The saved routes will be restored on host after that.
if err = i.saveHostRoutes(); err != nil {
return err
}

// Set datapathID of OVS bridge.
// If no datapathID configured explicitly, the reconfiguration operation will change OVS bridge datapathID
Expand Down
12 changes: 6 additions & 6 deletions pkg/agent/agent_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ func mockGetInterfaceByName(t *testing.T, ipDevice *net.Interface) {
t.Cleanup(func() { getInterfaceByName = prevGetInterfaceByName })
}

func mockGetAllIPNetsByName(t *testing.T, ips []*net.IPNet) {
prevGetAllIPNetsByName := getAllIPNetsByName
getAllIPNetsByName = func(name string) ([]*net.IPNet, error) {
return ips, nil
func mockGetInterfaceConfig(t *testing.T, ips []*net.IPNet, routes []interface{}) {
prevGetInterfaceConfig := getInterfaceConfig
getInterfaceConfig = func(name string) (*net.Interface, []*net.IPNet, []interface{}, error) {
return nil, ips, routes, nil
}
t.Cleanup(func() { getAllIPNetsByName = prevGetAllIPNetsByName })
t.Cleanup(func() { getInterfaceConfig = prevGetInterfaceConfig })
}

func TestPrepareOVSBridgeForK8sNode(t *testing.T) {
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestPrepareOVSBridgeForK8sNode(t *testing.T) {
initializer.nodeConfig = nodeConfig
mockGetIPNetDeviceFromIP(t, nodeIPNet, ipDevice)
mockGetInterfaceByName(t, ipDevice)
mockGetAllIPNetsByName(t, []*net.IPNet{nodeIPNet})
mockGetInterfaceConfig(t, []*net.IPNet{nodeIPNet}, []interface{}{})
if tt.expectedCalls != nil {
tt.expectedCalls(mockOVSBridgeClient)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/agent/util/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func GetIPNetsByLink(link *net.Interface) ([]*net.IPNet, error) {
}
var addrs []*net.IPNet
for _, a := range addrList {
if ipNet, ok := a.(*net.IPNet); ok {
if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLinkLocalUnicast() {
addrs = append(addrs, ipNet)
}
}
Expand Down

0 comments on commit 7007314

Please sign in to comment.