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

Commit

Permalink
Merge pull request #749 from weaveworks/dont-crash-on-nil-gw
Browse files Browse the repository at this point in the history
Avoid crash when network has no gateway
  • Loading branch information
stealthybox authored Jan 23, 2021
2 parents 58d0e53 + 7bb6815 commit f7266d1
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 f7266d1

Please sign in to comment.