Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ dev = [
"testcontainers>=4.9.2",
"pytest-xdist>=3.6.1",
"pyinstrument>=5.0.0",
"kubernetes-stubs-elephant-fork",
"kubernetes-stubs-elephant-fork>=35.0.0.post3",
# docs
"dstack[server]; python_version >= '3.11'",
"dstack-plugin-server; python_version >= '3.11'",
Expand Down
25 changes: 19 additions & 6 deletions src/dstack/_internal/core/backends/kubernetes/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
from collections.abc import Generator
from contextlib import contextmanager
from typing import Annotated, Any, Callable, Generic, Literal, Optional, Protocol, TypeVar, Union
from typing import (
Annotated,
Any,
Callable,
Generic,
Literal,
Optional,
Protocol,
TypeVar,
Union,
cast,
)

import yaml
from kubernetes.client import CoreV1Api, V1Status
Expand All @@ -9,9 +20,7 @@
# XXX: This function is missing in the stubs package
new_client_from_config_dict, # pyright: ignore[reportAttributeAccessIssue]
)

# XXX: The watch module is missing in the stubs package
from kubernetes.watch import Watch # pyright: ignore[reportMissingImports]
from kubernetes.watch import Watch
from pydantic import Field
from typing_extensions import ParamSpec, TypedDict
from urllib3.exceptions import HTTPError
Expand Down Expand Up @@ -157,7 +166,8 @@ def watch_events(
method: Callable[P, ObjectList[T]], *args: P.args, **kwargs: P.kwargs
) -> Generator[Generator[tuple[str, T], None, None], None, None]:
watch = Watch()
gen = _watch_events_gen(watch.stream(method, *args, **kwargs))
inner_gen = cast(Generator[_EventDict[T], None, None], watch.stream(method, *args, **kwargs))
gen = _watch_events_gen(inner_gen)
try:
yield gen
finally:
Expand All @@ -182,8 +192,11 @@ class _ErrorEventDict(TypedDict):
object: V1Status


_EventDict = Union[_StateEventDict[T], _BookmarkEventDict[T], _ErrorEventDict]


def _watch_events_gen(
gen: Generator[Union[_StateEventDict[T], _BookmarkEventDict[T], _ErrorEventDict], None, None],
gen: Generator[_EventDict[T], None, None],
) -> Generator[tuple[str, T], None, None]:
try:
for event in gen:
Expand Down
Loading