Fix idempotency problem for IPv6 addresses.

This commit is contained in:
Felix Fontein 2025-11-14 19:54:25 +01:00
parent 6ad4678364
commit b3723a30c3
3 changed files with 31 additions and 6 deletions

View File

@ -1,2 +1,3 @@
bugfixes: bugfixes:
- "docker_container - fix ``pull`` idempotency with Docker 29.0.0 (https://github.com/ansible-collections/community.docker/pull/1192)." - "docker_container - fix ``pull`` idempotency with Docker 29.0.0 (https://github.com/ansible-collections/community.docker/pull/1192)."
- "docker_container - fix idempotency for IPv6 addresses with Docker 29.0.0 (https://github.com/ansible-collections/community.docker/pull/1192)."

View File

@ -25,6 +25,7 @@ from ansible_collections.community.docker.plugins.module_utils._util import (
DockerBaseClass, DockerBaseClass,
compare_generic, compare_generic,
is_image_name_id, is_image_name_id,
normalize_ip_address,
sanitize_result, sanitize_result,
) )
@ -925,13 +926,13 @@ class ContainerManager(DockerBaseClass, t.Generic[Client]):
else: else:
diff = False diff = False
network_info_ipam = network_info.get("IPAMConfig") or {} network_info_ipam = network_info.get("IPAMConfig") or {}
if network.get("ipv4_address") and network[ if network.get("ipv4_address") and normalize_ip_address(
"ipv4_address" network["ipv4_address"]
] != network_info_ipam.get("IPv4Address"): ) != normalize_ip_address(network_info_ipam.get("IPv4Address")):
diff = True diff = True
if network.get("ipv6_address") and network[ if network.get("ipv6_address") and normalize_ip_address(
"ipv6_address" network["ipv6_address"]
] != network_info_ipam.get("IPv6Address"): ) != normalize_ip_address(network_info_ipam.get("IPv6Address")):
diff = True diff = True
if network.get("aliases") and not compare_generic( if network.get("aliases") and not compare_generic(
network["aliases"], network["aliases"],

View File

@ -7,6 +7,7 @@
from __future__ import annotations from __future__ import annotations
import ipaddress
import json import json
import re import re
import typing as t import typing as t
@ -505,3 +506,25 @@ def omit_none_from_dict(d: dict[str, t.Any]) -> dict[str, t.Any]:
Return a copy of the dictionary with all keys with value None omitted. Return a copy of the dictionary with all keys with value None omitted.
""" """
return {k: v for (k, v) in d.items() if v is not None} return {k: v for (k, v) in d.items() if v is not None}
@t.overload
def normalize_ip_address(ip_address: str) -> str: ...
@t.overload
def normalize_ip_address(ip_address: str | None) -> str | None: ...
def normalize_ip_address(ip_address: str | None) -> str | None:
"""
Given an IP address as a string, normalize it so that it can be
used to compare IP addresses as strings.
"""
if ip_address is None:
return None
try:
return ipaddress.ip_address(ip_address).compressed
except ValueError:
# Fallback for invalid addresses: simply return the input
return ip_address