Skip to content

Correctly load configuration when instantiating new client without kwargs #544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions podman/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down