managed_deployment.py 55.3 KB
Newer Older
1
# SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
3
4
5
6
7
# SPDX-License-Identifier: Apache-2.0

import asyncio
import logging
import os
import re
8
import secrets
9
import time
10
from dataclasses import dataclass, field
11
from typing import Any, List, Literal, Optional
12
13
14
15

import kr8s
import requests
import yaml
16
from kr8s.objects import Pod, Service
17
from kubernetes_asyncio import client, config
18
from kubernetes_asyncio.client import exceptions
19

20
21
from tests.utils.test_output import resolve_test_output_path

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

def _get_workspace_dir() -> str:
    """Get workspace directory without depending on dynamo.common package.

    This allows tests to run without requiring dynamo package to be installed.
    """
    # Start from this file's location and walk up to find workspace root
    current = os.path.dirname(os.path.abspath(__file__))
    while current != os.path.dirname(current):  # Stop at filesystem root
        # Workspace root has pyproject.toml
        if os.path.exists(os.path.join(current, "pyproject.toml")):
            return current
        current = os.path.dirname(current)

    # Fallback: assume workspace is 3 levels up from tests/utils/
    return os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
38

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

class ServiceSpec:
    """Wrapper around a single service in the deployment spec."""

    def __init__(self, service_name: str, service_spec: dict):
        self._name = service_name
        self._spec = service_spec

    @property
    def name(self) -> str:
        """The service name (read-only)"""
        return self._name

    # ----- Image -----
    @property
    def image(self) -> Optional[str]:
        """Container image for the service"""
        try:
            return self._spec["extraPodSpec"]["mainContainer"]["image"]
        except KeyError:
            return None

    @image.setter
    def image(self, value: str):
        if "extraPodSpec" not in self._spec:
            self._spec["extraPodSpec"] = {"mainContainer": {}}
        if "mainContainer" not in self._spec["extraPodSpec"]:
            self._spec["extraPodSpec"]["mainContainer"] = {}
        self._spec["extraPodSpec"]["mainContainer"]["image"] = value

69
70
71
72
73
74
75
76
77
78
79
80
81
82
    @property
    def frontend_sidecar_image(self) -> Optional[str]:
        """Container image for the frontendSidecar (if present)."""
        try:
            return self._spec["frontendSidecar"]["image"]
        except KeyError:
            return None

    @frontend_sidecar_image.setter
    def frontend_sidecar_image(self, value: str):
        if "frontendSidecar" not in self._spec:
            self._spec["frontendSidecar"] = {}
        self._spec["frontendSidecar"]["image"] = value

83
84
85
86
87
88
89
90
91
    @property
    def envs(self) -> list[dict[str, str]]:
        """Environment variables for the service"""
        return self._spec.get("envs", [])

    @envs.setter
    def envs(self, value: list[dict[str, str]]):
        self._spec["envs"] = value

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    def _get_args(self) -> list[str]:
        """Return the container args list, normalising scalar strings to a list in-place.

        Always returns the same list object that is stored in the spec, so
        in-place mutations (append / index assignment) are reflected immediately
        without an explicit writeback.
        """
        try:
            container = self._spec["extraPodSpec"]["mainContainer"]
        except KeyError:
            return []
        if "args" not in container:
            container["args"] = []
        args = container["args"]
        if isinstance(args, str):
            args = args.split()
            container["args"] = args
        return args

111
112
113
114
115
116
117
118
119
120
121
    # ----- Replicas -----
    @property
    def replicas(self) -> int:
        return self._spec.get("replicas", 0)

    @replicas.setter
    def replicas(self, value: int):
        self._spec["replicas"] = value

    @property
    def model(self) -> Optional[str]:
122
        """Model being served by this service (checks both --model and --model-path)"""
123
124
125
126
127
        args = self._get_args()
        for i, arg in enumerate(args):
            if arg in ["--model", "--model-path"]:
                if i + 1 < len(args) and not args[i + 1].startswith("-"):
                    return args[i + 1]
128
129
130
131
        return None

    @model.setter
    def model(self, value: str):
132
133
134
135
136
        args = self._get_args()
        for i, arg in enumerate(args):
            if arg in ["--model", "--model-path"]:
                if i + 1 < len(args) and not args[i + 1].startswith("-"):
                    args[i + 1] = value
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
                return

    # ----- GPUs -----
    @property
    def gpus(self) -> int:
        try:
            return int(self._spec["resources"]["limits"]["gpu"])
        except KeyError:
            return 0

    @gpus.setter
    def gpus(self, value: int):
        if "resources" not in self._spec:
            self._spec["resources"] = {}
        if "limits" not in self._spec["resources"]:
            self._spec["resources"]["limits"] = {}
        self._spec["resources"]["limits"]["gpu"] = str(value)

    @property
    def tensor_parallel_size(self) -> int:
        """Get tensor parallel size from vLLM arguments"""
158
159
160
161
162
163
        args = self._get_args()
        for i, arg in enumerate(args):
            if arg == "--tensor-parallel-size":
                if i + 1 < len(args) and not args[i + 1].startswith("-"):
                    return int(args[i + 1])
                return 1
164
165
166
167
        return 1

    @tensor_parallel_size.setter
    def tensor_parallel_size(self, value: int):
168
169
170
171
172
173
174
175
176
177
        args = self._get_args()
        for i, arg in enumerate(args):
            if arg == "--tensor-parallel-size":
                if i + 1 < len(args) and not args[i + 1].startswith("-"):
                    args[i + 1] = str(value)
                else:
                    args.append(str(value))
                self.gpus = value
                return
        args.extend(["--tensor-parallel-size", str(value)])
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
        self.gpus = value


class DeploymentSpec:
    def __init__(
        self, base: str, endpoint="/v1/chat/completions", port=8000, system_port=9090
    ):
        """Load the deployment YAML file"""
        with open(base, "r") as f:
            self._deployment_spec = yaml.safe_load(f)
        self._endpoint = endpoint
        self._port = port
        self._system_port = system_port

    @property
    def name(self) -> str:
        """Deployment name"""
        return self._deployment_spec["metadata"]["name"]

    @name.setter
    def name(self, value: str):
        self._deployment_spec["metadata"]["name"] = value

    @property
    def port(self) -> int:
        """Deployment port"""
        return self._port

    @property
    def system_port(self) -> int:
        """Deployment port"""
        return self._system_port

    @property
    def endpoint(self) -> str:
        return self._endpoint

    @property
    def namespace(self) -> str:
        """Deployment namespace"""
        return self._deployment_spec["metadata"]["namespace"]

    @namespace.setter
    def namespace(self, value: str):
        self._deployment_spec["metadata"]["namespace"] = value

    def disable_grove(self):
        if "annotations" not in self._deployment_spec["metadata"]:
            self._deployment_spec["metadata"]["annotations"] = {}
        self._deployment_spec["metadata"]["annotations"][
            "nvidia.com/enable-grove"
        ] = "false"

    def set_model(self, model: str, service_name: Optional[str] = None):
        if service_name is None:
            services = self.services
        else:
            services = [self[service_name]]
        for service in services:
            service.model = model

    def set_image(self, image: str, service_name: Optional[str] = None):
        if service_name is None:
            services = self.services
        else:
            services = [self[service_name]]
        for service in services:
            service.image = image

247
248
249
250
251
252
253
254
255
256
    def set_frontend_sidecar_image(
        self, image: str, service_name: Optional[str] = None
    ):
        if service_name is None:
            services = self.services
        else:
            services = [self[service_name]]
        for service in services:
            service.frontend_sidecar_image = image

257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
    def set_tensor_parallel(self, tp_size: int, service_names: Optional[list] = None):
        """Scale deployment for different tensor parallel configurations

        Args:
            tp_size: Target tensor parallel size
            service_names: List of service names to update (defaults to worker services)
        """
        if service_names is None:
            # Auto-detect worker services (services with GPU requirements)
            service_names = [svc.name for svc in self.services if svc.gpus > 0]

        for service_name in service_names:
            service = self[service_name]
            service.tensor_parallel_size = tp_size
            service.gpus = tp_size

    def set_logging(self, enable_jsonl: bool = True, log_level: str = "debug"):
        """Configure logging for the deployment

        Args:
            enable_jsonl: Enable JSON line logging (sets DYN_LOGGING_JSONL=true)
            log_level: Set log level (sets DYN_LOG to specified level)
        """
        spec = self._deployment_spec
        if "envs" not in spec["spec"]:
            spec["spec"]["envs"] = []

        # Remove any existing logging env vars to avoid duplicates
        spec["spec"]["envs"] = [
            env
            for env in spec["spec"]["envs"]
            if env.get("name") not in ["DYN_LOGGING_JSONL", "DYN_LOG"]
        ]

        if enable_jsonl:
            spec["spec"]["envs"].append({"name": "DYN_LOGGING_JSONL", "value": "true"})

        if log_level:
            spec["spec"]["envs"].append({"name": "DYN_LOG", "value": log_level})

    def get_logging_config(self) -> dict:
        """Get current logging configuration

        Returns:
            dict with 'jsonl_enabled' and 'log_level' keys
        """
        envs = self._deployment_spec.get("spec", {}).get("envs", [])

        jsonl_enabled = False
        log_level = None

        for env in envs:
            if env.get("name") == "DYN_LOGGING_JSONL":
                jsonl_enabled = env.get("value") in ["true", "1"]
            elif env.get("name") == "DYN_LOG":
                log_level = env.get("value")

        return {"jsonl_enabled": jsonl_enabled, "log_level": log_level}

316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
    def set_service_env_var(self, service_name: str, name: str, value: str):
        """
        Set an environment variable for a specific service
        """
        service = self.get_service(service_name)
        envs = service.envs if service.envs is not None else []

        # if env var already exists, update it
        for env in envs:
            if env["name"] == name:
                env["value"] = value
                service.envs = envs  # Save back to trigger the setter
                return

        # if env var does not exist, add it
        envs.append({"name": name, "value": value})
        service.envs = envs  # Save back to trigger the setter

    def get_service_env_vars(self, service_name: str) -> list[dict]:
        """
        Get all environment variables for a specific service

        Returns:
            List of environment variable dicts (e.g., [{"name": "VAR", "value": "val"}])
        """
        service = self.get_service(service_name)
        return service.envs

344
    @property
345
    def services(self) -> list[ServiceSpec]:
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
        """List of ServiceSpec objects"""
        return [
            ServiceSpec(svc, spec)
            for svc, spec in self._deployment_spec["spec"]["services"].items()
        ]

    def __getitem__(self, service_name: str) -> ServiceSpec:
        """Allow dict-like access: d['Frontend']"""
        return ServiceSpec(
            service_name, self._deployment_spec["spec"]["services"][service_name]
        )

    def spec(self):
        return self._deployment_spec

361
362
363
364
365
366
367
368
369
    def add_arg_to_service(self, service_name: str, arg_name: str, arg_value: str):
        """
        Add or override a command-line argument for a specific service

        Args:
            service_name: Name of the service (e.g., "VllmDecodeWorker", "TRTLLMWorker")
            arg_name: Argument name (e.g., "--max-model-len", "--max-seq-len")
            arg_value: Argument value (e.g., "1024")
        """
370
371
        service = self.get_service(service_name)
        service_spec = service._spec
372
373

        # Ensure args list exists
374
375
376
377
378
379
        if "extraPodSpec" not in service_spec:
            service_spec["extraPodSpec"] = {"mainContainer": {}}
        if "mainContainer" not in service_spec["extraPodSpec"]:
            service_spec["extraPodSpec"]["mainContainer"] = {}
        if "args" not in service_spec["extraPodSpec"]["mainContainer"]:
            service_spec["extraPodSpec"]["mainContainer"]["args"] = []
380

381
        args_list = service_spec["extraPodSpec"]["mainContainer"]["args"]
382
383
384
385
386
387

        # Convert to list if needed (sometimes it's a single string)
        if isinstance(args_list, str):
            import shlex

            args_list = shlex.split(args_list)
388
            service_spec["extraPodSpec"]["mainContainer"]["args"] = args_list
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410

        # Find existing argument
        arg_index = None
        for i, arg in enumerate(args_list):
            if arg == arg_name:
                arg_index = i
                break

        if arg_index is not None:
            # Argument found, check if it has a value
            if arg_index + 1 < len(args_list) and not args_list[
                arg_index + 1
            ].startswith("-"):
                # Has a value, replace it
                args_list[arg_index + 1] = arg_value
            else:
                # No value after the argument, insert the value
                args_list.insert(arg_index + 1, arg_value)
        else:
            # Add new argument
            args_list.extend([arg_name, arg_value])

411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
    def get_service(self, service_name: str) -> ServiceSpec:
        """
        Get a specific service from the deployment spec
        """
        if service_name not in self._deployment_spec["spec"]["services"]:
            raise ValueError(f"Service '{service_name}' not found in deployment spec")

        return ServiceSpec(
            service_name, self._deployment_spec["spec"]["services"][service_name]
        )

    def set_service_replicas(self, service_name: str, replicas: int):
        """
        Set the number of replicas for a specific service
        """
        service = self.get_service(service_name)
        service.replicas = replicas

429
430
431
432
433
434
435
    def save(self, out_file: str):
        """Save updated deployment to file"""
        with open(out_file, "w") as f:
            yaml.safe_dump(self._deployment_spec, f, default_flow_style=False)


class PodProcess:
436
    def __init__(self, pod: Pod, line: str):
437
438
439
440
441
442
443
444
445
446
447
448
449
450
        self.pid = int(re.split(r"\s+", line)[1])
        self.command = " ".join(
            re.split(r"\s+", line)[10:]
        )  # Columns 10+ are the command
        self._pod = pod

    def kill(self, signal=None):
        """Kill this process in the given pod"""

        if not signal:
            if self.pid == 1:
                signal = "SIGINT"
            else:
                signal = "SIGKILL"
451
452
453
454
455
456
457
458
459
        # Python processes need signal handlers for graceful shutdown
        if self.pid == 1 and signal == "SIGKILL" and "python" in self.command.lower():
            logging.info(
                f"PID 1 is a Python process ({self.command[:50]}...), "
                "changing SIGKILL to SIGINT for graceful shutdown"
            )
            signal = "SIGINT"

        logging.info("Killing PID %s with %s", self.pid, signal)
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478

        return self._pod.exec(["kill", f"-{signal}", str(self.pid)])

    def wait(self, timeout: int = 60):
        """Wait for this process to exit in the given pod"""
        # Simple implementation; adjust as needed
        for _ in range(timeout):
            try:
                result = self._pod.exec(
                    ["kill", "-0", str(self.pid)]
                )  # Check if process exists
                if result.returncode != 0:
                    return True  # Process exited
                time.sleep(1)
            except Exception:
                return True
        return False  # Timed out


479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
@dataclass
class PodStatusDetail:
    """Container-level status snapshot for a single container in a pod."""

    pod_name: str
    container_name: str
    state: Literal["Waiting", "Terminated", "Running", "Unknown"]
    reason: str = ""
    message: str = ""
    exit_code: Optional[int] = None
    restart_count: int = 0

    def format(self) -> str:
        result = f"{self.pod_name}/{self.container_name}: {self.state}"
        if self.reason:
            result += f": {self.reason}"
        if self.message:
            result += f" ({self.message})"
        if self.exit_code is not None:
            result += f" (exit_code={self.exit_code})"
        if self.restart_count > 0:
            result += f" [restarts={self.restart_count}]"
        return result


504
505
506
507
508
@dataclass
class ManagedDeployment:
    log_dir: str
    deployment_spec: DeploymentSpec
    namespace: str
509
510
511
512
    # TODO: this should be determined by the deployment_spec
    # the service containing component_type: Frontend determines what is actually the frontend service
    frontend_service_name: str = "Frontend"
    skip_service_restart: bool = False
513

514
515
    _custom_api: Optional[client.CustomObjectsApi] = None
    _core_api: Optional[client.CoreV1Api] = None
516
517
518
    _in_cluster: bool = False
    _logger: logging.Logger = logging.getLogger()
    _port_forward: Optional[Any] = None
519
520
    # Initialized from deployment_spec.name in __post_init__; placeholder needed for dataclass ordering
    _deployment_name: str = field(default="")
521
    _apps_v1: Optional[Any] = None
522
    _active_port_forwards: List[Any] = field(default_factory=list)
523
524
525

    def __post_init__(self):
        self._deployment_name = self.deployment_spec.name
526
        self.log_dir = resolve_test_output_path(self.log_dir)
527
528

    async def _init_kubernetes(self):
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
        """Initialize kubernetes client.

        Priority order:
        1. KUBECONFIG environment variable (CI scenario with proper RBAC)
        2. In-cluster config (for pods without explicit kubeconfig)
        3. Default kubeconfig (~/.kube/config)
        """
        kubeconfig_path = os.environ.get("KUBECONFIG")

        if kubeconfig_path and os.path.exists(kubeconfig_path):
            # Explicit kubeconfig provided (CI scenario) - use it first
            self._logger.info(f"Loading kubeconfig from KUBECONFIG: {kubeconfig_path}")
            await config.load_kube_config(config_file=kubeconfig_path)
            self._in_cluster = False
            self._logger.info("Successfully loaded kubeconfig from KUBECONFIG")
        else:
            try:
                # Try in-cluster config (for pods without explicit kubeconfig)
                self._logger.info("Attempting in-cluster kubernetes config")
                config.load_incluster_config()
                self._in_cluster = True
                self._logger.info("Successfully loaded in-cluster kubernetes config")
            except Exception as e:
                # Fallback to default kube config file (for local development)
                self._logger.warning(
                    f"In-cluster config failed ({type(e).__name__}: {e}), "
                    f"falling back to default kubeconfig (~/.kube/config)"
                )
                await config.load_kube_config()
                self._in_cluster = False
                self._logger.info("Successfully loaded default kubeconfig")

561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
        k8s_client = client.ApiClient()
        self._custom_api = client.CustomObjectsApi(k8s_client)
        self._core_api = client.CoreV1Api(k8s_client)
        self._apps_v1 = client.AppsV1Api()

    async def _wait_for_pods(self, label, expected, timeout=300):
        for _ in range(timeout):
            assert self._core_api is not None, "Kubernetes API not initialized"
            pods = await self._core_api.list_namespaced_pod(
                self.namespace, label_selector=label
            )
            running = sum(
                1
                for pod in pods.items
                if any(
                    cond.type == "Ready" and cond.status == "True"
                    for cond in (pod.status.conditions or [])
                )
            )
            if running == expected:
                return True
            await asyncio.sleep(1)
        raise Exception(f"Didn't Reach Expected Pod Count {label}=={expected}")

    async def _scale_statfulset(self, name, label, replicas):
        body = {"spec": {"replicas": replicas}}
        assert self._apps_v1 is not None, "Kubernetes API not initialized"
        await self._apps_v1.patch_namespaced_stateful_set_scale(
            name, self.namespace, body
        )
        await self._wait_for_pods(label, replicas)

    async def _restart_stateful(self, name, label):
        self._logger.info(f"Restarting {name} {label}")

        await self._scale_statfulset(name, label, 0)
        assert self._core_api is not None, "Kubernetes API not initialized"
        nats_pvc = await self._core_api.list_namespaced_persistent_volume_claim(
            self.namespace, label_selector=label
        )
        for pvc in nats_pvc.items:
            await self._core_api.delete_namespaced_persistent_volume_claim(
                pvc.metadata.name, self.namespace
            )

        await self._scale_statfulset(name, label, 1)

        self._logger.info(f"Restarted {name} {label}")

610
611
612
613
614
615
616
617
618
619
620
    async def wait_for_unready(self, timeout: int = 1800, sleep=1, log_interval=60):
        """
        Wait for the custom resource to be unready.

        Args:
            timeout: Maximum time to wait in seconds, default to 30 mins (image pulling can take a while)
        """
        return await self._wait_for_condition(
            timeout, sleep, log_interval, False, "pending"
        )

621
622
623
624
625
626
627
    async def _wait_for_ready(self, timeout: int = 1800, sleep=1, log_interval=60):
        """
        Wait for the custom resource to be ready.

        Args:
            timeout: Maximum time to wait in seconds, default to 30 mins (image pulling can take a while)
        """
628
629
630
631
632
633
634
635
636
637
638
639
        return await self._wait_for_condition(
            timeout, sleep, log_interval, True, "successful"
        )

    async def _wait_for_condition(
        self,
        timeout: int = 1800,
        sleep=1,
        log_interval=60,
        desired_ready_condition_val: bool = True,
        desired_state_val: str = "successful",
    ):
640
641
        start_time = time.time()

642
643
644
        self._logger.info(
            f"Waiting for Deployment {self._deployment_name} to have Ready condition {desired_ready_condition_val} and state {desired_state_val}"
        )
645
646
647
648
649
650
651

        attempt = 0

        while (time.time() - start_time) < timeout:
            try:
                attempt += 1
                assert self._custom_api is not None, "Kubernetes API not initialized"
652
                status = await self._custom_api.get_namespaced_custom_object(  # type: ignore[awaitable-is-not-coroutine]
653
654
655
656
657
658
659
660
661
                    group="nvidia.com",
                    version="v1alpha1",
                    namespace=self.namespace,
                    plural="dynamographdeployments",
                    name=self._deployment_name,
                )
                # Check both conditions:
                # 1. Ready condition is True
                # 2. State is successful
662
663
664
                status_obj = status.get("status", {})  # type: ignore[attr-defined]
                conditions = status_obj.get("conditions", [])  # type: ignore[attr-defined]
                current_state = status_obj.get("state", "unknown")  # type: ignore[attr-defined]
665

666
                observed_ready_condition_val = ""
667
                for condition in conditions:
668
669
670
671
672
673
674
675
676
677
678
679
680
                    if condition.get("type") == "Ready":
                        observed_ready_condition_val = condition.get("status")
                        if observed_ready_condition_val == str(
                            desired_ready_condition_val
                        ):
                            break

                observed_state_val = status_obj.get("state")  # type: ignore[attr-defined]

                if (
                    observed_ready_condition_val == str(desired_ready_condition_val)
                    and observed_state_val == desired_state_val
                ):
681
682
683
684
685
686
                    self._logger.info(f"Current deployment state: {current_state}")
                    self._logger.info(f"Current conditions: {conditions}")
                    self._logger.info(
                        f"Elapsed time: {time.time() - start_time:.1f}s / {timeout}s"
                    )

687
688
689
                    self._logger.info(
                        f"Deployment {self._deployment_name} has Ready condition {desired_ready_condition_val} and state {desired_state_val}"
                    )
690
691
692
693
694
695
696
697
698
                    return True
                else:
                    if attempt % log_interval == 0:
                        self._logger.info(f"Current deployment state: {current_state}")
                        self._logger.info(f"Current conditions: {conditions}")
                        self._logger.info(
                            f"Elapsed time: {time.time() - start_time:.1f}s / {timeout}s"
                        )
                        self._logger.info(
699
                            f"Deployment has Ready condition {observed_ready_condition_val} and state {observed_state_val}, desired condition {desired_ready_condition_val} and state {desired_state_val}"
700
                        )
701
702
703
704
705
706
707
708
709
                        pod_details = await self._get_pod_status_details()
                        if pod_details:
                            for d in pod_details:
                                self._logger.info(f"  Pod status: {d.format()}")
                        pod_events = await self._get_pod_events()
                        if pod_events:
                            self._logger.info("  Pod warning events:")
                            for ev in pod_events:
                                self._logger.info(f"    {ev}")
710

711
            except exceptions.ApiException as e:
712
713
714
715
716
717
718
719
720
                self._logger.info(
                    f"API Exception while checking deployment status: {e}"
                )
                self._logger.info(f"Status code: {e.status}, Reason: {e.reason}")
            except Exception as e:
                self._logger.info(
                    f"Unexpected exception while checking deployment status: {e}"
                )
            await asyncio.sleep(sleep)
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803

        # Collect pod diagnostics before raising
        pod_details = await self._get_pod_status_details()
        elapsed = time.time() - start_time
        msg = (
            f"Deployment {self._deployment_name} failed to reach "
            f"Ready={desired_ready_condition_val}, state={desired_state_val} "
            f"within {elapsed:.0f}s (timeout={timeout}s)"
        )
        if pod_details:
            detail_lines = "\n".join(f"  {d.format()}" for d in pod_details)
            msg += f"\n\nPod status at timeout:\n{detail_lines}"
        raise TimeoutError(msg)

    async def _get_pod_status_details(self) -> List[PodStatusDetail]:
        """Collect container-level status for all pods owned by this deployment.

        Returns a list of PodStatusDetail objects. Returns empty list on any
        API failure so callers never need to guard against exceptions.
        """
        try:
            assert self._core_api is not None, "Kubernetes API not initialized"
            label = f"nvidia.com/dynamo-graph-deployment-name={self._deployment_name}"
            pods = await self._core_api.list_namespaced_pod(
                self.namespace, label_selector=label
            )

            details: List[PodStatusDetail] = []
            for pod in pods.items:
                pod_name = pod.metadata.name
                pod_status = pod.status
                phase = pod_status.phase if pod_status else "Unknown"

                container_statuses = (
                    pod_status.container_statuses if pod_status else None
                )
                if not container_statuses:
                    details.append(
                        PodStatusDetail(
                            pod_name=pod_name,
                            container_name="*",
                            state="Unknown",
                            reason=f"{phase} (no container status)",
                        )
                    )
                    continue

                for cs in container_statuses:
                    state: Literal[
                        "Waiting", "Terminated", "Running", "Unknown"
                    ] = "Unknown"
                    reason = ""
                    message = ""
                    exit_code: Optional[int] = None

                    if cs.state and cs.state.waiting:
                        state = "Waiting"
                        reason = cs.state.waiting.reason or ""
                        message = cs.state.waiting.message or ""
                    elif cs.state and cs.state.terminated:
                        state = "Terminated"
                        reason = cs.state.terminated.reason or ""
                        exit_code = cs.state.terminated.exit_code
                    elif cs.state and cs.state.running:
                        state = "Running"

                    details.append(
                        PodStatusDetail(
                            pod_name=pod_name,
                            container_name=cs.name,
                            state=state,
                            reason=reason,
                            message=message,
                            exit_code=exit_code,
                            restart_count=cs.restart_count or 0,
                        )
                    )

            return details

        except exceptions.ApiException as e:
            self._logger.debug(f"Failed to collect pod status details: {e}")
            return []
804

805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
    async def _get_pod_events(self) -> List[str]:
        """Fetch warning events for pods in this deployment's namespace."""
        try:
            assert self._core_api is not None, "Kubernetes API not initialized"
            events = await self._core_api.list_namespaced_event(self.namespace)
            warnings = []
            for event in events.items:
                if event.type != "Normal" and event.involved_object.kind == "Pod":
                    name = event.involved_object.name or "unknown"
                    reason = event.reason or ""
                    msg = event.message or ""
                    warnings.append(f"{name}: {reason} - {msg}")
            return warnings[-10:]
        except Exception as e:
            self._logger.debug(f"Failed to collect pod events: {e}")
            return []

822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
    async def _restart_nats(self):
        NATS_STS_NAME = "dynamo-platform-nats"
        NATS_LABEL = "app.kubernetes.io/component=nats"

        await self._restart_stateful(NATS_STS_NAME, NATS_LABEL)

    async def _restart_etcd(self):
        ETCD_STS_NAME = "dynamo-platform-etcd"
        ETCD_LABEL = "app.kubernetes.io/component=etcd"

        await self._restart_stateful(ETCD_STS_NAME, ETCD_LABEL)

    async def _create_deployment(self):
        """
        Create a DynamoGraphDeployment from either a dict or yaml file path.

        Args:
            deployment: Either a dict containing the deployment spec or a path to a yaml file
        """

        # Extract service names

        self._services = self.deployment_spec.services

        self._logger.info(
            f"Starting Deployment {self._deployment_name} with spec {self.deployment_spec}"
        )

        try:
            assert self._custom_api is not None, "Kubernetes API not initialized"
            await self._custom_api.create_namespaced_custom_object(
                group="nvidia.com",
                version="v1alpha1",
                namespace=self.namespace,
                plural="dynamographdeployments",
                body=self.deployment_spec.spec(),
            )
            self._logger.info(self.deployment_spec.spec())
            self._logger.info(f"Deployment Started {self._deployment_name}")
861
        except exceptions.ApiException as e:
862
863
864
865
866
867
868
869
            if e.status == 409:  # Already exists
                self._logger.info(f"Deployment {self._deployment_name} already exists")
            else:
                self._logger.info(
                    f"Failed to create deployment {self._deployment_name}: {e}"
                )
                raise

870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
    async def trigger_rolling_upgrade(self, service_names: list[str]):
        """
        Triggers a rolling update for a list of services
        This is a dummy update - sets an env var on the service
        """

        if not service_names:
            raise ValueError(
                "service_names cannot be empty for trigger_rolling_upgrade"
            )

        patch_body: dict[str, Any] = {"spec": {"services": {}}}

        for service_name in service_names:
            self.deployment_spec.set_service_env_var(
                service_name, "TEST_ROLLING_UPDATE_TRIGGER", secrets.token_hex(8)
            )

            updated_envs = self.deployment_spec.get_service_env_vars(service_name)
            patch_body["spec"]["services"][service_name] = {"envs": updated_envs}

        try:
            assert self._custom_api is not None, "Kubernetes API not initialized"
            await self._custom_api.patch_namespaced_custom_object(
                group="nvidia.com",
                version="v1alpha1",
                namespace=self.namespace,
                plural="dynamographdeployments",
                name=self._deployment_name,
                body=patch_body,
                _content_type="application/merge-patch+json",
            )
        except exceptions.ApiException as e:
            self._logger.info(
                f"Failed to patch deployment {self._deployment_name}: {e}"
            )
            raise

    async def get_pod_names(self, service_names: list[str] | None = None) -> list[str]:
        if not service_names:
            service_names = [service.name for service in self.deployment_spec.services]

        pod_names: list[str] = []

914
        for original_name in service_names:
915
            label_selector = (
916
917
                f"nvidia.com/dynamo-graph-deployment-name={self._deployment_name},"
                f"nvidia.com/dynamo-component={original_name}"
918
919
920
921
922
923
924
925
926
927
928
            )
            assert self._core_api is not None, "Kubernetes API not initialized"
            pods: client.V1PodList = await self._core_api.list_namespaced_pod(
                self.namespace, label_selector=label_selector
            )
            for pod in pods.items:
                pod_names.append(pod.metadata.name)

        return pod_names

    def get_processes(self, pod: Pod) -> list[PodProcess]:
929
930
931
932
933
934
935
936
937
938
939
940
        """Get list of processes in the given pod"""
        result = pod.exec(["ps", "-aux"])
        lines = result.stdout.decode().splitlines()
        # Skip header line
        processes = [PodProcess(pod, line) for line in lines[1:]]
        return processes

    def get_service(self, service_name=None):
        if not service_name:
            service_name = ""
        full_service_name = f"{self._deployment_name}-{service_name.lower()}"

941
        return Service.get(full_service_name, namespace=self.namespace)
942

943
944
    def get_pods(self, service_names: list[str] | None = None) -> dict[str, list[Pod]]:
        result: dict[str, list[Pod]] = {}
945

946
947
        if not service_names:
            service_names = [service.name for service in self.deployment_spec.services]
948

949
950
        for original_name in service_names:
            # List pods using stable labels that are not affected by worker hash suffixes.
951
            label_selector = (
952
953
                f"nvidia.com/dynamo-graph-deployment-name={self._deployment_name},"
                f"nvidia.com/dynamo-component={original_name}"
954
955
            )

956
            pods: list[Pod] = []
957
958
959
960

            for pod in kr8s.get(
                "pods", namespace=self.namespace, label_selector=label_selector
            ):
961
                pods.append(pod)  # type: ignore[arg-type]
962

963
            result[original_name] = pods
964
965
966

        return result

967
968
    def get_pod_manifest_logs_metrics(self, service_name: str, pod: Pod, suffix=""):
        directory = os.path.join(self.log_dir, service_name)
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
        os.makedirs(directory, exist_ok=True)

        try:
            with open(os.path.join(directory, f"{pod.name}{suffix}.yaml"), "w") as f:
                f.write(pod.to_yaml())
        except Exception as e:
            self._logger.error(e)
        try:
            with open(os.path.join(directory, f"{pod.name}{suffix}.log"), "w") as f:
                f.write("\n".join(pod.logs()))
        except Exception as e:
            self._logger.error(e)
        try:
            previous_logs = pod.logs(previous=True)
            with open(
                os.path.join(directory, f"{pod.name}{suffix}.previous.log"), "w"
            ) as f:
                f.write("\n".join(previous_logs))
        except Exception as e:
            self._logger.debug(e)

990
        self._get_pod_metrics(pod, service_name, suffix)
991
992

    def _get_service_logs(self, service_name=None, suffix=""):
993
994
995
996
997
        service_names = None
        if service_name:
            service_names = [service_name]

        service_pods = self.get_pods(service_names)
998
999

        for service, pods in service_pods.items():
1000
1001
            for pod in pods:
                self.get_pod_manifest_logs_metrics(service, pod, suffix)
1002

1003
    def _get_pod_metrics(self, pod: Pod, service_name: str, suffix=""):
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
        directory = os.path.join(self.log_dir, service_name)
        os.makedirs(directory, exist_ok=True)
        port = None
        if service_name == self.frontend_service_name:
            port = self.deployment_spec.port
        else:
            port = self.deployment_spec.system_port

        pf = self.port_forward(pod, port)

        if not pf:
            self._logger.error(f"Unable to get metrics for {service_name}")
            return

        content = None

        try:
            url = f"http://localhost:{pf.local_port}/metrics"

            response = requests.get(url, timeout=30)
            content = None
            try:
                content = response.text
            except ValueError:
                pass

        except Exception as e:
            self._logger.error(str(e))

        if content:
            with open(
                os.path.join(directory, f"{pod.name}.metrics{suffix}.log"), "w"
            ) as f:
                f.write(content)

    async def _delete_deployment(self):
        """
        Delete the DynamoGraphDeployment CR.
        """
        try:
            if self._deployment_name and self._custom_api is not None:
                await self._custom_api.delete_namespaced_custom_object(
                    group="nvidia.com",
                    version="v1alpha1",
                    namespace=self.namespace,
                    plural="dynamographdeployments",
                    name=self._deployment_name,
                )
1052
        except exceptions.ApiException as e:
1053
1054
1055
            if e.status != 404:  # Ignore if already deleted
                raise

1056
1057
1058
    def port_forward(
        self, pod: Pod, remote_port: int, max_connection_attempts: int = 3
    ):
1059
        """Attempt to connect to a pod and return the port-forward object on success.
1060

1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
        Note: Port forwards run in background threads. When pods are terminated,
        the async cleanup may fail, which is expected and can be safely ignored.
        """
        try:
            # Create port forward - this runs in a background thread
            # Use 127.0.0.1 (localhost) instead of 0.0.0.0 to prevent port conflicts
            port_forward = pod.portforward(
                remote_port=remote_port,
                local_port=0,  # Auto-assign an available port
                address="127.0.0.1",  # Use localhost for better isolation and conflict prevention
            )
            port_forward.start()
1073

1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
            # Try to connect with exponential backoff
            backoff_delay = 0.5  # Start with 500ms

            for attempt in range(max_connection_attempts):
                time.sleep(backoff_delay)
                backoff_delay = min(
                    backoff_delay * 1.5, 5.0
                )  # Double delay, max 5 seconds

                # Check if port is assigned
                if port_forward.local_port == 0:
                    self._logger.debug(
                        f"Port not yet assigned for pod {pod.name} (attempt {attempt+1}/{max_connection_attempts})"
                    )
                    continue

                # Try to connect to the port forwarded service
                test_url = f"http://localhost:{port_forward.local_port}/"
                try:
                    # Send HEAD request to test connection
                    response = requests.head(test_url, timeout=5)
                    if response.status_code in (200, 404):  # 404 is acceptable
                        self._active_port_forwards.append(port_forward)
                        return port_forward
                except (requests.ConnectionError, requests.Timeout) as e:
                    self._logger.warning(
                        f"Connection test failed for pod {pod.name} (attempt {attempt+1}/{max_connection_attempts}): {e}"
                    )

                # Restart port-forward for next attempt (except on last attempt)
                if attempt == max_connection_attempts - 1:
                    continue
                try:
                    port_forward.stop()
                    port_forward.start()
                except Exception as e:
                    self._logger.debug(
                        f"Error restarting port forward for pod {pod.name}: {e}"
                    )
                    break

            # All attempts failed
            self._logger.warning(
                f"Port forward failed after {max_connection_attempts} attempts for pod {pod.name}"
            )
1119
            try:
1120
1121
1122
1123
                port_forward.stop()
            except Exception:
                pass  # Ignore errors during cleanup
            return None
1124

1125
1126
1127
1128
1129
        except Exception as e:
            self._logger.warning(
                f"Failed to create port forward for pod {pod.name}: {e}"
            )
            return None
1130
1131
1132

    async def _cleanup(self):
        try:
1133
            # Collect logs/metrics first; any PFs opened here will be tracked and stopped below.
1134
            self._get_service_logs()
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
            self._logger.info(
                f"Cleaning up {len(self._active_port_forwards)} active port forwards"
            )
            for port_forward in self._active_port_forwards:
                try:
                    port_forward.stop()
                except RuntimeError as e:
                    # Expected error when pod is terminated:
                    # "anext(): asynchronous generator is already running"
                    if "anext()" in str(e) or "already running" in str(e):
                        self._logger.debug(f"Port forward cleanup: {e}")
                    else:
                        self._logger.warning(
                            f"Unexpected error stopping port forward: {e}"
                        )
                except Exception as e:
                    self._logger.debug(f"Error stopping port forward: {e}")
            self._active_port_forwards.clear()
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
        finally:
            await self._delete_deployment()

    async def __aenter__(self):
        try:
            self._logger = logging.getLogger(self.__class__.__name__)
            self.deployment_spec.namespace = self.namespace
            self._deployment_name = self.deployment_spec.name
            logging.getLogger("httpx").setLevel(logging.WARNING)
            await self._init_kubernetes()
1163
1164
1165
1166
1167
1168
1169

            # Run delete deployment and service restarts in parallel
            tasks = [self._delete_deployment()]
            if not self.skip_service_restart:
                tasks.extend([self._restart_etcd(), self._restart_nats()])
            await asyncio.gather(*tasks)

1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
            await self._create_deployment()
            await self._wait_for_ready()

        except:
            await self._cleanup()
            raise
        return self

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        await self._cleanup()


1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
class ManagedDGDR:
    """Async helper for managing DynamoGraphDeploymentRequest custom resources.

    Provides CRUD operations and phase-polling against the DGDR CRD using the
    ``kubernetes_asyncio`` client, following the same patterns as
    ``ManagedDeployment`` (shared kubeconfig initialisation, timeout logic,
    structured error messages).

    Typical usage from a pytest fixture::

        dgdr = ManagedDGDR(namespace="default")
        await dgdr.init()
        await dgdr.create(manifest)
        phase = await dgdr.wait_for_phase(name, "Ready", timeout=600)
        await dgdr.delete(name)
        await dgdr.close()
    """

    # CRD coordinates for DGDR
    DGDR_GROUP = "nvidia.com"
    DGDR_VERSION = "v1beta1"
    DGDR_PLURAL = "dynamographdeploymentrequests"

    # CRD coordinates for DGD (for mocker cleanup)
    DGD_PLURAL = "dynamographdeployments"

    DEFAULT_POLL_INTERVAL = 10  # seconds

    def __init__(
        self,
        namespace: str = "default",
        loop: Optional[asyncio.AbstractEventLoop] = None,
    ):
        self.namespace = namespace
        self._custom_api: Optional[client.CustomObjectsApi] = None
        self._api_client: Optional[client.ApiClient] = None
        self._logger = logging.getLogger(self.__class__.__name__)
        self._loop = loop

    def run(self, coro):
        """Run an async coroutine synchronously using the stored event loop.

        Convenience for callers that are not themselves async (e.g. pytest
        fixtures and synchronous test methods).
        """
        if self._loop is None:
            raise RuntimeError(
                "No event loop set on ManagedDGDR; pass loop= at construction or call init() first"
            )
        return self._loop.run_until_complete(coro)

    async def init(self) -> None:
        """Initialise the kubernetes_asyncio client.

        Priority: KUBECONFIG env → in-cluster → ~/.kube/config  (same as
        ManagedDeployment._init_kubernetes).
        """
        kubeconfig_path = os.environ.get("KUBECONFIG")

        if kubeconfig_path and os.path.exists(kubeconfig_path):
            self._logger.info("Loading kubeconfig from KUBECONFIG: %s", kubeconfig_path)
            await config.load_kube_config(config_file=kubeconfig_path)
        else:
            try:
                self._logger.info("Attempting in-cluster kubernetes config")
                config.load_incluster_config()
            except Exception as e:
                self._logger.warning(
                    "In-cluster config failed (%s: %s), falling back to default kubeconfig",
                    type(e).__name__,
                    e,
                )
                await config.load_kube_config()

        self._api_client = client.ApiClient()
        self._custom_api = client.CustomObjectsApi(self._api_client)

    async def close(self) -> None:
        """Close the underlying API client."""
        if self._api_client:
            await self._api_client.close()
            self._api_client = None
            self._custom_api = None

    # ----- CRUD -----

    async def create(self, manifest: dict) -> str:
        """Create a DGDR custom resource.  Returns the resource name."""
        assert self._custom_api is not None, "call init() first"
        name = manifest["metadata"]["name"]
        await self._custom_api.create_namespaced_custom_object(
            group=self.DGDR_GROUP,
            version=self.DGDR_VERSION,
            namespace=self.namespace,
            plural=self.DGDR_PLURAL,
            body=manifest,
        )
        self._logger.info("Created DGDR %s/%s", self.namespace, name)
        return name

    async def get(self, name: str) -> Optional[dict]:
        """Get a DGDR as a dict, or ``None`` if not found."""
        assert self._custom_api is not None, "call init() first"
        try:
            return await self._custom_api.get_namespaced_custom_object(
                group=self.DGDR_GROUP,
                version=self.DGDR_VERSION,
                namespace=self.namespace,
                plural=self.DGDR_PLURAL,
                name=name,
            )
        except exceptions.ApiException as e:
            if e.status == 404:
                return None
            raise

    async def delete(self, name: str, ignore_not_found: bool = True) -> None:
        """Delete a DGDR."""
        assert self._custom_api is not None, "call init() first"
        try:
            await self._custom_api.delete_namespaced_custom_object(
                group=self.DGDR_GROUP,
                version=self.DGDR_VERSION,
                namespace=self.namespace,
                plural=self.DGDR_PLURAL,
                name=name,
            )
            self._logger.info("Deleted DGDR %s/%s", self.namespace, name)
        except exceptions.ApiException as e:
            if e.status == 404 and ignore_not_found:
                return
            raise

    async def list(self, label_selector: str = "") -> List[dict]:
        """List DGDRs, optionally filtered by label selector.  Returns items."""
        assert self._custom_api is not None, "call init() first"
        resp = await self._custom_api.list_namespaced_custom_object(
            group=self.DGDR_GROUP,
            version=self.DGDR_VERSION,
            namespace=self.namespace,
            plural=self.DGDR_PLURAL,
            label_selector=label_selector,
        )
        return resp.get("items", [])

    async def server_dry_run(self, manifest: dict) -> dict:
        """Apply with server-side dry-run to validate admission webhooks.

        Returns the API response dict.  Raises ``ApiException`` on rejection.
        """
        assert self._custom_api is not None, "call init() first"
        return await self._custom_api.create_namespaced_custom_object(
            group=self.DGDR_GROUP,
            version=self.DGDR_VERSION,
            namespace=self.namespace,
            plural=self.DGDR_PLURAL,
            body=manifest,
            dry_run="All",
        )

    # ----- Phase helpers -----

    async def get_phase(self, name: str) -> Optional[str]:
        """Return ``status.phase`` of the named DGDR, or ``None``."""
        obj = await self.get(name)
        if obj is None:
            return None
        return obj.get("status", {}).get("phase")

    async def get_condition(self, name: str, condition_type: str) -> Optional[dict]:
        """Return the named condition dict from ``status.conditions``."""
        obj = await self.get(name)
        if obj is None:
            return None
        for c in obj.get("status", {}).get("conditions", []):
            if c.get("type") == condition_type:
                return c
        return None

    async def wait_for_phase(
        self,
        name: str,
        target_phase: str,
        timeout: int = 3600,
        fail_fast_phases: Optional[List[str]] = None,
        poll_interval: int = DEFAULT_POLL_INTERVAL,
    ) -> str:
        """Poll until the DGDR reaches *target_phase* or times out.

        Returns the final observed phase.  Raises ``AssertionError`` on
        fail-fast and ``TimeoutError`` on timeout.
        """
        if fail_fast_phases is None:
            fail_fast_phases = ["Failed"]

        deadline = time.monotonic() + timeout
        last_phase: Optional[str] = None

        while time.monotonic() < deadline:
            current = await self.get_phase(name)
            if current != last_phase:
                self._logger.info("DGDR %s/%s phase: %s", self.namespace, name, current)
                last_phase = current

            if current == target_phase:
                return current
            if current in fail_fast_phases:
                obj = await self.get(name)
                conditions = obj.get("status", {}).get("conditions", []) if obj else []
                raise AssertionError(
                    f"DGDR {self.namespace}/{name} reached fail-fast phase {current!r} "
                    f"while waiting for {target_phase!r}. conditions={conditions}"
                )
            await asyncio.sleep(poll_interval)

        raise TimeoutError(
            f"Timed out after {timeout}s waiting for DGDR {self.namespace}/{name} "
            f"to reach phase {target_phase!r}. Last phase: {last_phase!r}"
        )

    async def wait_for_any_phase(
        self,
        name: str,
        target_phases: List[str],
        timeout: int = 3600,
        poll_interval: int = DEFAULT_POLL_INTERVAL,
    ) -> str:
        """Poll until the DGDR reaches any of *target_phases*.  Returns matched phase."""
        deadline = time.monotonic() + timeout
        last_phase: Optional[str] = None

        while time.monotonic() < deadline:
            current = await self.get_phase(name)
            if current != last_phase:
                self._logger.info("DGDR %s/%s phase: %s", self.namespace, name, current)
                last_phase = current
            if current in target_phases:
                return current
            await asyncio.sleep(poll_interval)

        raise TimeoutError(
            f"Timed out after {timeout}s waiting for DGDR {self.namespace}/{name} "
            f"to reach any of {target_phases!r}. Last phase: {last_phase!r}"
        )

    # ----- DGD helpers (for mocker cleanup) -----

    async def delete_dgd(self, name: str, ignore_not_found: bool = True) -> None:
        """Delete a DynamoGraphDeployment resource."""
        assert self._custom_api is not None, "call init() first"
        try:
            await self._custom_api.delete_namespaced_custom_object(
                group=self.DGDR_GROUP,
                version="v1alpha1",
                namespace=self.namespace,
                plural=self.DGD_PLURAL,
                name=name,
            )
            self._logger.info("Deleted DGD %s/%s", self.namespace, name)
        except exceptions.ApiException as e:
            if e.status == 404 and ignore_not_found:
                return
            raise

    async def get_dgd(self, name: str) -> Optional[dict]:
        """Get a DynamoGraphDeployment, or ``None`` if not found."""
        assert self._custom_api is not None, "call init() first"
        try:
            return await self._custom_api.get_namespaced_custom_object(
                group=self.DGDR_GROUP,
                version="v1alpha1",
                namespace=self.namespace,
                plural=self.DGD_PLURAL,
                name=name,
            )
        except exceptions.ApiException as e:
            if e.status == 404:
                return None
            raise


1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
async def main():
    LOG_FORMAT = "[TEST] %(asctime)s %(levelname)s %(name)s: %(message)s"
    DATE_FORMAT = "%Y-%m-%dT%H:%M:%S"

    # Configure logging
    logging.basicConfig(
        level=logging.INFO,
        format=LOG_FORMAT,
        datefmt=DATE_FORMAT,  # ISO 8601 UTC format
    )

1474
1475
    # Get workspace directory
    workspace_dir = _get_workspace_dir()
1476

1477
    deployment_spec = DeploymentSpec(
1478
        os.path.join(workspace_dir, "examples/backends/vllm/deploy/agg.yaml")
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
    )

    deployment_spec.disable_grove()

    print(deployment_spec._deployment_spec)

    deployment_spec.name = "foo"

    deployment_spec.set_image("nvcr.io/nvidia/ai-dynamo/vllm-runtime:0.4.1")

    # Configure logging
    deployment_spec.set_logging(enable_jsonl=True, log_level="debug")

    print(f"Logging config: {deployment_spec.get_logging_config()}")

    async with ManagedDeployment(
        namespace="test", log_dir=".", deployment_spec=deployment_spec
    ):
        time.sleep(60)


if __name__ == "__main__":
    asyncio.run(main())