Skip to content

Commit 096cd4b

Browse files
hotzenklotznormanrzNorman Rzepka
authored
Reduce default wk-libs dependencies (#1024)
* create an optional dependency group for wk-libs examples * remove nifti script to avoid sklearn dependency * make example deps optional * install dev deps in CI * extract cluster_tool extra dependencies * lazily load kubernetes * fix CI? * lazy import distributed for dask * updated readmes * installed python pacakges into virtual env in Docker * updated dockerfile * Update ci.yml * Update Dockerfile * fixes for kubernetes * dockerfile refactor * no venv for poetry * Update cluster_tools/Changelog.md Co-authored-by: Norman Rzepka <[email protected]> * Update cluster_tools/cluster_tools/executors/dask.py Co-authored-by: Norman Rzepka <[email protected]> * Update cluster_tools/cluster_tools/schedulers/kube.py Co-authored-by: Norman Rzepka <[email protected]> * Update webknossos/Changelog.md Co-authored-by: Norman Rzepka <[email protected]> --------- Co-authored-by: Norman Rzepka <[email protected]> Co-authored-by: Norman Rzepka <[email protected]>
1 parent b62883e commit 096cd4b

File tree

19 files changed

+547
-1032
lines changed

19 files changed

+547
-1032
lines changed

.github/workflows/ci.yml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,22 @@ jobs:
8484
./kind create cluster --config=tests/cluster-config.yaml
8585
./kind export kubeconfig
8686
87+
cp ../requirements.txt .
8788
docker build -f tests/Dockerfile -t scalableminds/cluster-tools:latest .
8889
./kind load docker-image scalableminds/cluster-tools:latest
8990
9091
- name: Install dependencies (without docker)
91-
if: ${{ matrix.executors != 'slurm' }}
92+
if: ${{ matrix.executors == 'multiprocessing' }}
9293
run: |
9394
pip install -r ../requirements.txt
9495
poetry install
9596
97+
- name: Install dependencies (without docker)
98+
if: ${{ matrix.executors == 'kubernetes' || matrix.executors == 'dask' }}
99+
run: |
100+
pip install -r ../requirements.txt
101+
poetry install --all-extras
102+
96103
- name: Check typing
97104
if: ${{ matrix.executors == 'multiprocessing' && matrix.python-version == '3.11' }}
98105
run: ./typecheck.sh
@@ -163,7 +170,7 @@ jobs:
163170
- name: Install dependencies
164171
run: |
165172
pip install -r ../requirements.txt
166-
poetry install --extras all
173+
poetry install --extras all --with examples --with dev
167174
168175
- name: Check formatting
169176
if: ${{ matrix.group == 1 && matrix.python-version == '3.11' }}

.github/workflows/nightly.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
- name: Install dependencies
3030
run: |
3131
pip install poetry
32-
poetry install --extras all
32+
poetry install --extras all --with examples --with dev
3333
3434
- name: Check if git is dirty
3535
run: |

cluster_tools/Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ For upgrade instructions, please check the respective *Breaking Changes* section
1414
### Added
1515

1616
### Changed
17+
- Moved the dependencies for Kubernetes and Dask support into optional extras that need to be installed separately. Either with `poetry install --all-extras or pip install -e "all"`. [#1024](https://github.com/scalableminds/webknossos-libs/pull/1024)
1718

1819
### Fixed
1920

cluster_tools/README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Code Style](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://docs.astral.sh/ruff/)
55

66

7-
This package provides python `Executor` classes for distributing tasks on a slurm cluster or via multi processing.
7+
This package provides python `Executor` classes for distributing tasks on a Slurm cluster, Kubernetes, Dask or via multi processing.
88

99
## Example
1010

@@ -21,6 +21,23 @@ if __name__ == '__main__':
2121
assert result == [4, 9, 16]
2222
```
2323

24+
## Installation
25+
The `cluster_tools` package requires at least Python 3.8.
26+
27+
You can install it from [pypi](https://pypi.org/project/cluster_tools/), e.g. via pip:
28+
29+
```bash
30+
pip install cluster_tools
31+
```
32+
33+
By default only the dependencies for running jobs on Slurm and via multiprocessing are installed.
34+
For Kubernetes and Dask run:
35+
36+
```bash
37+
pip install cluster_tools[kubernetes]
38+
pip install cluster_tools[dask]
39+
```
40+
2441
## Configuration
2542

2643
### Slurm
@@ -63,6 +80,8 @@ docker exec -it slurmctld bash
6380
docker exec -it c1 bash
6481
```
6582

83+
Make sure to install all extra dependencies, such as Kubernetes, with `poetry install --all-extras`.
84+
6685
Tests can be executed with `cd tests && poetry run pytest -s tests.py` after entering the container.
6786
Linting can be run with `./lint.sh`.
6887
Code formatting (black) can be run with `./format.sh`.

cluster_tools/cluster_tools/executors/dask.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import os
23
import re
34
import signal
@@ -28,6 +29,9 @@
2829
if TYPE_CHECKING:
2930
from distributed import Client
3031

32+
logger = logging.getLogger()
33+
34+
3135
_T = TypeVar("_T")
3236
_P = ParamSpec("_P")
3337
_S = TypeVar("_S")
@@ -110,6 +114,14 @@ class DaskExecutor(futures.Executor):
110114
def __init__(
111115
self, client: "Client", job_resources: Optional[Dict[str, Any]] = None
112116
) -> None:
117+
try:
118+
import distributed # noqa: F401 unused import
119+
except ModuleNotFoundError:
120+
logger.error(
121+
'The distributed Python package for Dask is not installed. cluster_tools does not install this dependency be default. Run `pip install cluster_tools[dask]` or `poetry install --extras "dask"` to install Dask support.'
122+
)
123+
exit()
124+
113125
self.client = client
114126
self.pending_futures = set()
115127
self.job_resources = job_resources

cluster_tools/cluster_tools/schedulers/kube.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Abstracts access to a Kubernetes cluster via its Python library."""
22

3+
import logging
34
import os
45
import re
56
import sys
@@ -8,11 +9,10 @@
89
from typing import Any, Dict, List, Literal, Optional, Tuple
910
from uuid import uuid4
1011

11-
import kubernetes
12-
import kubernetes.client.models as kubernetes_models
13-
1412
from cluster_tools.schedulers.cluster_executor import ClusterExecutor
1513

14+
logger = logging.getLogger()
15+
1616

1717
def _volume_name_from_path(path: Path) -> str:
1818
return f"{(hash(str(path)) & sys.maxsize):016x}"
@@ -30,6 +30,8 @@ def _deduplicate_mounts(mounts: List[Path]) -> List[Path]:
3030

3131
class KubernetesClient:
3232
def __init__(self) -> None:
33+
import kubernetes
34+
3335
kubernetes.config.load_kube_config()
3436
self.core = kubernetes.client.api.core_v1_api.CoreV1Api()
3537
self.batch = kubernetes.client.api.batch_v1_api.BatchV1Api()
@@ -48,6 +50,14 @@ def __init__(
4850
additional_setup_lines: Optional[List[str]] = None,
4951
**kwargs: Any,
5052
):
53+
try:
54+
import kubernetes # noqa: F401 unused import
55+
except ModuleNotFoundError:
56+
logger.error(
57+
'The Kubernetes Python package is not installed. cluster_tools does not install this dependency be default. Run `pip install cluster_tools[kubernetes]` or `poetry install --extras "kubernetes"` to install Kubernetes support.'
58+
)
59+
exit()
60+
5161
super().__init__(
5262
debug=debug,
5363
keep_logs=keep_logs,
@@ -117,6 +127,9 @@ def inner_handle_kill(
117127
)
118128

119129
def ensure_kubernetes_namespace(self) -> None:
130+
import kubernetes
131+
import kubernetes.client.models as kubernetes_models
132+
120133
kubernetes_client = KubernetesClient()
121134
try:
122135
kubernetes_client.core.read_namespace(self.job_resources["namespace"])
@@ -145,6 +158,8 @@ def inner_submit(
145158
) -> Tuple[List["Future[str]"], List[Tuple[int, int]]]:
146159
"""Starts a Kubernetes pod that runs the specified shell command line."""
147160

161+
import kubernetes.client.models as kubernetes_models
162+
148163
kubernetes_client = KubernetesClient()
149164
self.ensure_kubernetes_namespace()
150165
job_id = str(uuid4())

cluster_tools/dockered-slurm/docker-compose.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ services:
2222
volumes:
2323
- etc_munge:/etc/munge
2424
- etc_slurm:/etc/slurm
25-
- ${PWD}/slurm.conf:/etc/slurm/slurm.conf
26-
- ${PWD}/cgroup.conf:/etc/slurm/cgroup.conf
25+
- ./slurm.conf:/etc/slurm/slurm.conf
26+
- ./cgroup.conf:/etc/slurm/cgroup.conf
2727
- var_log_slurm:/var/log/slurm
2828
expose:
2929
- "6819"
@@ -40,8 +40,8 @@ services:
4040
volumes:
4141
- etc_munge:/etc/munge
4242
- etc_slurm:/etc/slurm
43-
- ${PWD}/slurm.conf:/etc/slurm/slurm.conf
44-
- ${PWD}/cgroup.conf:/etc/slurm/cgroup.conf
43+
- ./slurm.conf:/etc/slurm/slurm.conf
44+
- ./cgroup.conf:/etc/slurm/cgroup.conf
4545
- ./slurm_jobdir:/data
4646
- ..:/cluster_tools
4747
- var_log_slurm:/var/log/slurm
@@ -59,8 +59,8 @@ services:
5959
volumes:
6060
- etc_munge:/etc/munge
6161
- etc_slurm:/etc/slurm
62-
- ${PWD}/slurm.conf:/etc/slurm/slurm.conf
63-
- ${PWD}/cgroup.conf:/etc/slurm/cgroup.conf
62+
- ./slurm.conf:/etc/slurm/slurm.conf
63+
- ./cgroup.conf:/etc/slurm/cgroup.conf
6464
- ./slurm_jobdir:/data
6565
- ..:/cluster_tools
6666
- var_log_slurm:/var/log/slurm
@@ -77,8 +77,8 @@ services:
7777
volumes:
7878
- etc_munge:/etc/munge
7979
- etc_slurm:/etc/slurm
80-
- ${PWD}/slurm.conf:/etc/slurm/slurm.conf
81-
- ${PWD}/cgroup.conf:/etc/slurm/cgroup.conf
80+
- ./slurm.conf:/etc/slurm/slurm.conf
81+
- ./cgroup.conf:/etc/slurm/cgroup.conf
8282
- ./slurm_jobdir:/data
8383
- ..:/cluster_tools
8484
- var_log_slurm:/var/log/slurm

0 commit comments

Comments
 (0)