Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
antoninbas committed Feb 25, 2021
1 parent a41787f commit 3b086f4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 44 deletions.
41 changes: 22 additions & 19 deletions pkg/agent/util/net_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

Expand Down
54 changes: 29 additions & 25 deletions pkg/agent/util/net_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down

0 comments on commit 3b086f4

Please sign in to comment.