From 8707af9c006d5ba319ec8d358d836869c843547f Mon Sep 17 00:00:00 2001 From: wanyunSu Date: Tue, 9 Dec 2025 12:25:35 +0100 Subject: [PATCH] bring back hostname --- src/drunc/controller/interface/shell_utils.py | 41 ++++++------------- .../process_manager/k8s_process_manager.py | 4 +- 2 files changed, 14 insertions(+), 31 deletions(-) diff --git a/src/drunc/controller/interface/shell_utils.py b/src/drunc/controller/interface/shell_utils.py index 399d4687f..cc60dc13c 100644 --- a/src/drunc/controller/interface/shell_utils.py +++ b/src/drunc/controller/interface/shell_utils.py @@ -716,27 +716,8 @@ def grab_default_value_from_env(argument_name): return cmd, cmd_name -@lru_cache(maxsize=1024) -def is_private_ip(ip_str: str) -> bool: - """ - Checks if an IP address is private (RFC 1918), loopback, or link-local. - These IPs will almost never have a public reverse DNS record. - """ - if not ip_str: - return True - try: - ip_obj = ipaddress.ip_address(ip_str) - # .is_private = 10.x, 172.16-31.x, 192.168.x - # .is_loopback = 127.x.x.x - # .is_link_local = 169.254.x.x - return ip_obj.is_private or ip_obj.is_loopback or ip_obj.is_link_local - except ValueError: - # Not 'valid' IP address -> treat as private - return True - - @lru_cache(maxsize=4096) -def get_hostname_smart(ip_address: str, timeout_seconds: float = 0.2) -> str: +def get_hostname_smart(ip_or_host: str, timeout_seconds: float = 0.2) -> str: """ Resolves an IP to a hostname, with optimizations: 1. Caches all results. @@ -744,20 +725,22 @@ def get_hostname_smart(ip_address: str, timeout_seconds: float = 0.2) -> str: 3. Uses a short timeout for public IPs. """ - # If private IP (k8s), don't try to resolve it - if is_private_ip(ip_address): - return ip_address + if not ip_or_host: + return "" + try: + ip_address = ipaddress.ip_address(ip_or_host) + except ValueError: + return ip_or_host # If public IP, try to resolve it. original_timeout = socket.getdefaulttimeout() try: socket.setdefaulttimeout(timeout_seconds) - - hostname, _, _ = socket.gethostbyaddr(ip_address) - return hostname - - except (socket.herror, socket.gaierror, socket.timeout): - return ip_address + try: + hostname, _, _ = socket.gethostbyaddr(str(ip_address)) + return hostname + except (socket.herror, socket.gaierror, socket.timeout, OSError): + return ip_or_host finally: socket.setdefaulttimeout(original_timeout) diff --git a/src/drunc/process_manager/k8s_process_manager.py b/src/drunc/process_manager/k8s_process_manager.py index 076f37d15..58787093e 100644 --- a/src/drunc/process_manager/k8s_process_manager.py +++ b/src/drunc/process_manager/k8s_process_manager.py @@ -711,7 +711,7 @@ def _build_pod_main_container( if log_file_path: self.log.info(f"Redirecting pod stdout/stderr to '{log_file_path}'") - log_redirect_cmd = f"exec > {log_file_path} 2>&1;" + log_redirect_cmd = f"exec > >(tee -a {log_file_path}) 2>&1;" else: log_redirect_cmd = "" @@ -730,7 +730,7 @@ def _build_pod_main_container( main_container = client.V1Container( name=podname, image=pod_image, - command=["/bin/sh", "-c"], + command=["/bin/bash", "-c"], args=[final_command_args], env=[ client.V1EnvVar(name=k, value=v)