Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Commit

Permalink
Avoid crash when network has no gateway
Browse files Browse the repository at this point in the history
A route doesn't have to have a gateway; it can be directed to a device.
  • Loading branch information
bboreham committed Dec 16, 2020
1 parent 915d489 commit a315f7c
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pkg/container/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,15 @@ func takeAddress(iface *net.Interface) (*net.IPNet, *net.IP, bool, error) {
}

var gw *net.IP
gwString := "<nil>"
routes, err := netlink.RouteList(link, netlink.FAMILY_ALL)
if err != nil {
return nil, nil, false, fmt.Errorf("failed to get default gateway for interface %q: %v", iface.Name, err)
}
for _, rt := range routes {
if rt.Gw != nil {
gw = &rt.Gw
gwString = gw.String()
break
}
}
Expand All @@ -197,7 +199,7 @@ func takeAddress(iface *net.Interface) (*net.IPNet, *net.IP, bool, error) {
return nil, nil, false, fmt.Errorf("failed to remove address %q from interface %q: %v", delAddr, iface.Name, err)
}

log.Infof("Moving IP address %s (%s) with gateway %s from container to VM", ip.String(), maskString(mask), gw.String())
log.Infof("Moving IP address %s (%s) with gateway %s from container to VM", ip.String(), maskString(mask), gwString)

return &net.IPNet{
IP: ip,
Expand Down

0 comments on commit a315f7c

Please sign in to comment.