|
9 | 9 | from flyteidl2.core.security_pb2 import SecurityContext |
10 | 10 | from flyteidl2.task import common_pb2, environment_pb2 |
11 | 11 | from kubernetes.client import ( |
| 12 | + CoreV1ResourceClaim, |
12 | 13 | V1Container, |
13 | 14 | V1EnvVar, |
14 | 15 | V1LocalObjectReference, |
| 16 | + V1PodResourceClaim, |
15 | 17 | V1PodSpec, |
| 18 | + V1ResourceRequirements, |
16 | 19 | ) |
17 | 20 |
|
18 | 21 | import flyte |
@@ -279,6 +282,59 @@ async def t2(a: int, b: str) -> str: |
279 | 282 | get_proto_task(t2, context) |
280 | 283 |
|
281 | 284 |
|
| 285 | +def test_get_k8s_pod_preserves_dra_claims_with_task_resources(): |
| 286 | + """A DRA resource claim on the pod template's primary container must survive |
| 287 | + even when the task also declares cpu/memory via Resources(...).""" |
| 288 | + pod_template = PodTemplate( |
| 289 | + pod_spec=V1PodSpec( |
| 290 | + resource_claims=[V1PodResourceClaim(name="gpu", resource_claim_template_name="flyte-task-gpu-x2")], |
| 291 | + containers=[ |
| 292 | + V1Container( |
| 293 | + name="primary", |
| 294 | + resources=V1ResourceRequirements(claims=[CoreV1ResourceClaim(name="gpu")]), |
| 295 | + ) |
| 296 | + ], |
| 297 | + ), |
| 298 | + ) |
| 299 | + |
| 300 | + env = flyte.TaskEnvironment( |
| 301 | + name="test_env", |
| 302 | + image="python:3.10", |
| 303 | + # Declaring cpu/memory is exactly what used to strip the .claims link. |
| 304 | + resources=flyte.Resources(cpu="24", memory="490Gi"), |
| 305 | + pod_template=pod_template, |
| 306 | + ) |
| 307 | + |
| 308 | + @env.task(short_name="dra_task") |
| 309 | + async def t1(a: int) -> int: |
| 310 | + return a |
| 311 | + |
| 312 | + context = SerializationContext( |
| 313 | + project="test-project", |
| 314 | + domain="test-domain", |
| 315 | + version="test-version", |
| 316 | + org="test-org", |
| 317 | + input_path="/tmp/inputs", |
| 318 | + output_path="/tmp/outputs", |
| 319 | + image_cache=None, |
| 320 | + code_bundle=None, |
| 321 | + root_dir=pathlib.Path.cwd(), |
| 322 | + ) |
| 323 | + |
| 324 | + from google.protobuf import json_format |
| 325 | + |
| 326 | + k8s_pod = _get_k8s_pod(_get_urun_container(context, t1), pod_template) |
| 327 | + pod_spec = json_format.MessageToDict(k8s_pod.pod_spec) |
| 328 | + primary = next(c for c in pod_spec["containers"] if c["name"] == "primary") |
| 329 | + resources = primary["resources"] |
| 330 | + |
| 331 | + # The DRA claim link is preserved... |
| 332 | + assert resources["claims"] == [{"name": "gpu"}] |
| 333 | + # ...alongside the task-declared cpu/memory. |
| 334 | + assert resources["requests"]["cpu"] == "24" |
| 335 | + assert resources["requests"]["memory"] == "490Gi" |
| 336 | + |
| 337 | + |
282 | 338 | @pytest.fixture(scope="module") |
283 | 339 | def env_task_ctx(): |
284 | 340 | # Create a real task environment |
|
0 commit comments