Skip to content

Commit 64b1a83

Browse files
authored
Merge pull request #15 from PouuleT/routeForGw
Add route for gateway before adding gw as default
2 parents 9f2fce2 + deec883 commit 64b1a83

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ func main() {
210210
log.Warn("Error while setting up the interface TC attributes: ", err)
211211
return
212212
}
213-
log.Debugf("Set %s as the route", gwaddr)
214213

215214
// ============================= Set the default route in the namespace
216215

net.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"net"
56

67
"github.com/vishvananda/netlink"
@@ -60,6 +61,25 @@ func newMacVLAN() (*netlink.Link, error) {
6061
}
6162

6263
func setLinkRoute(link *netlink.Link) error {
64+
// Add a route for the gateway
65+
_, gwaddrNet, err := net.ParseCIDR(fmt.Sprintf("%s/32", gwaddr.String()))
66+
if err != nil {
67+
log.Warnf("Error when parsing gateway", err)
68+
return err
69+
}
70+
log.Debug("Setting a route for the gateway")
71+
err = netlink.RouteAdd(&netlink.Route{
72+
Scope: netlink.SCOPE_UNIVERSE,
73+
LinkIndex: (*link).Attrs().Index,
74+
Dst: gwaddrNet,
75+
})
76+
if err != nil {
77+
log.Warn("Error while setting link route: ", err)
78+
return err
79+
}
80+
81+
log.Debugf("Set %s as the default gateway", gwaddr)
82+
// Add the default gateway
6383
return netlink.RouteAdd(&netlink.Route{
6484
Scope: netlink.SCOPE_UNIVERSE,
6585
LinkIndex: (*link).Attrs().Index,

0 commit comments

Comments
 (0)