Skip to content

Commit 8d1f97e

Browse files
authored
Merge pull request #2407 from lemrouch/2406-fix
Macvlan internal network should not change default gateway
2 parents 264bffc + d5f5553 commit 8d1f97e

2 files changed

Lines changed: 100 additions & 78 deletions

File tree

drivers/ipvlan/ipvlan_joinleave.go

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -50,65 +50,76 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo,
5050
if ep == nil {
5151
return fmt.Errorf("could not find endpoint with id %s", eid)
5252
}
53-
if n.config.IpvlanMode == modeL3 {
54-
// disable gateway services to add a default gw using dev eth0 only
55-
jinfo.DisableGatewayService()
56-
defaultRoute, err := ifaceGateway(defaultV4RouteCidr)
57-
if err != nil {
58-
return err
59-
}
60-
if err := jinfo.AddStaticRoute(defaultRoute.Destination, defaultRoute.RouteType, defaultRoute.NextHop); err != nil {
61-
return fmt.Errorf("failed to set an ipvlan l3 mode ipv4 default gateway: %v", err)
62-
}
63-
logrus.Debugf("Ipvlan Endpoint Joined with IPv4_Addr: %s, Ipvlan_Mode: %s, Parent: %s",
64-
ep.addr.IP.String(), n.config.IpvlanMode, n.config.Parent)
65-
// If the endpoint has a v6 address, set a v6 default route
66-
if ep.addrv6 != nil {
67-
default6Route, err := ifaceGateway(defaultV6RouteCidr)
53+
if !n.config.Internal {
54+
if n.config.IpvlanMode == modeL3 {
55+
// disable gateway services to add a default gw using dev eth0 only
56+
jinfo.DisableGatewayService()
57+
defaultRoute, err := ifaceGateway(defaultV4RouteCidr)
6858
if err != nil {
6959
return err
7060
}
71-
if err = jinfo.AddStaticRoute(default6Route.Destination, default6Route.RouteType, default6Route.NextHop); err != nil {
72-
return fmt.Errorf("failed to set an ipvlan l3 mode ipv6 default gateway: %v", err)
61+
if err := jinfo.AddStaticRoute(defaultRoute.Destination, defaultRoute.RouteType, defaultRoute.NextHop); err != nil {
62+
return fmt.Errorf("failed to set an ipvlan l3 mode ipv4 default gateway: %v", err)
7363
}
74-
logrus.Debugf("Ipvlan Endpoint Joined with IPv6_Addr: %s, Ipvlan_Mode: %s, Parent: %s",
75-
ep.addrv6.IP.String(), n.config.IpvlanMode, n.config.Parent)
76-
}
77-
}
78-
if n.config.IpvlanMode == modeL2 {
79-
// parse and correlate the endpoint v4 address with the available v4 subnets
80-
if len(n.config.Ipv4Subnets) > 0 {
81-
s := n.getSubnetforIPv4(ep.addr)
82-
if s == nil {
83-
return fmt.Errorf("could not find a valid ipv4 subnet for endpoint %s", eid)
64+
logrus.Debugf("Ipvlan Endpoint Joined with IPv4_Addr: %s, Ipvlan_Mode: %s, Parent: %s",
65+
ep.addr.IP.String(), n.config.IpvlanMode, n.config.Parent)
66+
// If the endpoint has a v6 address, set a v6 default route
67+
if ep.addrv6 != nil {
68+
default6Route, err := ifaceGateway(defaultV6RouteCidr)
69+
if err != nil {
70+
return err
71+
}
72+
if err = jinfo.AddStaticRoute(default6Route.Destination, default6Route.RouteType, default6Route.NextHop); err != nil {
73+
return fmt.Errorf("failed to set an ipvlan l3 mode ipv6 default gateway: %v", err)
74+
}
75+
logrus.Debugf("Ipvlan Endpoint Joined with IPv6_Addr: %s, Ipvlan_Mode: %s, Parent: %s",
76+
ep.addrv6.IP.String(), n.config.IpvlanMode, n.config.Parent)
8477
}
85-
v4gw, _, err := net.ParseCIDR(s.GwIP)
86-
if err != nil {
87-
return fmt.Errorf("gateway %s is not a valid ipv4 address: %v", s.GwIP, err)
78+
}
79+
if n.config.IpvlanMode == modeL2 {
80+
// parse and correlate the endpoint v4 address with the available v4 subnets
81+
if len(n.config.Ipv4Subnets) > 0 {
82+
s := n.getSubnetforIPv4(ep.addr)
83+
if s == nil {
84+
return fmt.Errorf("could not find a valid ipv4 subnet for endpoint %s", eid)
85+
}
86+
v4gw, _, err := net.ParseCIDR(s.GwIP)
87+
if err != nil {
88+
return fmt.Errorf("gateway %s is not a valid ipv4 address: %v", s.GwIP, err)
89+
}
90+
err = jinfo.SetGateway(v4gw)
91+
if err != nil {
92+
return err
93+
}
94+
logrus.Debugf("Ipvlan Endpoint Joined with IPv4_Addr: %s, Gateway: %s, Ipvlan_Mode: %s, Parent: %s",
95+
ep.addr.IP.String(), v4gw.String(), n.config.IpvlanMode, n.config.Parent)
8896
}
89-
err = jinfo.SetGateway(v4gw)
90-
if err != nil {
91-
return err
97+
// parse and correlate the endpoint v6 address with the available v6 subnets
98+
if len(n.config.Ipv6Subnets) > 0 {
99+
s := n.getSubnetforIPv6(ep.addrv6)
100+
if s == nil {
101+
return fmt.Errorf("could not find a valid ipv6 subnet for endpoint %s", eid)
102+
}
103+
v6gw, _, err := net.ParseCIDR(s.GwIP)
104+
if err != nil {
105+
return fmt.Errorf("gateway %s is not a valid ipv6 address: %v", s.GwIP, err)
106+
}
107+
err = jinfo.SetGatewayIPv6(v6gw)
108+
if err != nil {
109+
return err
110+
}
111+
logrus.Debugf("Ipvlan Endpoint Joined with IPv6_Addr: %s, Gateway: %s, Ipvlan_Mode: %s, Parent: %s",
112+
ep.addrv6.IP.String(), v6gw.String(), n.config.IpvlanMode, n.config.Parent)
92113
}
93-
logrus.Debugf("Ipvlan Endpoint Joined with IPv4_Addr: %s, Gateway: %s, Ipvlan_Mode: %s, Parent: %s",
94-
ep.addr.IP.String(), v4gw.String(), n.config.IpvlanMode, n.config.Parent)
95114
}
96-
// parse and correlate the endpoint v6 address with the available v6 subnets
115+
} else {
116+
if len(n.config.Ipv4Subnets) > 0 {
117+
logrus.Debugf("Ipvlan Endpoint Joined with IPv4_Addr: %s, IpVlan_Mode: %s, Parent: %s",
118+
ep.addr.IP.String(), n.config.IpvlanMode, n.config.Parent)
119+
}
97120
if len(n.config.Ipv6Subnets) > 0 {
98-
s := n.getSubnetforIPv6(ep.addrv6)
99-
if s == nil {
100-
return fmt.Errorf("could not find a valid ipv6 subnet for endpoint %s", eid)
101-
}
102-
v6gw, _, err := net.ParseCIDR(s.GwIP)
103-
if err != nil {
104-
return fmt.Errorf("gateway %s is not a valid ipv6 address: %v", s.GwIP, err)
105-
}
106-
err = jinfo.SetGatewayIPv6(v6gw)
107-
if err != nil {
108-
return err
109-
}
110-
logrus.Debugf("Ipvlan Endpoint Joined with IPv6_Addr: %s, Gateway: %s, Ipvlan_Mode: %s, Parent: %s",
111-
ep.addrv6.IP.String(), v6gw.String(), n.config.IpvlanMode, n.config.Parent)
121+
logrus.Debugf("Ipvlan Endpoint Joined with IPv6_Addr: %s IpVlan_Mode: %s, Parent: %s",
122+
ep.addrv6.IP.String(), n.config.IpvlanMode, n.config.Parent)
112123
}
113124
}
114125
iNames := jinfo.InterfaceName()

drivers/macvlan/macvlan_joinleave.go

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,38 +39,49 @@ func (d *driver) Join(nid, eid string, sboxKey string, jinfo driverapi.JoinInfo,
3939
return fmt.Errorf("could not find endpoint with id %s", eid)
4040
}
4141
// parse and match the endpoint address with the available v4 subnets
42-
if len(n.config.Ipv4Subnets) > 0 {
43-
s := n.getSubnetforIPv4(ep.addr)
44-
if s == nil {
45-
return fmt.Errorf("could not find a valid ipv4 subnet for endpoint %s", eid)
42+
if !n.config.Internal {
43+
if len(n.config.Ipv4Subnets) > 0 {
44+
s := n.getSubnetforIPv4(ep.addr)
45+
if s == nil {
46+
return fmt.Errorf("could not find a valid ipv4 subnet for endpoint %s", eid)
47+
}
48+
v4gw, _, err := net.ParseCIDR(s.GwIP)
49+
if err != nil {
50+
return fmt.Errorf("gateway %s is not a valid ipv4 address: %v", s.GwIP, err)
51+
}
52+
err = jinfo.SetGateway(v4gw)
53+
if err != nil {
54+
return err
55+
}
56+
logrus.Debugf("Macvlan Endpoint Joined with IPv4_Addr: %s, Gateway: %s, MacVlan_Mode: %s, Parent: %s",
57+
ep.addr.IP.String(), v4gw.String(), n.config.MacvlanMode, n.config.Parent)
4658
}
47-
v4gw, _, err := net.ParseCIDR(s.GwIP)
48-
if err != nil {
49-
return fmt.Errorf("gateway %s is not a valid ipv4 address: %v", s.GwIP, err)
50-
}
51-
err = jinfo.SetGateway(v4gw)
52-
if err != nil {
53-
return err
59+
// parse and match the endpoint address with the available v6 subnets
60+
if len(n.config.Ipv6Subnets) > 0 {
61+
s := n.getSubnetforIPv6(ep.addrv6)
62+
if s == nil {
63+
return fmt.Errorf("could not find a valid ipv6 subnet for endpoint %s", eid)
64+
}
65+
v6gw, _, err := net.ParseCIDR(s.GwIP)
66+
if err != nil {
67+
return fmt.Errorf("gateway %s is not a valid ipv6 address: %v", s.GwIP, err)
68+
}
69+
err = jinfo.SetGatewayIPv6(v6gw)
70+
if err != nil {
71+
return err
72+
}
73+
logrus.Debugf("Macvlan Endpoint Joined with IPv6_Addr: %s Gateway: %s MacVlan_Mode: %s, Parent: %s",
74+
ep.addrv6.IP.String(), v6gw.String(), n.config.MacvlanMode, n.config.Parent)
5475
}
55-
logrus.Debugf("Macvlan Endpoint Joined with IPv4_Addr: %s, Gateway: %s, MacVlan_Mode: %s, Parent: %s",
56-
ep.addr.IP.String(), v4gw.String(), n.config.MacvlanMode, n.config.Parent)
57-
}
58-
// parse and match the endpoint address with the available v6 subnets
59-
if len(n.config.Ipv6Subnets) > 0 {
60-
s := n.getSubnetforIPv6(ep.addrv6)
61-
if s == nil {
62-
return fmt.Errorf("could not find a valid ipv6 subnet for endpoint %s", eid)
76+
} else {
77+
if len(n.config.Ipv4Subnets) > 0 {
78+
logrus.Debugf("Macvlan Endpoint Joined with IPv4_Addr: %s, MacVlan_Mode: %s, Parent: %s",
79+
ep.addr.IP.String(), n.config.MacvlanMode, n.config.Parent)
6380
}
64-
v6gw, _, err := net.ParseCIDR(s.GwIP)
65-
if err != nil {
66-
return fmt.Errorf("gateway %s is not a valid ipv6 address: %v", s.GwIP, err)
67-
}
68-
err = jinfo.SetGatewayIPv6(v6gw)
69-
if err != nil {
70-
return err
81+
if len(n.config.Ipv6Subnets) > 0 {
82+
logrus.Debugf("Macvlan Endpoint Joined with IPv6_Addr: %s MacVlan_Mode: %s, Parent: %s",
83+
ep.addrv6.IP.String(), n.config.MacvlanMode, n.config.Parent)
7184
}
72-
logrus.Debugf("Macvlan Endpoint Joined with IPv6_Addr: %s Gateway: %s MacVlan_Mode: %s, Parent: %s",
73-
ep.addrv6.IP.String(), v6gw.String(), n.config.MacvlanMode, n.config.Parent)
7485
}
7586
iNames := jinfo.InterfaceName()
7687
err = iNames.SetNames(vethName, containerVethPrefix)

0 commit comments

Comments
 (0)