Skip to content
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

Update pod component to use generic Iterable and Mapping types #599

Merged
Show file tree
Hide file tree
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
90 changes: 43 additions & 47 deletions python_on_whales/components/pod/cli_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
from datetime import datetime, timedelta
from typing import (
Any,
Dict,
Iterable,
List,
Mapping,
Optional,
Sequence,
Tuple,
TypedDict,
Union,
Expand Down Expand Up @@ -259,17 +257,17 @@ def create(
self,
name: Optional[str] = None,
*,
add_hosts: List[Tuple[str, str]] = [],
add_hosts: Iterable[Tuple[str, str]] = (),
cgroup_parent: Optional[str] = None,
cpus: Optional[float] = None,
cpuset_cpus: Optional[List[int]] = None,
devices: List[str] = [],
device_read_bps: List[str] = [],
dns: List[str] = [],
dns_options: List[str] = [],
dns_search: List[str] = [],
cpuset_cpus: Optional[Iterable[int]] = None,
devices: Iterable[str] = (),
device_read_bps: Iterable[str] = (),
dns: Iterable[str] = (),
dns_options: Iterable[str] = (),
dns_search: Iterable[str] = (),
exit_policy: Optional[str] = None,
gidmaps: List[Tuple[int, int, int]] = [],
gidmaps: Iterable[Tuple[int, int, int]] = (),
hostname: Optional[str] = None,
infra: Optional[bool] = None,
infra_command: Optional[str] = None,
Expand All @@ -280,33 +278,33 @@ def create(
infra_name: Optional[str] = None,
ip: Optional[str] = None,
ip6: Optional[str] = None,
labels: Dict[str, str] = {},
label_files: List[ValidPath] = [],
labels: Mapping[str, str] = {},
label_files: Iterable[ValidPath] = (),
mac_address: Optional[str] = None,
memory: Union[int, str, None] = None,
networks: List[
networks: Iterable[
python_on_whales.components.network.cli_wrapper.ValidNetwork
] = [],
network_aliases: List[str] = [],
] = (),
network_aliases: Iterable[str] = (),
no_hosts: bool = False,
pid: Optional[str] = None,
pod_id_file: Optional[ValidPath] = None,
publish: List[ValidPortMapping] = [],
publish: Iterable[ValidPortMapping] = (),
replace: bool = False,
restart: Optional[str] = None,
security_options: List[str] = [],
share: Optional[List[str]] = None,
security_options: Iterable[str] = (),
share: Optional[Iterable[str]] = None,
shm_size: Optional[Union[int, str]] = None,
subgidname: Optional[str] = None,
subuidname: Optional[str] = None,
sysctl: Dict[str, str] = {},
uidmaps: List[Tuple[int, int, int]] = [],
sysctl: Mapping[str, str] = {},
uidmaps: Iterable[Tuple[int, int, int]] = (),
userns: Optional[str] = None,
uts: Optional[str] = None,
volumes: List[VolumeDefinition] = [],
volumes_from: List[
volumes: Iterable[VolumeDefinition] = (),
volumes_from: Iterable[
python_on_whales.components.container.cli_wrapper.ValidContainer
] = [],
] = (),
) -> Pod:
"""Creates a pod, but does not start it.

Expand Down Expand Up @@ -401,19 +399,19 @@ def create(
full_cmd = self.docker_cmd + ["pod", "create"]
full_cmd.add_simple_arg("--name", name)

full_cmd.add_args_iterable_or_single(
full_cmd.add_args_iterable(
"--add-host", [f"{host}:{ip}" for host, ip in add_hosts]
)
full_cmd.add_simple_arg("--cgroup-parent", cgroup_parent)
full_cmd.add_simple_arg("--cpus", cpus)
full_cmd.add_simple_arg("--cpuset-cpus", join_if_not_none(cpuset_cpus))
full_cmd.add_args_iterable_or_single("--device", devices)
full_cmd.add_args_iterable_or_single("--device-read-bps", device_read_bps)
full_cmd.add_args_iterable_or_single("--dns", dns)
full_cmd.add_args_iterable_or_single("--dns-option", dns_options)
full_cmd.add_args_iterable_or_single("--dns-search", dns_search)
full_cmd.add_args_iterable("--device", devices)
full_cmd.add_args_iterable("--device-read-bps", device_read_bps)
full_cmd.add_args_iterable("--dns", dns)
full_cmd.add_args_iterable("--dns-option", dns_options)
full_cmd.add_args_iterable("--dns-search", dns_search)
full_cmd.add_simple_arg("--exit-policy", exit_policy)
full_cmd.add_args_iterable_or_single("--gidmap", [":".join(x) for x in gidmaps])
full_cmd.add_args_iterable("--gidmap", [":".join(x) for x in gidmaps])
full_cmd.add_simple_arg("--hostname", hostname)

if infra is not None:
Expand All @@ -426,27 +424,25 @@ def create(

full_cmd.add_simple_arg("--ip", ip)
full_cmd.add_simple_arg("--ip6", ip6)
full_cmd.add_args_iterable_or_single("--label", format_mapping_for_cli(labels))
full_cmd.add_args_mapping("--label", labels)
full_cmd.add_args_iterable_or_single("--label-file", label_files)
full_cmd.add_simple_arg("--mac-address", mac_address)
full_cmd.add_simple_arg("--memory", memory)
full_cmd.add_args_iterable_or_single("--network", networks)
full_cmd.add_args_iterable_or_single("--network-alias", network_aliases)
full_cmd.add_args_iterable("--network-alias", network_aliases)
full_cmd.add_flag("--no-hosts", no_hosts)
full_cmd.add_simple_arg("--pid", pid)
full_cmd.add_simple_arg("--pod-id-file", pod_id_file)
full_cmd.add_args_iterable_or_single(
"-p", [format_port_arg(p) for p in publish]
)
full_cmd.add_args_iterable("-p", [format_port_arg(p) for p in publish])
full_cmd.add_flag("--replace", replace)
full_cmd.add_simple_arg("--restart", restart)
full_cmd.add_args_iterable_or_single("--security-opt", security_options)
full_cmd.add_args_iterable("--security-opt", security_options)
if share is not None:
full_cmd.add_simple_arg("--share", ",".join(share))
full_cmd.add_simple_arg("--shm-size", shm_size)
full_cmd.add_simple_arg("--subgidname", subgidname)
full_cmd.add_simple_arg("--subuidname", subuidname)
full_cmd.add_args_iterable_or_single("--sysctl", format_mapping_for_cli(sysctl))
full_cmd.add_args_mapping("--sysctl", sysctl)
full_cmd.add_args_iterable_or_single("--uidmap", [":".join(x) for x in uidmaps])
full_cmd.add_simple_arg("--userns", userns)
full_cmd.add_simple_arg("--uts", uts)
Expand Down Expand Up @@ -476,10 +472,10 @@ def exists(self, pod: ValidPod) -> bool:
def inspect(self, x: ValidPod, /) -> Pod: ...

@overload
def inspect(self, x: Sequence[ValidPod], /) -> List[Pod]: ...
def inspect(self, x: Iterable[ValidPod], /) -> List[Pod]: ...

def inspect(
self, x: Union[ValidPod, Sequence[ValidPod]], /
self, x: Union[ValidPod, Iterable[ValidPod]], /
) -> Union[Pod, List[Pod]]:
"""Creates a `python_on_whales.Pod` object.

Expand All @@ -498,7 +494,7 @@ def inspect(

def kill(
self,
x: Union[ValidPod, Sequence[ValidPod]],
x: Union[ValidPod, Iterable[ValidPod]],
/,
*,
signal: Optional[Union[int, str]] = None,
Expand Down Expand Up @@ -618,7 +614,7 @@ def logs(
else:
return "".join(x[1].decode() for x in iterator)

def pause(self, x: Union[ValidPod, Sequence[ValidPod]], /) -> None:
def pause(self, x: Union[ValidPod, Iterable[ValidPod]], /) -> None:
"""Pauses one or more pods

Parameters:
Expand All @@ -641,7 +637,7 @@ def prune(self) -> None:

def remove(
self,
x: Union[ValidPod, Sequence[ValidPod]],
x: Union[ValidPod, Iterable[ValidPod]],
/,
*,
force: bool = False,
Expand Down Expand Up @@ -671,7 +667,7 @@ def remove(
full_cmd.extend([str(p) for p in pods])
run(full_cmd)

def restart(self, x: Union[ValidPod, Sequence[ValidPod]], /) -> None:
def restart(self, x: Union[ValidPod, Iterable[ValidPod]], /) -> None:
"""Restarts one or more pods

Parameters:
Expand All @@ -687,7 +683,7 @@ def restart(self, x: Union[ValidPod, Sequence[ValidPod]], /) -> None:
full_cmd.extend([str(p) for p in pods])
run(full_cmd)

def start(self, x: Union[ValidPod, Sequence[ValidPod]], /) -> None:
def start(self, x: Union[ValidPod, Iterable[ValidPod]], /) -> None:
"""Starts one or more pods

Parameters:
Expand All @@ -703,7 +699,7 @@ def start(self, x: Union[ValidPod, Sequence[ValidPod]], /) -> None:
full_cmd.extend([str(p) for p in pods])
run(full_cmd)

def stats(self, x: Union[ValidPod, Sequence[ValidPod]], /) -> List[PodStats]:
def stats(self, x: Union[ValidPod, Iterable[ValidPod]], /) -> List[PodStats]:
"""Get pods resource usage statistics

The data unit is the byte.
Expand Down Expand Up @@ -731,7 +727,7 @@ def stats(self, x: Union[ValidPod, Sequence[ValidPod]], /) -> List[PodStats]:

def stop(
self,
x: Union[ValidPod, Sequence[ValidPod]],
x: Union[ValidPod, Iterable[ValidPod]],
/,
*,
time: Optional[int] = None,
Expand Down Expand Up @@ -759,7 +755,7 @@ def top(self, pod: ValidPod):
Not yet implemented"""
raise NotImplementedError

def unpause(self, x: Union[ValidPod, Sequence[ValidPod]], /) -> None:
def unpause(self, x: Union[ValidPod, Iterable[ValidPod]], /) -> None:
"""Unpauses one or more pods

Parameters:
Expand Down
6 changes: 3 additions & 3 deletions tests/python_on_whales/components/test_pod.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_create_devices_arg(podman_client: DockerClient):

def test_create_dns_arg(podman_client: DockerClient):
pod_name = random_name()
with podman_client.pod.create(pod_name, dns="1.2.3.4") as pod:
with podman_client.pod.create(pod_name, dns=["1.2.3.4"]) as pod:
output = podman_client.container.run(
"ubuntu", ["cat", "/etc/resolv.conf"], pod=pod
)
Expand Down Expand Up @@ -192,9 +192,9 @@ def test_create_share_arg(podman_client: DockerClient):
"ubuntu", ["readlink", "/proc/self"], pod=pod
)
assert output == "1"
with podman_client.pod.create(pod_name, share=["pid"]) as pod:
with podman_client.pod.create(pod_name, share=["pid", "ipc"]) as pod:
pod.start() # start the infra container (PID 1)
assert pod.shared_namespaces == ["pid"]
assert pod.shared_namespaces == ["pid", "ipc"]
output = podman_client.container.run(
"ubuntu", ["readlink", "/proc/self"], pod=pod
)
Expand Down
Loading