Skip to content

Commit 9855fd4

Browse files
author
Jonas Dedden
committed
General code quality improvements for the controller part of this repository.
Make sure exported symbols are correctly set in __init__.py with __all__. Renamed pytest fixture for creating a k8s namespace from ns to namespace since ns is a bit overloaded, especially in test_controller.py Introduce typehints in controller.py and test_controller.py to massively improve developer experience (previously, one just really had to guess what functions are for and which types are expected) Removed unused function parameters Made function headers (i.e. parameter [type] defintion) compatible with what `kopf.on.[create/change/...]' expects Introduce mypy (and missing stubs of external packages if available) Introduce mypy ignores for (still) untyped parts of the codebase for now
1 parent 323e0d1 commit 9855fd4

File tree

9 files changed

+483
-241
lines changed

9 files changed

+483
-241
lines changed

dask_kubernetes/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from importlib import import_module
2-
from warnings import warn
32

43
from . import config
54
from . import _version

dask_kubernetes/classic/tests/test_async.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -773,7 +773,7 @@ async def test_start_with_workers(k8s_cluster, pod_spec):
773773

774774
@pytest.mark.anyio
775775
@pytest.mark.xfail(reason="Flaky in CI and classic is deprecated anyway")
776-
async def test_adapt_delete(cluster, ns):
776+
async def test_adapt_delete(cluster, namespace):
777777
"""
778778
testing whether KubeCluster.adapt will bring
779779
back deleted worker pod (issue #244)
@@ -782,7 +782,7 @@ async def test_adapt_delete(cluster, ns):
782782

783783
async def get_worker_pods():
784784
pods_list = await core_api.list_namespaced_pod(
785-
namespace=ns,
785+
namespace=namespace,
786786
label_selector=f"dask.org/component=worker,dask.org/cluster-name={cluster.name}",
787787
)
788788
return [x.metadata.name for x in pods_list.items]
@@ -797,7 +797,7 @@ async def get_worker_pods():
797797
assert len(worker_pods) == 2
798798
# delete one worker pod
799799
to_delete = worker_pods[0]
800-
await core_api.delete_namespaced_pod(name=to_delete, namespace=ns)
800+
await core_api.delete_namespaced_pod(name=to_delete, namespace=namespace)
801801
# wait until it is deleted
802802
start = time()
803803
while True:

dask_kubernetes/conftest.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,38 @@
55
import sys
66
import tempfile
77
import uuid
8+
from typing import Final, Iterator
89

910
import pytest
1011
from kopf.testing import KopfRunner
1112
from pytest_kind.cluster import KindCluster
1213

1314
from dask_kubernetes.common.utils import check_dependency
1415

15-
DIR = pathlib.Path(__file__).parent.absolute()
16+
DIR: Final[pathlib.Path] = pathlib.Path(__file__).parent.absolute()
1617

1718
check_dependency("helm")
1819
check_dependency("kubectl")
1920
check_dependency("docker")
2021

21-
DISABLE_LOGGERS = ["httpcore", "httpx"]
22+
DISABLE_LOGGERS: Final[list[str]] = ["httpcore", "httpx"]
2223

2324

24-
def pytest_configure():
25+
def pytest_configure() -> None:
2526
for logger_name in DISABLE_LOGGERS:
2627
logger = logging.getLogger(logger_name)
2728
logger.disabled = True
2829

2930

3031
@pytest.fixture()
31-
def kopf_runner(k8s_cluster, ns):
32+
def kopf_runner(k8s_cluster: KindCluster, namespace: str) -> KopfRunner:
3233
yield KopfRunner(
33-
["run", "-m", "dask_kubernetes.operator", "--verbose", "--namespace", ns]
34+
["run", "-m", "dask_kubernetes.operator", "--verbose", "--namespace", namespace]
3435
)
3536

3637

3738
@pytest.fixture(scope="session")
38-
def docker_image():
39+
def docker_image() -> str:
3940
image_name = "dask-kubernetes:dev"
4041
python_version = f"{sys.version_info.major}.{sys.version_info.minor}"
4142
subprocess.run(
@@ -54,7 +55,9 @@ def docker_image():
5455

5556

5657
@pytest.fixture(scope="session")
57-
def k8s_cluster(request, docker_image):
58+
def k8s_cluster(
59+
request: pytest.FixtureRequest, docker_image: str
60+
) -> Iterator[KindCluster]:
5861
image = None
5962
if version := os.environ.get("KUBERNETES_VERSION"):
6063
image = f"kindest/node:v{version}"
@@ -73,7 +76,7 @@ def k8s_cluster(request, docker_image):
7376

7477

7578
@pytest.fixture(scope="session", autouse=True)
76-
def install_istio(k8s_cluster):
79+
def install_istio(k8s_cluster: KindCluster) -> None:
7780
if bool(os.environ.get("TEST_ISTIO", False)):
7881
check_dependency("istioctl")
7982
subprocess.run(
@@ -85,15 +88,15 @@ def install_istio(k8s_cluster):
8588

8689

8790
@pytest.fixture(autouse=True)
88-
def ns(k8s_cluster):
91+
def namespace(k8s_cluster: KindCluster) -> Iterator[str]:
8992
ns = "dask-k8s-pytest-" + uuid.uuid4().hex[:10]
9093
k8s_cluster.kubectl("create", "ns", ns)
9194
yield ns
9295
k8s_cluster.kubectl("delete", "ns", ns, "--wait=false", "--ignore-not-found=true")
9396

9497

9598
@pytest.fixture(scope="session", autouse=True)
96-
def install_gateway(k8s_cluster):
99+
def install_gateway(k8s_cluster: KindCluster) -> Iterator[None]:
97100
if bool(os.environ.get("TEST_DASK_GATEWAY", False)):
98101
check_dependency("helm")
99102
# To ensure the operator can coexist with Gateway
@@ -129,11 +132,11 @@ def install_gateway(k8s_cluster):
129132

130133

131134
@pytest.fixture(scope="session", autouse=True)
132-
def customresources(k8s_cluster):
135+
def customresources(k8s_cluster: KindCluster) -> Iterator[None]:
133136
temp_dir = tempfile.TemporaryDirectory()
134137
crd_path = os.path.join(DIR, "operator", "customresources")
135138

136-
def run_generate(crd_path, patch_path, temp_path):
139+
def run_generate(crd_path: str, patch_path: str, temp_path: str) -> None:
137140
subprocess.run(
138141
["k8s-crd-resolver", "-r", "-j", patch_path, crd_path, temp_path],
139142
check=True,
@@ -154,5 +157,5 @@ def run_generate(crd_path, patch_path, temp_path):
154157

155158

156159
@pytest.fixture
157-
def anyio_backend():
160+
def anyio_backend() -> str:
158161
return "asyncio"

dask_kubernetes/operator/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@
55
make_worker_spec,
66
discover,
77
)
8+
9+
__all__ = [
10+
"KubeCluster",
11+
"make_cluster_spec",
12+
"make_scheduler_spec",
13+
"make_worker_spec",
14+
"discover",
15+
]

0 commit comments

Comments
 (0)