Skip to content

Commit 21d1c9d

Browse files
committed
refactor: polish code
Signed-off-by: thxCode <[email protected]>
1 parent 86d8625 commit 21d1c9d

File tree

3 files changed

+49
-68
lines changed

3 files changed

+49
-68
lines changed

gpustack_runtime/deployer/__init__.py

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@
6060
Mapping from deployer name to deployer.
6161
"""
6262

63+
_NO_AVAILABLE_DEPLOYER_MSG = (
64+
"No available deployer. "
65+
"Please provide a container runtime, e.g. "
66+
"bind mount the host `/var/run/docker.sock` on Docker, "
67+
"or allow (in-)cluster access on Kubernetes"
68+
)
69+
6370

6471
def supported_list() -> list[Deployer]:
6572
"""
@@ -98,13 +105,7 @@ def create_workload(workload: WorkloadPlan):
98105
dep.create(workload=workload)
99106
return
100107

101-
msg = (
102-
"No available deployer. "
103-
"Please provide a container runtime, e.g. "
104-
"bind mount the host `/var/run/docker.sock` on Docker, "
105-
"or allow (in-)cluster access on Kubernetes"
106-
)
107-
raise UnsupportedError(msg)
108+
raise UnsupportedError(_NO_AVAILABLE_DEPLOYER_MSG)
108109

109110

110111
def get_workload(
@@ -136,13 +137,7 @@ def get_workload(
136137

137138
return dep.get(name=name, namespace=namespace)
138139

139-
msg = (
140-
"No available deployer. "
141-
"Please provide a container runtime, e.g. "
142-
"bind mount the host `/var/run/docker.sock` on Docker, "
143-
"or allow (in-)cluster access on Kubernetes"
144-
)
145-
raise UnsupportedError(msg)
140+
raise UnsupportedError(_NO_AVAILABLE_DEPLOYER_MSG)
146141

147142

148143
def delete_workload(
@@ -174,13 +169,7 @@ def delete_workload(
174169

175170
return dep.delete(name=name, namespace=namespace)
176171

177-
msg = (
178-
"No available deployer. "
179-
"Please provide a container runtime, e.g. "
180-
"bind mount the host `/var/run/docker.sock` on Docker, "
181-
"or allow (in-)cluster access on Kubernetes"
182-
)
183-
raise UnsupportedError(msg)
172+
raise UnsupportedError(_NO_AVAILABLE_DEPLOYER_MSG)
184173

185174

186175
def list_workloads(
@@ -212,13 +201,7 @@ def list_workloads(
212201

213202
return dep.list(namespace=namespace, labels=labels)
214203

215-
msg = (
216-
"No available deployer. "
217-
"Please provide a container runtime, e.g. "
218-
"bind mount the host `/var/run/docker.sock` on Docker, "
219-
"or allow (in-)cluster access on Kubernetes"
220-
)
221-
raise UnsupportedError(msg)
204+
raise UnsupportedError(_NO_AVAILABLE_DEPLOYER_MSG)
222205

223206

224207
def logs_workload(
@@ -273,13 +256,7 @@ def logs_workload(
273256
follow=follow,
274257
)
275258

276-
msg = (
277-
"No available deployer. "
278-
"Please provide a container runtime, e.g. "
279-
"bind mount the host `/var/run/docker.sock` on Docker, "
280-
"or allow (in-)cluster access on Kubernetes"
281-
)
282-
raise UnsupportedError(msg)
259+
raise UnsupportedError(_NO_AVAILABLE_DEPLOYER_MSG)
283260

284261

285262
async def async_logs_workload(
@@ -334,13 +311,7 @@ async def async_logs_workload(
334311
follow=follow,
335312
)
336313

337-
msg = (
338-
"No available deployer. "
339-
"Please provide a container runtime, e.g. "
340-
"bind mount the host `/var/run/docker.sock` on Docker, "
341-
"or allow (in-)cluster access on Kubernetes"
342-
)
343-
raise UnsupportedError(msg)
314+
raise UnsupportedError(_NO_AVAILABLE_DEPLOYER_MSG)
344315

345316

346317
def exec_workload(
@@ -392,13 +363,7 @@ def exec_workload(
392363
args=args,
393364
)
394365

395-
msg = (
396-
"No available deployer. "
397-
"Please provide a container runtime, e.g. "
398-
"bind mount the host `/var/run/docker.sock` on Docker, "
399-
"or allow (in-)cluster access on Kubernetes"
400-
)
401-
raise UnsupportedError(msg)
366+
raise UnsupportedError(_NO_AVAILABLE_DEPLOYER_MSG)
402367

403368

404369
__all__ = [

gpustack_runtime/deployer/docker.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class DockerWorkloadStatus(WorkloadStatus):
175175

176176
@staticmethod
177177
def parse_state(
178-
d_containers: list[docker.models.containers],
178+
d_containers: list[docker.models.containers.Container],
179179
) -> WorkloadStatusStateEnum:
180180
"""
181181
Parse the state of the workload based on the status of its containers.
@@ -221,7 +221,7 @@ def parse_state(
221221
d_run_state = WorkloadStatusStateEnum.PENDING
222222
else:
223223
health = cr.attrs["State"].get("Health", {})
224-
if health and health.get("Status", "healthy") != "healthy":
224+
if health and health.get("Status", "healthy") not in ["healthy", ""]:
225225
return WorkloadStatusStateEnum.UNHEALTHY
226226

227227
d_init_state = None
@@ -252,7 +252,7 @@ def parse_state(
252252
def __init__(
253253
self,
254254
name: WorkloadName,
255-
d_containers: list[docker.models.containers],
255+
d_containers: list[docker.models.containers.Container],
256256
**kwargs,
257257
):
258258
created_at = d_containers[0].attrs["Created"]
@@ -352,10 +352,10 @@ def _get_client() -> docker.DockerClient | None:
352352
contextlib.redirect_stdout(dev_null),
353353
contextlib.redirect_stderr(dev_null),
354354
):
355-
if Path("/var/run/docker.sock").exists():
356-
client = docker.DockerClient(base_url="unix://var/run/docker.sock")
357-
else:
358-
client = docker.from_env()
355+
os_env = os.environ.copy()
356+
if envs.GPUSTACK_RUNTIME_DOCKER_HOST:
357+
os_env["DOCKER_HOST"] = envs.GPUSTACK_RUNTIME_DOCKER_HOST
358+
client = docker.from_env(environment=os_env)
359359
except docker.errors.DockerException:
360360
debug_log_exception(logger, "Failed to get Docker client")
361361

@@ -760,7 +760,7 @@ def _append_container_mounts(
760760
else:
761761
continue
762762

763-
if m.mode == ContainerMountModeEnum.ROX:
763+
if m.mode != ContainerMountModeEnum.RWX:
764764
binding["ReadOnly"] = True
765765

766766
mount_binding.append(binding)
@@ -1204,7 +1204,7 @@ def _prepare_create(self):
12041204
# Always filter out Docker Socket mount.
12051205
m
12061206
for m in (self_container.attrs["Mounts"] or [])
1207-
if m.get("Destination") != "/var/run/docker.sock"
1207+
if not m.get("Destination").endswith("/docker.sock")
12081208
]
12091209
if igs := envs.GPUSTACK_RUNTIME_DEPLOY_MIRRORED_DEPLOYMENT_IGNORE_VOLUMES:
12101210
mirrored_mounts = [
@@ -1593,7 +1593,7 @@ def _delete(
15931593
# Remove all containers with the workload label.
15941594
try:
15951595
d_containers = getattr(workload, "_d_containers", [])
1596-
for c in d_containers:
1596+
for c in reversed(d_containers):
15971597
c.remove(
15981598
force=True,
15991599
)
@@ -1860,7 +1860,7 @@ def _exec(
18601860
}
18611861

18621862
try:
1863-
result = container.exec_run(
1863+
_, output = container.exec_run(
18641864
detach=False,
18651865
**exec_options,
18661866
)
@@ -1869,8 +1869,8 @@ def _exec(
18691869
raise OperationError(msg) from e
18701870
else:
18711871
if not attach:
1872-
return result.output
1873-
return DockerWorkloadExecStream(result.output)
1872+
return output
1873+
return DockerWorkloadExecStream(output)
18741874

18751875

18761876
def _has_restart_policy(

gpustack_runtime/envs.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@
195195
# Deployer
196196

197197
## Docker
198+
GPUSTACK_RUNTIME_DOCKER_HOST: str | None = None
199+
"""
200+
Host for Docker connection.
201+
Used to override the default Docker host.
202+
Default is `http+unix:///var/run/docker.sock`.
203+
"""
198204
GPUSTACK_RUNTIME_DOCKER_MIRRORED_NAME_FILTER_LABELS: dict[str, str] | None = None
199205
"""
200206
Filter labels for selecting the mirrored deployer container in Docker.
@@ -207,19 +213,19 @@
207213
"""
208214
GPUSTACK_RUNTIME_DOCKER_PAUSE_IMAGE: str | None = None
209215
"""
210-
Docker image used for the pause container.
216+
Container image used for the pause container in Docker.
211217
"""
212218
GPUSTACK_RUNTIME_DOCKER_UNHEALTHY_RESTART_IMAGE: str | None = None
213219
"""
214-
Docker image used for unhealthy restart container.
220+
Container image used for unhealthy restart container in Docker.
215221
"""
216222
GPUSTACK_RUNTIME_DOCKER_EPHEMERAL_FILES_DIR: Path | None = None
217223
"""
218224
Directory for storing ephemeral files for Docker.
219225
"""
220226
GPUSTACK_RUNTIME_DOCKER_MUTE_ORIGINAL_HEALTHCHECK: bool = True
221227
"""
222-
Mute the original healthcheck of the container.
228+
Mute the original healthcheck of the container in Docker.
223229
"""
224230
## Kubernetes
225231
GPUSTACK_RUNTIME_KUBERNETES_NODE_NAME: str | None = None
@@ -432,12 +438,25 @@
432438
),
433439
# Deployer
434440
## Docker
441+
"GPUSTACK_RUNTIME_DOCKER_HOST": lambda: trim_str(
442+
getenvs(
443+
keys=[
444+
"GPUSTACK_RUNTIME_DOCKER_HOST",
445+
# Fallback to standard Docker environment variable.
446+
"DOCKER_HOST",
447+
],
448+
default="http+unix:///var/run/docker.sock",
449+
),
450+
),
435451
"GPUSTACK_RUNTIME_DOCKER_MIRRORED_NAME_FILTER_LABELS": lambda: to_dict(
436452
getenv(
437453
"GPUSTACK_RUNTIME_DOCKER_MIRRORED_NAME_FILTER_LABELS",
438454
),
439455
sep=";",
440456
),
457+
"GPUSTACK_RUNTIME_DOCKER_IMAGE_NO_PULL_VISUALIZATION": lambda: to_bool(
458+
getenv("GPUSTACK_RUNTIME_DOCKER_IMAGE_NO_PULL_VISUALIZATION", "0"),
459+
),
441460
"GPUSTACK_RUNTIME_DOCKER_PAUSE_IMAGE": lambda: getenv(
442461
"GPUSTACK_RUNTIME_DOCKER_PAUSE_IMAGE",
443462
"gpustack/runtime:pause",
@@ -455,9 +474,6 @@
455474
"GPUSTACK_RUNTIME_DOCKER_MUTE_ORIGINAL_HEALTHCHECK": lambda: to_bool(
456475
getenv("GPUSTACK_RUNTIME_DOCKER_MUTE_ORIGINAL_HEALTHCHECK", "1"),
457476
),
458-
"GPUSTACK_RUNTIME_DOCKER_IMAGE_NO_PULL_VISUALIZATION": lambda: to_bool(
459-
getenv("GPUSTACK_RUNTIME_DOCKER_IMAGE_NO_PULL_VISUALIZATION", "0"),
460-
),
461477
## Kubernetes
462478
"GPUSTACK_RUNTIME_KUBERNETES_NODE_NAME": lambda: getenv(
463479
"GPUSTACK_RUNTIME_KUBERNETES_NODE_NAME",

0 commit comments

Comments
 (0)