Skip to content

Commit 9a9336f

Browse files
authored
Expand on the typing for volume types to support Kubernetes (#13467)
1 parent f7abe95 commit 9a9336f

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

localstack-core/localstack/utils/bootstrap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
NoSuchImage,
3535
NoSuchNetwork,
3636
PortMappings,
37-
VolumeDirMount,
3837
VolumeMappings,
38+
VolumeMappingSpecification,
3939
)
4040
from localstack.utils.container_utils.docker_cmd_client import CmdDockerClient
4141
from localstack.utils.docker_utils import DOCKER_CLIENT
@@ -666,7 +666,7 @@ def _cfg(cfg: ContainerConfiguration):
666666
return _cfg
667667

668668
@staticmethod
669-
def volume(volume: BindMount | VolumeDirMount):
669+
def volume(volume: VolumeMappingSpecification):
670670
def _cfg(cfg: ContainerConfiguration):
671671
cfg.volumes.add(volume)
672672

localstack-core/localstack/utils/container_utils/container_client.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
Literal,
1616
NamedTuple,
1717
Protocol,
18+
TypeAlias,
1819
TypedDict,
1920
)
2021

@@ -357,7 +358,13 @@ def __repr__(self):
357358

358359

359360
@dataclasses.dataclass
360-
class BindMount:
361+
class Mount:
362+
def to_str(self) -> str:
363+
return str(self)
364+
365+
366+
@dataclasses.dataclass
367+
class BindMount(Mount):
361368
"""Represents a --volume argument run/create command. When using VolumeBind to bind-mount a file or directory
362369
that does not yet exist on the Docker host, -v creates the endpoint for you. It is always created as a directory.
363370
"""
@@ -402,7 +409,7 @@ def parse(cls, param: str) -> "BindMount":
402409

403410

404411
@dataclasses.dataclass
405-
class VolumeDirMount:
412+
class VolumeDirMount(Mount):
406413
volume_path: str
407414
"""
408415
Absolute path inside /var/lib/localstack to mount into the container
@@ -439,24 +446,25 @@ def to_docker_sdk_parameters(self) -> tuple[str, dict[str, str]]:
439446
}
440447

441448

449+
VolumeMappingSpecification: TypeAlias = SimpleVolumeBind | Mount
450+
451+
442452
class VolumeMappings:
443-
mappings: list[SimpleVolumeBind | BindMount]
453+
mappings: list[VolumeMappingSpecification]
444454

445-
def __init__(self, mappings: list[SimpleVolumeBind | BindMount | VolumeDirMount] = None):
455+
def __init__(
456+
self,
457+
mappings: list[VolumeMappingSpecification] = None,
458+
):
446459
self.mappings = mappings if mappings is not None else []
447460

448-
def add(self, mapping: SimpleVolumeBind | BindMount | VolumeDirMount):
461+
def add(self, mapping: VolumeMappingSpecification):
449462
self.append(mapping)
450463

451-
def append(
452-
self,
453-
mapping: SimpleVolumeBind | BindMount | VolumeDirMount,
454-
):
464+
def append(self, mapping: VolumeMappingSpecification):
455465
self.mappings.append(mapping)
456466

457-
def find_target_mapping(
458-
self, container_dir: str
459-
) -> SimpleVolumeBind | BindMount | VolumeDirMount | None:
467+
def find_target_mapping(self, container_dir: str) -> VolumeMappingSpecification | None:
460468
"""
461469
Looks through the volumes and returns the one where the container dir matches ``container_dir``.
462470
Returns None if there is no volume mapping to the given container directory.
@@ -1510,7 +1518,8 @@ def convert_mount_list_to_dict(
15101518
) -> dict[str, dict[str, str]]:
15111519
"""Converts a List of (host_path, container_path) tuples to a Dict suitable as volume argument for docker sdk"""
15121520

1513-
def _map_to_dict(paths: SimpleVolumeBind | BindMount | VolumeDirMount):
1521+
def _map_to_dict(paths: VolumeMappingSpecification):
1522+
# TODO: move this logic to the `Mount` base class
15141523
if isinstance(paths, (BindMount, VolumeDirMount)):
15151524
return paths.to_docker_sdk_parameters()
15161525
else:

localstack-core/localstack/utils/container_utils/docker_cmd_client.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from localstack.utils.collections import ensure_list
1313
from localstack.utils.container_utils.container_client import (
1414
AccessDenied,
15-
BindMount,
1615
CancellableStream,
1716
ContainerClient,
1817
ContainerException,
@@ -21,16 +20,16 @@
2120
DockerNotAvailable,
2221
DockerPlatform,
2322
LogConfig,
23+
Mount,
2424
NoSuchContainer,
2525
NoSuchImage,
2626
NoSuchNetwork,
2727
NoSuchObject,
2828
PortMappings,
2929
RegistryConnectionError,
30-
SimpleVolumeBind,
3130
Ulimit,
3231
Util,
33-
VolumeDirMount,
32+
VolumeMappingSpecification,
3433
)
3534
from localstack.utils.run import run
3635
from localstack.utils.strings import first_char_to_upper, to_str
@@ -810,7 +809,7 @@ def _build_run_create_cmd(
810809
tty: bool = False,
811810
detach: bool = False,
812811
command: list[str] | str | None = None,
813-
volumes: list[SimpleVolumeBind] | None = None,
812+
volumes: list[VolumeMappingSpecification] | None = None,
814813
ports: PortMappings | None = None,
815814
exposed_ports: list[str] | None = None,
816815
env_vars: dict[str, str] | None = None,
@@ -900,7 +899,7 @@ def _build_run_create_cmd(
900899
return cmd, env_file
901900

902901
@staticmethod
903-
def _map_to_volume_param(volume: SimpleVolumeBind | BindMount | VolumeDirMount) -> str:
902+
def _map_to_volume_param(volume: VolumeMappingSpecification) -> str:
904903
"""
905904
Maps the mount volume, to a parameter for the -v docker cli argument.
906905
@@ -911,7 +910,8 @@ def _map_to_volume_param(volume: SimpleVolumeBind | BindMount | VolumeDirMount)
911910
:param volume: Either a SimpleVolumeBind, in essence a tuple (host_dir, container_dir), or a VolumeBind object
912911
:return: String which is passable as parameter to the docker cli -v option
913912
"""
914-
if isinstance(volume, (BindMount, VolumeDirMount)):
913+
# TODO: move this logic to the VolumeMappingSpecification type
914+
if isinstance(volume, Mount):
915915
return volume.to_str()
916916
else:
917917
return f"{volume[0]}:{volume[1]}"

0 commit comments

Comments
 (0)