|
201 | 201 | import fnmatch |
202 | 202 | import functools |
203 | 203 | import gzip |
| 204 | +import ipaddress |
204 | 205 | import json |
205 | 206 | import logging |
206 | 207 | import os |
@@ -1288,6 +1289,21 @@ def _check_ipconfig(ret, net_name, **kwargs): |
1288 | 1289 | return ret |
1289 | 1290 |
|
1290 | 1291 |
|
| 1292 | +def _is_auto_assigned_gateway(subnet, gateway): |
| 1293 | + """ |
| 1294 | + Docker auto-assigns the subnet's first host address as the gateway when |
| 1295 | + none is explicitly requested. Only such an auto-assigned gateway should be |
| 1296 | + ignored when comparing IPAM pools; an explicit, non-default gateway is a |
| 1297 | + real configuration difference. |
| 1298 | + """ |
| 1299 | + try: |
| 1300 | + network = ipaddress.ip_network(subnet, strict=False) |
| 1301 | + gateway_addr = ipaddress.ip_address(gateway) |
| 1302 | + except (ValueError, TypeError): |
| 1303 | + return False |
| 1304 | + return gateway_addr == network.network_address + 1 |
| 1305 | + |
| 1306 | + |
1291 | 1307 | def compare_networks(first, second, ignore="Name,Id,Created,Containers,Status"): |
1292 | 1308 | """ |
1293 | 1309 | .. versionadded:: 2018.3.0 |
@@ -1359,18 +1375,26 @@ def config_changed(pools1, pools2): |
1359 | 1375 | return True |
1360 | 1376 | for subnet_key, pool1 in by_subnet1.items(): |
1361 | 1377 | pool2 = by_subnet2[subnet_key] |
1362 | | - # Some Docker engine versions auto-assign a |
1363 | | - # Gateway for a pool where none was explicitly |
1364 | | - # requested (e.g. when only a Subnet was given). |
1365 | | - # Don't treat that as a real difference. |
| 1378 | + # Docker auto-assigns a Gateway for a pool where |
| 1379 | + # none was explicitly requested (e.g. when only a |
| 1380 | + # Subnet was given). Ignore a one-sided Gateway |
| 1381 | + # only when it matches that auto-assigned default; |
| 1382 | + # an explicit, non-default Gateway that was added |
| 1383 | + # or removed is a real configuration change. |
1366 | 1384 | if "Gateway" in pool1 and "Gateway" not in pool2: |
1367 | | - pool1 = { |
1368 | | - k: v for k, v in pool1.items() if k != "Gateway" |
1369 | | - } |
| 1385 | + if _is_auto_assigned_gateway( |
| 1386 | + subnet_key, pool1["Gateway"] |
| 1387 | + ): |
| 1388 | + pool1 = { |
| 1389 | + k: v for k, v in pool1.items() if k != "Gateway" |
| 1390 | + } |
1370 | 1391 | elif "Gateway" in pool2 and "Gateway" not in pool1: |
1371 | | - pool2 = { |
1372 | | - k: v for k, v in pool2.items() if k != "Gateway" |
1373 | | - } |
| 1392 | + if _is_auto_assigned_gateway( |
| 1393 | + subnet_key, pool2["Gateway"] |
| 1394 | + ): |
| 1395 | + pool2 = { |
| 1396 | + k: v for k, v in pool2.items() if k != "Gateway" |
| 1397 | + } |
1374 | 1398 | if pool1 != pool2: |
1375 | 1399 | return True |
1376 | 1400 | return False |
|
0 commit comments