Skip to content

Commit

Permalink
UT ipassigner linux
Browse files Browse the repository at this point in the history
Signed-off-by: Rajnish Kumar <[email protected]>
  • Loading branch information
rajnkamr committed Sep 29, 2023
1 parent 4806be3 commit c730c77
Show file tree
Hide file tree
Showing 4 changed files with 487 additions and 7 deletions.
1 change: 1 addition & 0 deletions hack/update-codegen-dockerized.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ MOCKGEN_TARGETS=(
"pkg/agent/querier AgentQuerier testing"
"pkg/agent/route Interface testing"
"pkg/agent/ipassigner IPAssigner testing"
"pkg/agent/ipassigner/responder Responder testing"
"pkg/agent/secondarynetwork/podwatch InterfaceConfigurator testing"
"pkg/agent/secondarynetwork/ipam IPAMDelegator testing"
"pkg/agent/servicecidr Interface testing"
Expand Down
23 changes: 16 additions & 7 deletions pkg/agent/ipassigner/ip_assigner_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ import (
"antrea.io/antrea/pkg/agent/util/sysctl"
)

var (
netlinkAddrAdd = netlink.AddrAdd
netlinkAddrDel = netlink.AddrDel
)

type advertiseArpNdp func(a *ipAssigner, ip net.IP)

var advertiseResponder advertiseArpNdp = (*ipAssigner).advertise

// ipAssigner creates a dummy device and assigns IPs to it.
// It's supposed to be used in the cases that external IPs should be configured on the system so that they can be used
// for SNAT (egress scenario) or DNAT (ingress scenario). A dummy device is used because the IPs just need to be present
Expand Down Expand Up @@ -154,14 +163,14 @@ func (a *ipAssigner) AssignIP(ip string, forceAdvertise bool) error {
if a.assignedIPs.Has(ip) {
klog.V(2).InfoS("The IP is already assigned", "ip", ip)
if forceAdvertise {
a.advertise(parsedIP)
advertiseResponder(a, parsedIP)
}
return nil
}

if a.dummyDevice != nil {
addr := util.NewIPNet(parsedIP)
if err := netlink.AddrAdd(a.dummyDevice, &netlink.Addr{IPNet: addr}); err != nil {
if err := netlinkAddrAdd(a.dummyDevice, &netlink.Addr{IPNet: addr}); err != nil {
if !errors.Is(err, unix.EEXIST) {
return fmt.Errorf("failed to add IP %v to interface %s: %v", ip, a.dummyDevice.Attrs().Name, err)
} else {
Expand All @@ -183,7 +192,7 @@ func (a *ipAssigner) AssignIP(ip string, forceAdvertise bool) error {
}
}
// Always advertise the IP when the IP is newly assigned to this Node.
a.advertise(parsedIP)
advertiseResponder(a, parsedIP)
a.assignedIPs.Insert(ip)
return nil
}
Expand Down Expand Up @@ -218,7 +227,7 @@ func (a *ipAssigner) UnassignIP(ip string) error {

if a.dummyDevice != nil {
addr := util.NewIPNet(parsedIP)
if err := netlink.AddrDel(a.dummyDevice, &netlink.Addr{IPNet: addr}); err != nil {
if err := netlinkAddrDel(a.dummyDevice, &netlink.Addr{IPNet: addr}); err != nil {
if !errors.Is(err, unix.EADDRNOTAVAIL) {
return fmt.Errorf("failed to delete IP %v from interface %s: %v", ip, a.dummyDevice.Attrs().Name, err)
} else {
Expand Down Expand Up @@ -264,15 +273,15 @@ func (a *ipAssigner) InitIPs(ips sets.Set[string]) error {
}
for ip := range ips.Difference(assigned) {
addr := util.NewIPNet(net.ParseIP(ip))
if err := netlink.AddrAdd(a.dummyDevice, &netlink.Addr{IPNet: addr}); err != nil {
if err := netlinkAddrAdd(a.dummyDevice, &netlink.Addr{IPNet: addr}); err != nil {
if !errors.Is(err, unix.EEXIST) {
return fmt.Errorf("failed to add IP %v to interface %s: %v", ip, a.dummyDevice.Attrs().Name, err)
}
}
}
for ip := range assigned.Difference(ips) {
addr := util.NewIPNet(net.ParseIP(ip))
if err := netlink.AddrDel(a.dummyDevice, &netlink.Addr{IPNet: addr}); err != nil {
if err := netlinkAddrDel(a.dummyDevice, &netlink.Addr{IPNet: addr}); err != nil {
if !errors.Is(err, unix.EADDRNOTAVAIL) {
return fmt.Errorf("failed to delete IP %v from interface %s: %v", ip, a.dummyDevice.Attrs().Name, err)
}
Expand All @@ -291,7 +300,7 @@ func (a *ipAssigner) InitIPs(ips sets.Set[string]) error {
if err != nil {
return err
}
a.advertise(ip)
advertiseResponder(a, ip)
}
a.assignedIPs = ips.Union(nil)
return nil
Expand Down
Loading

0 comments on commit c730c77

Please sign in to comment.