Skip to content

Commit e2bc79f

Browse files
committed
Refactor volume mount configuration and add shared mount
Use separate classes for each volume mount type. Added support for shared filesystem volume mounts. The base VolumeMount class now serves as an abstract base, and specific volume mount types like ScratchMount, SecretMount, MemoryMount, and SharedFilesystemMount inherit from it.
1 parent 5c1338a commit e2bc79f

File tree

1 file changed

+68
-7
lines changed

1 file changed

+68
-7
lines changed

datacrunch/containers/containers.py

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class VolumeMountType(str, Enum):
4343
SCRATCH = "scratch"
4444
SECRET = "secret"
4545
MEMORY = "memory"
46+
SHARED = "shared"
4647

4748

4849
class ContainerRegistryType(str, Enum):
@@ -119,25 +120,85 @@ class EnvVar:
119120
@dataclass_json(undefined=Undefined.EXCLUDE)
120121
@dataclass
121122
class VolumeMount:
122-
"""Volume mount configuration for containers.
123+
"""Base class for volume mount configurations.
123124
124125
Attributes:
125126
type: Type of volume mount.
126127
mount_path: Path where the volume should be mounted in the container.
127-
size_in_mb: Size of the memory volume in megabytes, only used for memory volume mounts
128128
"""
129129

130130
type: VolumeMountType
131131
mount_path: str
132-
size_in_mb: Optional[int] = None
133132

134133

135-
@dataclass_json
134+
@dataclass_json(undefined=Undefined.EXCLUDE)
136135
@dataclass
137-
class SecretMount:
138-
mount_path: str
136+
class ScratchMount(VolumeMount):
137+
"""Scratch volume mount configuration.
138+
"""
139+
140+
def __init__(self, mount_path: str):
141+
"""Initialize a scratch volume mount.
142+
143+
Args:
144+
mount_path: Path where the volume should be mounted in the container.
145+
"""
146+
super().__init__(type=VolumeMountType.SCRATCH, mount_path=mount_path)
147+
148+
149+
@dataclass_json(undefined=Undefined.EXCLUDE)
150+
@dataclass
151+
class SecretMount(VolumeMount):
152+
"""Secret volume mount configuration.
153+
154+
A secret volume mount allows mounting secret files into the container.
155+
156+
Attributes:
157+
secret_name: Name of the fileset secret to mount
158+
file_names: List of file names that are part of the fileset secret
159+
"""
160+
139161
secret_name: str
140-
type: VolumeMountType = VolumeMountType.SECRET
162+
file_names: Optional[List[str]] = None
163+
164+
def __init__(self, mount_path: str, secret_name: str, file_names: Optional[List[str]] = None):
165+
super().__init__(type=VolumeMountType.SECRET, mount_path=mount_path)
166+
self.secret_name = secret_name
167+
self.file_names = file_names
168+
169+
170+
@dataclass_json(undefined=Undefined.EXCLUDE)
171+
@dataclass
172+
class MemoryMount(VolumeMount):
173+
"""Memory volume mount configuration.
174+
175+
A memory volume mount provides high-speed, ephemeral in-memory storage inside your container.
176+
The mount path is currently hardcoded to /dev/shm and cannot be changed.
177+
178+
Attributes:
179+
size_in_mb: Size of the memory volume in megabytes.
180+
"""
181+
182+
size_in_mb: int
183+
184+
def __init__(self, size_in_mb: int):
185+
super().__init__(type=VolumeMountType.MEMORY, mount_path='/dev/shm')
186+
self.size_in_mb = size_in_mb
187+
188+
189+
@dataclass_json(undefined=Undefined.EXCLUDE)
190+
@dataclass
191+
class SharedFilesystemMount(VolumeMount):
192+
"""Shared filesystem volume mount configuration.
193+
194+
A shared filesystem volume mount allows mounting a shared filesystem into the container.
195+
"""
196+
197+
volume_id: str # The ID of the shared filesystem volume to mount, needs to be created first
198+
199+
def __init__(self, mount_path: str, volume_id: str):
200+
super().__init__(type=VolumeMountType.SHARED, mount_path=mount_path)
201+
self.volume_id = volume_id
141202

142203

143204
@dataclass_json

0 commit comments

Comments
 (0)