diff --git a/pkg/agent/util/net_linux.go b/pkg/agent/util/net_linux.go index 9c7714ef894..f26e711f351 100644 --- a/pkg/agent/util/net_linux.go +++ b/pkg/agent/util/net_linux.go @@ -130,6 +130,28 @@ func SetLinkUp(name string) (net.HardwareAddr, int, error) { return mac, index, nil } +func addrSliceDifference(s1, s2 []netlink.Addr) []*netlink.Addr { + var diff []*netlink.Addr + + for i, e1 := range s1 { + found := false + for _, e2 := range s2 { + if e1.Equal(e2) { + found = true + break + } + } + if !found { + diff = append(diff, &s1[i]) + } + } + + return diff +} + +// ConfigureLinkAddresses adds the provided addresses to the interface identified by index idx, if +// they are missing from the interface. Any other existing address already configured for the +// interface will be removed, unless it is a link-local address. func ConfigureLinkAddresses(idx int, ipNets []*net.IPNet) error { // No need to check the error here, since the link is found in previous steps. link, _ := netlink.LinkByIndex(idx) @@ -151,25 +173,6 @@ func ConfigureLinkAddresses(idx int, ipNets []*net.IPNet) error { } } - addrSliceDifference := func(s1, s2 []netlink.Addr) []*netlink.Addr { - var diff []*netlink.Addr - - for i, e1 := range s1 { - found := false - for _, e2 := range s2 { - if e1.Equal(e2) { - found = true - break - } - } - if !found { - diff = append(diff, &s1[i]) - } - } - - return diff - } - addrsToAdd := addrSliceDifference(newAddrs, addrs) addrsToRemove := addrSliceDifference(addrs, newAddrs) diff --git a/pkg/agent/util/net_windows.go b/pkg/agent/util/net_windows.go index 1b117f32fb8..c01cc1d9140 100644 --- a/pkg/agent/util/net_windows.go +++ b/pkg/agent/util/net_windows.go @@ -281,6 +281,35 @@ func SetLinkUp(name string) (net.HardwareAddr, int, error) { return mac, index, nil } +func addrEqual(addr1, addr2 *net.IPNet) bool { + size1, _ := addr1.Mask.Size() + size2, _ := addr2.Mask.Size() + return addr1.IP.Equal(addr2.IP) && size1 == size2 +} + +func addrSliceDifference(s1, s2 []*net.IPNet) []*net.IPNet { + var diff []*net.IPNet + + for _, e1 := range s1 { + found := false + for _, e2 := range s2 { + if addrEqual(e1, e2) { + found = true + break + } + } + if !found { + diff = append(diff, e1) + } + } + + return diff +} + +// ConfigureLinkAddresses adds the provided addresses to the interface identified by index idx, if +// they are missing from the interface. Any other existing address already configured for the +// interface will be removed, unless it is a link-local address. At the moment, this function only +// supports IPv4 addresses and will ignore any address in ipNets that is not IPv4. func ConfigureLinkAddresses(idx int, ipNets []*net.IPNet) error { iface, _ := net.InterfaceByIndex(idx) ifaceName := iface.Name @@ -297,31 +326,6 @@ func ConfigureLinkAddresses(idx int, ipNets []*net.IPNet) error { } } - addrEqual := func(addr1, addr2 *net.IPNet) bool { - size1, _ := addr1.Mask.Size() - size2, _ := addr2.Mask.Size() - return addr1.IP.Equal(addr2.IP) && size1 == size2 - } - - addrSliceDifference := func(s1, s2 []*net.IPNet) []*net.IPNet { - var diff []*net.IPNet - - for _, e1 := range s1 { - found := false - for _, e2 := range s2 { - if addrEqual(e1, e2) { - found = true - break - } - } - if !found { - diff = append(diff, e1) - } - } - - return diff - } - addrsToAdd := addrSliceDifference(ipNets, addrs) addrsToRemove := addrSliceDifference(addrs, ipNets)