Skip to content

Commit ea6b489

Browse files
committed
refactor: deployer and detector
- tidy up docker deployer - support kubernetes deployer - bump runner version Signed-off-by: thxCode <[email protected]>
1 parent ac9b6f1 commit ea6b489

File tree

15 files changed

+3157
-572
lines changed

15 files changed

+3157
-572
lines changed

gpustack_runtime/cmds/deployer.py

Lines changed: 328 additions & 139 deletions
Large diffs are not rendered by default.

gpustack_runtime/cmds/detector.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import time
66
from typing import TYPE_CHECKING
77

8-
from ..detector import Devices, detect_devices # noqa: TID252
8+
from ..detector import Devices, detect_devices
99
from .__types__ import SubCommand
1010

1111
if TYPE_CHECKING:
@@ -66,7 +66,7 @@ def run(self):
6666

6767

6868
def format_devices_json(devs: Devices) -> str:
69-
return json.dumps([dev.__dict__ for dev in devs], indent=2)
69+
return json.dumps([dev.to_dict() for dev in devs], indent=2)
7070

7171

7272
def format_devices_table(devs: Devices) -> str:

gpustack_runtime/deployer/__init__.py

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,25 @@
2222
ContainerSecurity,
2323
OperationError,
2424
UnsupportedError,
25-
WorkloadExecResult,
25+
WorkloadExecStream,
26+
WorkloadNamespace,
2627
WorkloadOperationToken,
2728
WorkloadPlan,
2829
WorkloadSecurity,
2930
WorkloadSecuritySysctl,
3031
WorkloadStatus,
3132
WorkloadStatusStateEnum,
3233
)
33-
from .docker import DockerDeployer, DockerWorkloadPlan, DockerWorkloadStatus
34+
from .docker import (
35+
DockerDeployer,
36+
DockerWorkloadPlan,
37+
DockerWorkloadStatus,
38+
)
39+
from .kuberentes import (
40+
KubernetesDeployer,
41+
KubernetesWorkloadPlan,
42+
KubernetesWorkloadStatus,
43+
)
3444

3545
if TYPE_CHECKING:
3646
from collections.abc import Generator
@@ -39,6 +49,7 @@
3949

4050
deployers: list[Deployer] = [
4151
DockerDeployer(),
52+
KubernetesDeployer(),
4253
]
4354

4455

@@ -51,6 +62,10 @@ def create_workload(workload: WorkloadPlan):
5162
The workload to deploy.
5263
5364
Raises:
65+
TypeError:
66+
If the workload type is invalid.
67+
ValueError:
68+
If the workload fails to validate.
5469
UnsupportedError:
5570
If no deployer supports the given workload.
5671
OperationError:
@@ -61,20 +76,25 @@ def create_workload(workload: WorkloadPlan):
6176
if not dep.is_supported():
6277
continue
6378

64-
dep.create(workload)
79+
dep.create(workload=workload)
6580
return
6681

6782
msg = "No deployer supports"
6883
raise UnsupportedError(msg)
6984

7085

71-
def get_workload(name: WorkloadName) -> WorkloadStatus | None:
86+
def get_workload(
87+
name: WorkloadName,
88+
namespace: WorkloadNamespace | None = None,
89+
) -> WorkloadStatus | None:
7290
"""
7391
Get the status of a workload.
7492
7593
Args:
7694
name:
7795
The name of the workload.
96+
namespace:
97+
The namespace of the workload.
7898
7999
Returns:
80100
The status of the workload, or None if not found.
@@ -90,19 +110,24 @@ def get_workload(name: WorkloadName) -> WorkloadStatus | None:
90110
if not dep.is_supported():
91111
continue
92112

93-
return dep.get(name)
113+
return dep.get(name=name, namespace=namespace)
94114

95115
msg = "No deployer supports"
96116
raise UnsupportedError(msg)
97117

98118

99-
def delete_workload(name: WorkloadName) -> WorkloadStatus | None:
119+
def delete_workload(
120+
name: WorkloadName,
121+
namespace: WorkloadNamespace | None = None,
122+
) -> WorkloadStatus | None:
100123
"""
101124
Delete the given workload.
102125
103126
Args:
104127
name:
105128
The name of the workload to delete.
129+
namespace:
130+
The namespace of the workload.
106131
107132
Return:
108133
The status if found, None otherwise.
@@ -118,17 +143,22 @@ def delete_workload(name: WorkloadName) -> WorkloadStatus | None:
118143
if not dep.is_supported():
119144
continue
120145

121-
return dep.delete(name)
146+
return dep.delete(name=name, namespace=namespace)
122147

123148
msg = "No deployer supports"
124149
raise UnsupportedError(msg)
125150

126151

127-
def list_workloads(labels: dict[str, str] | None = None) -> list[WorkloadStatus]:
152+
def list_workloads(
153+
namespace: WorkloadNamespace | None = None,
154+
labels: dict[str, str] | None = None,
155+
) -> list[WorkloadStatus]:
128156
"""
129157
List all workloads.
130158
131159
Args:
160+
namespace:
161+
The namespace to filter workloads.
132162
labels:
133163
Labels to filter workloads.
134164
@@ -146,26 +176,29 @@ def list_workloads(labels: dict[str, str] | None = None) -> list[WorkloadStatus]
146176
if not dep.is_supported():
147177
continue
148178

149-
return dep.list(labels)
179+
return dep.list(namespace=namespace, labels=labels)
150180

151181
msg = "No deployer supports"
152182
raise UnsupportedError(msg)
153183

154184

155185
def logs_workload(
156186
name: WorkloadName,
187+
namespace: WorkloadNamespace | None = None,
157188
token: WorkloadOperationToken | None = None,
158189
timestamps: bool = False,
159190
tail: int | None = None,
160191
since: int | None = None,
161192
follow: bool = False,
162-
) -> Generator[bytes, None, None] | bytes:
193+
) -> Generator[bytes | str, None, None] | bytes | str:
163194
"""
164195
Get the logs of a workload.
165196
166197
Args:
167198
name:
168199
The name of the workload to get logs.
200+
namespace:
201+
The namespace of the workload.
169202
token:
170203
The token for operation.
171204
timestamps:
@@ -178,7 +211,7 @@ def logs_workload(
178211
Whether to follow the logs.
179212
180213
Returns:
181-
The logs as a byte string or a generator yielding byte strings if follow is True.
214+
The logs as a byte string, a string or a generator yielding byte strings or strings if follow is True.
182215
183216
Raises:
184217
UnsupportedError:
@@ -191,25 +224,36 @@ def logs_workload(
191224
if not dep.is_supported():
192225
continue
193226

194-
return dep.logs(name, token, timestamps, tail, since, follow)
227+
return dep.logs(
228+
name=name,
229+
namespace=namespace,
230+
token=token,
231+
timestamps=timestamps,
232+
tail=tail,
233+
since=since,
234+
follow=follow,
235+
)
195236

196237
msg = "No deployer supports"
197238
raise UnsupportedError(msg)
198239

199240

200241
def exec_workload(
201242
name: WorkloadName,
243+
namespace: WorkloadNamespace | None = None,
202244
token: WorkloadOperationToken | None = None,
203245
detach: bool = True,
204246
command: list[str] | None = None,
205247
args: list[str] | None = None,
206-
) -> WorkloadExecResult:
248+
) -> WorkloadExecStream | bytes | str:
207249
"""
208250
Execute a command in a running workload.
209251
210252
Args:
211253
name:
212254
The name of the workload to execute the command in.
255+
namespace:
256+
The namespace of the workload.
213257
token:
214258
The token for operation.
215259
detach:
@@ -220,8 +264,8 @@ def exec_workload(
220264
The arguments to pass to the command.
221265
222266
Returns:
223-
If detach is False, return a socket object in the output of WorkloadExecResult.
224-
otherwise, return the exit code and output of the command in WorkloadExecResult.
267+
If detach is False, return a WorkloadExecStream.
268+
otherwise, return the output of the command as a byte string or string.
225269
226270
Raises:
227271
UnsupportedError:
@@ -234,7 +278,14 @@ def exec_workload(
234278
if not dep.is_supported():
235279
continue
236280

237-
return dep.exec(name, token, detach, command, args)
281+
return dep.exec(
282+
name=name,
283+
namespace=namespace,
284+
token=token,
285+
detach=detach,
286+
command=command,
287+
args=args,
288+
)
238289

239290
msg = "No deployer supports"
240291
raise UnsupportedError(msg)
@@ -260,9 +311,11 @@ def exec_workload(
260311
"ContainerSecurity",
261312
"DockerWorkloadPlan",
262313
"DockerWorkloadStatus",
314+
"KubernetesWorkloadPlan",
315+
"KubernetesWorkloadStatus",
263316
"OperationError",
264317
"UnsupportedError",
265-
"WorkloadExecResult",
318+
"WorkloadExecStream",
266319
"WorkloadOperationToken",
267320
"WorkloadPlan",
268321
"WorkloadPlan",

0 commit comments

Comments
 (0)