Skip to content

Commit 9f3c9ce

Browse files
committed
Move gw4/gw6 options to network level (with optional overrides)
1 parent 69e14cc commit 9f3c9ce

3 files changed

Lines changed: 75 additions & 11 deletions

File tree

README.md

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,20 @@ Disable assignment of IPv4 gateway IP.
5757

5858
Disable assignment of IPv6 gateway IP.
5959

60+
`gw4=IP`
61+
62+
`gw6=IP`
63+
64+
Forces assignment of a specified gateway (only if one is not provided by the IPAM module)
65+
when creating the interface. Useful for [pyipam](https://github.com/jacekkow/docker-plugin-pyipam)
66+
with `ptp=1` option and `nogw=1`/`nogw4=1`/`nogw6=1` here.
67+
68+
Using these would add routes like:
69+
```
70+
default via IP dev eth0
71+
IP dev eth0 scope link
72+
```
73+
6074
## Container creation options
6175

6276
To use these options add `--network name=network_name,driver-opt=option=value,driver-opt=option=value`
@@ -72,15 +86,7 @@ Available options:
7286

7387
`gw6=IP`
7488

75-
Forces assignment of a specified gateway (if one is not provided by the IPAM)
76-
when creating the interface. Useful for [pyipam](https://github.com/jacekkow/docker-plugin-pyipam)
77-
with "ptp=1,nogw=1" options.
78-
79-
Using these would add routes:
80-
```
81-
default via IP dev eth0
82-
IP dev eth0 scope link
83-
```
89+
Overrides network-level gw4/gw6 options.
8490

8591
## Manual packaging
8692

lib/NetworkDriver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,14 @@ def Join():
163163
result['Gateway'] = gw4.ip.compressed
164164
if gw6 is not None:
165165
result['GatewayIPv6'] = gw6.ip.compressed
166-
gw4 = endpoint.Options.get("gw4", None)
166+
gw4 = endpoint.Options.get("gw4", network.Options.get("gw4", None))
167167
if gw4 is not None:
168168
result['StaticRoutes'].append({
169169
'Destination': gw4 + '/32',
170170
'RouteType': 1,
171171
})
172172
result['Gateway'] = gw4
173-
gw6 = endpoint.Options.get("gw6", None)
173+
gw6 = endpoint.Options.get("gw6", network.Options.get("gw6", None))
174174
if gw6 is not None:
175175
result['StaticRoutes'].append({
176176
'Destination': gw6 + '/128',

test_integration.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,61 @@ if echo "${ROUTES}" | grep 2001:db8:ffff:ffff:ffff:ffff:ffff:ffff; then
193193
fi
194194

195195
docker network rm test2
196+
197+
198+
###############
199+
# Test gw4/gw6
200+
201+
docker network create \
202+
--internal \
203+
--driver "${PLUGIN}" \
204+
--opt nogw=1 \
205+
--opt gw4=192.168.254.1 \
206+
--ipam-driver jacekkow/pyipam:latest \
207+
--ipam-opt ptp=1 \
208+
--ipv6 \
209+
--subnet 192.168.255.0/24 \
210+
--subnet 2001:db8::/32 \
211+
test2
212+
213+
ADDRESSES=$(docker run --rm --network test2 \
214+
alpine \
215+
/sbin/ip addr show
216+
)
217+
if ! echo "${ADDRESSES}" | grep 192.168.255.0/32; then
218+
echo "ERROR: invalid PtP address assigned"
219+
exit 1
220+
fi
221+
if ! echo "${ADDRESSES}" | grep 2001:db8::/128; then
222+
echo "ERROR: invalid PtP address assigned"
223+
exit 1
224+
fi
225+
226+
ROUTES=$(docker run --rm --network test2 \
227+
alpine \
228+
/sbin/ip route show
229+
)
230+
if ! echo "${ROUTES}" | grep "via 192.168.254.1"; then
231+
echo "ERROR: IPv4 route not assigned with gw4=..."
232+
exit 1
233+
fi
234+
235+
ROUTES=$(docker run --rm --network test2 \
236+
alpine \
237+
/sbin/ip -6 route show
238+
)
239+
if echo "${ROUTES}" | grep default; then
240+
echo "ERROR: IPv6 route assigned with nogw=1"
241+
exit 1
242+
fi
243+
244+
ROUTES=$(docker run --rm --network name=test2,driver-opt=gw6=fe80::1 \
245+
alpine \
246+
/sbin/ip -6 route show
247+
)
248+
if ! echo "${ROUTES}" | grep "via fe80::1"; then
249+
echo "ERROR: IPv6 route not assigned with per-container gw6=..."
250+
exit 1
251+
fi
252+
253+
docker network rm test2

0 commit comments

Comments
 (0)