From 4c537ee3bca041a7062e076ccd8db5dd0db83b90 Mon Sep 17 00:00:00 2001 From: Anish Asthana Date: Fri, 25 Apr 2025 14:27:10 -0400 Subject: [PATCH] Correctly load configuration when instantiating new client without kwargs The init function of PodmanClient was never loading the system configuration when no kwargs were specified. This change makes it so that we load the core data (base_url and identity) from the config file. Signed-off-by: Anish Asthana --- podman/client.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/podman/client.py b/podman/client.py index f9a023e7..ef41db9c 100644 --- a/podman/client.py +++ b/podman/client.py @@ -59,18 +59,32 @@ def __init__(self, **kwargs) -> None: """ super().__init__() config = PodmanConfig() - api_kwargs = kwargs.copy() + # Case 1: Use named connection from kwargs if specified if "connection" in api_kwargs: + logger.debug("Using kwargs") connection = config.services[api_kwargs.get("connection")] api_kwargs["base_url"] = connection.url.geturl() - + logger.debug("Connection URL: %s", api_kwargs["base_url"]) # Override configured identity, if provided in arguments api_kwargs["identity"] = kwargs.get("identity", str(connection.identity)) - elif "base_url" not in api_kwargs: + + # Case 2: No kwargs provided - try to load from system configuration + elif not api_kwargs and "Connection" in config.attrs: + logger.debug("Using System Configuration") + default_connection_name = config.attrs["Connection"]["Default"] + connection = config.attrs["Connection"]["Connections"][default_connection_name] + api_kwargs["base_url"] = connection["URI"] + api_kwargs["identity"] = connection["Identity"] + logger.debug("Connection URL: %s", api_kwargs["base_url"]) + + # Case 3: Fallback - if no base_url is specified, use default socket path + if "base_url" not in api_kwargs: path = str(Path(get_runtime_dir()) / "podman" / "podman.sock") api_kwargs["base_url"] = "http+unix://" + path + logger.debug("Using Default Socket Path: %s", api_kwargs["base_url"]) + self.api = APIClient(**api_kwargs) def __enter__(self) -> "PodmanClient":