From 84da957a5cf880463062da51119a81aa98e202a8 Mon Sep 17 00:00:00 2001 From: Jonathan Rosser Date: Wed, 27 Dec 2023 13:40:11 +0000 Subject: [PATCH] Handle no default route in get_default_ip_address() (#279) A host is not required to have a default gateway, more specific routes may be present instead. In this situation get_default_gateway_interface() returns None, which is reasonable. get_default_ip_address() then fails with a struct pack error. This patch allows get_default_ip_address to fall through and also return None when there is no interface identified as the default gateway. Co-authored-by: Jonathan Rosser --- magnum_cluster_api/proxy/utils.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/magnum_cluster_api/proxy/utils.py b/magnum_cluster_api/proxy/utils.py index f8e3b9d4..ecb6b16e 100644 --- a/magnum_cluster_api/proxy/utils.py +++ b/magnum_cluster_api/proxy/utils.py @@ -29,15 +29,16 @@ def get_default_gateway_interface(): def get_default_ip_address(): interface = get_default_gateway_interface() - sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - ip_address = socket.inet_ntoa( - fcntl.ioctl( - sock.fileno(), - 0x8915, # SIOCGIFADDR - struct.pack("256s", interface.encode("utf-8")[:15]), - )[20:24] - ) - return ip_address + if interface: + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + ip_address = socket.inet_ntoa( + fcntl.ioctl( + sock.fileno(), + 0x8915, # SIOCGIFADDR + struct.pack("256s", interface.encode("utf-8")[:15]), + )[20:24] + ) + return ip_address def find_free_port():