Skip to content

Commit 6f146d6

Browse files
twangboydwoz
authored andcommitted
Trim cruft and harden the Docker gateway idempotency fix
- systemd_service: drop the else-raise added to enable()/disable()'s sysvinit paths. It's unreachable (_get_service_exec() only ever returns a path ending in update-rc.d or chkconfig, or raises first) and untested; its commit rationale actually described the _runlevel() IndexError, which is fixed separately. - dockermod.compare_networks: tighten the auto-assigned-Gateway exception so it only ignores a one-sided Gateway when it equals the subnet's Docker- assigned default (network address + 1). Previously any one-sided Gateway was stripped unconditionally, which would mask a real change if a user explicitly set a non-default gateway and then removed it. Add unit tests covering the auto-assigned case, the non-default-gateway-removed case, and a changed-explicit-gateway case, mirroring the existing test_compare_networks_ipam_config_empty_iprange regression test. Add a changelog entry describing the fix accurately: the auto-gateway/no-gateway mismatch is structural (any Docker version that auto-assigns gateways), not specific to a newer Docker engine.
1 parent 605a67c commit 6f146d6

4 files changed

Lines changed: 151 additions & 18 deletions

File tree

‎changelog/69746.fixed.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed ``docker_network.present`` reporting spurious changes and recreating a network on every run when a ``subnet`` was specified without a ``gateway``. Docker auto-assigns the subnet's first host address as the gateway and reports it on inspect, while Salt's desired config omits the key entirely; ``docker.compare_networks`` now ignores a one-sided gateway only when it matches that auto-assigned default, so an explicitly added, removed, or changed gateway is still detected as a real change.

‎salt/modules/dockermod.py‎

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@
201201
import fnmatch
202202
import functools
203203
import gzip
204+
import ipaddress
204205
import json
205206
import logging
206207
import os
@@ -1288,6 +1289,21 @@ def _check_ipconfig(ret, net_name, **kwargs):
12881289
return ret
12891290

12901291

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+
12911307
def compare_networks(first, second, ignore="Name,Id,Created,Containers,Status"):
12921308
"""
12931309
.. versionadded:: 2018.3.0
@@ -1359,18 +1375,26 @@ def config_changed(pools1, pools2):
13591375
return True
13601376
for subnet_key, pool1 in by_subnet1.items():
13611377
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.
13661384
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+
}
13701391
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+
}
13741398
if pool1 != pool2:
13751399
return True
13761400
return False

‎salt/modules/systemd_service.py‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,10 +1212,6 @@ def enable(
12121212
cmd.extend([service_exec, "-f", name, "defaults", "99"])
12131213
elif service_exec.endswith("/chkconfig"):
12141214
cmd.extend([service_exec, name, "on"])
1215-
else:
1216-
raise CommandExecutionError(
1217-
f"Unrecognized sysv service manager: {service_exec}"
1218-
)
12191215
return (
12201216
__salt__["cmd.retcode"](cmd, python_shell=False, ignore_retcode=True) == 0
12211217
)
@@ -1280,10 +1276,6 @@ def disable(
12801276
cmd.extend([service_exec, "-f", name, "remove"])
12811277
elif service_exec.endswith("/chkconfig"):
12821278
cmd.extend([service_exec, name, "off"])
1283-
else:
1284-
raise CommandExecutionError(
1285-
f"Unrecognized sysv service manager: {service_exec}"
1286-
)
12871279
return (
12881280
__salt__["cmd.retcode"](cmd, python_shell=False, ignore_retcode=True) == 0
12891281
)

‎tests/pytests/unit/modules/dockermod/test_module.py‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,122 @@ def test_compare_networks_ipam_config_empty_iprange():
496496
assert "IPAM" in docker_mod.compare_networks(existing, desired)
497497

498498

499+
def test_compare_networks_ipam_config_auto_assigned_gateway():
500+
"""
501+
When a Subnet is specified without a Gateway, Docker auto-assigns the
502+
subnet's first host address as the Gateway and reports it on inspect.
503+
Salt's desired config (built without an explicit gateway) omits the key
504+
entirely, so a strict comparison reported a spurious diff and
505+
``docker_network.present`` would recreate the network on every run.
506+
"""
507+
existing = {
508+
"Name": "inf-network",
509+
"Driver": "bridge",
510+
"IPAM": {
511+
"Driver": "default",
512+
"Options": None,
513+
"Config": [
514+
{
515+
"Subnet": "10.247.197.96/27",
516+
"Gateway": "10.247.197.97",
517+
}
518+
],
519+
},
520+
"Options": {},
521+
}
522+
desired = {
523+
"Name": "inf-network",
524+
"Driver": "bridge",
525+
"IPAM": {
526+
"Driver": "default",
527+
"Options": {},
528+
"Config": [
529+
{
530+
"Subnet": "10.247.197.96/27",
531+
}
532+
],
533+
},
534+
"Options": {},
535+
}
536+
assert docker_mod.compare_networks(existing, desired) == {}
537+
538+
539+
def test_compare_networks_ipam_config_explicit_gateway_removed():
540+
"""
541+
Removing a real, non-default Gateway (one the user explicitly set, not
542+
the one Docker auto-assigns) must still be reported as a change -- the
543+
auto-assigned-gateway exception must not mask a genuine one-sided diff.
544+
"""
545+
existing = {
546+
"Name": "inf-network",
547+
"Driver": "bridge",
548+
"IPAM": {
549+
"Driver": "default",
550+
"Options": None,
551+
"Config": [
552+
{
553+
"Subnet": "10.247.197.96/27",
554+
"Gateway": "10.247.197.100",
555+
}
556+
],
557+
},
558+
"Options": {},
559+
}
560+
desired = {
561+
"Name": "inf-network",
562+
"Driver": "bridge",
563+
"IPAM": {
564+
"Driver": "default",
565+
"Options": {},
566+
"Config": [
567+
{
568+
"Subnet": "10.247.197.96/27",
569+
}
570+
],
571+
},
572+
"Options": {},
573+
}
574+
assert "IPAM" in docker_mod.compare_networks(existing, desired)
575+
576+
577+
def test_compare_networks_ipam_config_gateway_changed():
578+
"""
579+
Changing an explicit Gateway to a different explicit Gateway (both sides
580+
non-default) must still be reported as a change.
581+
"""
582+
existing = {
583+
"Name": "inf-network",
584+
"Driver": "bridge",
585+
"IPAM": {
586+
"Driver": "default",
587+
"Options": None,
588+
"Config": [
589+
{
590+
"Subnet": "10.247.197.96/27",
591+
"Gateway": "10.247.197.97",
592+
}
593+
],
594+
},
595+
"Options": {},
596+
}
597+
desired = {
598+
"Name": "inf-network",
599+
"Driver": "bridge",
600+
"IPAM": {
601+
"Driver": "default",
602+
"Options": {},
603+
"Config": [
604+
{
605+
"Subnet": "10.247.197.96/27",
606+
"Gateway": "10.247.197.98",
607+
}
608+
],
609+
},
610+
"Options": {},
611+
}
612+
assert "IPAM" in docker_mod.compare_networks(existing, desired)
613+
614+
499615
@docker_older_than_1_5_0_skip_marker
500616
def test_connect_container_to_network():
501617
"""

0 commit comments

Comments
 (0)