From 2714893288d5a6950e1bc458a2763fe0ab92aa7d Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Mon, 9 Dec 2019 15:55:41 +0100 Subject: [PATCH] mgr/rook: Added Mypy static type checking Signed-off-by: Sebastian Wagner --- src/pybind/mgr/orchestrator.py | 5 +++-- src/pybind/mgr/requirements.txt | 1 + src/pybind/mgr/rook/module.py | 17 +++++++++++------ src/pybind/mgr/rook/rook_cluster.py | 20 +++++++++++++------- src/pybind/mgr/tox.ini | 2 +- 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/src/pybind/mgr/orchestrator.py b/src/pybind/mgr/orchestrator.py index 65120ac87b120..3b8b8909c0204 100644 --- a/src/pybind/mgr/orchestrator.py +++ b/src/pybind/mgr/orchestrator.py @@ -26,7 +26,8 @@ from mgr_util import format_bytes try: from ceph.deployment.drive_group import DriveGroupSpec - from typing import TypeVar, Generic, List, Optional, Union, Tuple, Iterator, Callable, Any, Type + from typing import TypeVar, Generic, List, Optional, Union, Tuple, Iterator, Callable, Any, \ + Type, Sequence except ImportError: pass @@ -606,7 +607,7 @@ class Completion(_Promise): def pretty_print(completions): - # type: (List[Completion]) -> str + # type: (Sequence[Completion]) -> str return ', '.join(c.pretty_print() for c in completions) diff --git a/src/pybind/mgr/requirements.txt b/src/pybind/mgr/requirements.txt index fe76eee7964f6..c60dc4a8982d6 100644 --- a/src/pybind/mgr/requirements.txt +++ b/src/pybind/mgr/requirements.txt @@ -2,3 +2,4 @@ pytest-cov==2.7.1 mock; python_version <= '3.3' ipaddress; python_version < '3.3' ../../python-common +kubernetes diff --git a/src/pybind/mgr/rook/module.py b/src/pybind/mgr/rook/module.py index 4b534d967f21b..6ed4a0e917db9 100644 --- a/src/pybind/mgr/rook/module.py +++ b/src/pybind/mgr/rook/module.py @@ -5,7 +5,7 @@ import os from ceph.deployment import inventory try: - from typing import List, Dict, Optional, Callable, TypeVar + from typing import List, Dict, Optional, Callable, Any from ceph.deployment.drive_group import DriveGroupSpec except ImportError: pass # just for type checking @@ -97,7 +97,7 @@ class RookOrchestrator(MgrModule, orchestrator.Orchestrator): MODULE_OPTIONS = [ # TODO: configure k8s API addr instead of assuming local - ] + ] # type: List[Dict[str, Any]] def process(self, completions): # type: (List[RookCompletion]) -> None @@ -146,13 +146,16 @@ class RookOrchestrator(MgrModule, orchestrator.Orchestrator): @property def k8s(self): + # type: () -> client.CoreV1Api self._initialized.wait() + assert self._k8s is not None return self._k8s @property def rook_cluster(self): # type: () -> RookCluster self._initialized.wait() + assert self._rook_cluster is not None return self._rook_cluster def serve(self): @@ -346,9 +349,11 @@ class RookOrchestrator(MgrModule, orchestrator.Orchestrator): def update_mds(self, spec): num = spec.count - return RookWriteCompletion( - lambda: self.rook_cluster.update_mds_count(spec.name, num), None, - "Updating MDS server count in {0} to {1}".format(spec.name, num)) + return write_completion( + lambda: self.rook_cluster.update_mds_count(spec.name, num), + "Updating MDS server count in {0} to {1}".format(spec.name, num), + mgr=self + ) def update_nfs(self, spec): num = spec.count @@ -395,7 +400,7 @@ class RookOrchestrator(MgrModule, orchestrator.Orchestrator): def has_osds(all_hosts): # Find OSD pods on this host pod_osd_ids = set() - pods = self._k8s.list_namespaced_pod(self._rook_env.namespace, + pods = self.k8s.list_namespaced_pod(self._rook_env.namespace, label_selector="rook_cluster={},app=rook-ceph-osd".format(self._rook_env.cluster_name), field_selector="spec.nodeName={0}".format( drive_group.hosts(all_hosts)[0] diff --git a/src/pybind/mgr/rook/rook_cluster.py b/src/pybind/mgr/rook/rook_cluster.py index 4d4c3ed76e6d4..97fd2ddc43058 100644 --- a/src/pybind/mgr/rook/rook_cluster.py +++ b/src/pybind/mgr/rook/rook_cluster.py @@ -19,12 +19,18 @@ from urllib3.exceptions import ProtocolError from mgr_util import merge_dicts +try: + from typing import Optional +except ImportError: + pass # just for type annotations + try: from kubernetes.client.rest import ApiException from kubernetes.client import V1ListMeta, CoreV1Api, V1Pod from kubernetes import watch except ImportError: - class ApiException(Exception): pass + class ApiException(Exception): # type: ignore + status = 0 import orchestrator @@ -85,7 +91,7 @@ class KubernetesResource(object): # ``_items`` is accessed by different threads. I assume assignment is atomic. self._items = dict() - self.thread = None # type: threading.Thread + self.thread = None # type: Optional[threading.Thread] self.exception = None if not _urllib3_supports_read_chunked: logging.info('urllib3 is too old. Fallback to full fetches') @@ -115,7 +121,7 @@ class KubernetesResource(object): if _urllib3_supports_read_chunked: # Start a thread which will use the kubernetes watch client against a resource log.debug("Attaching resource watcher for k8s {}".format(self.api_func)) - self.thread = self._watch(resource_version) # type: threading.Thread + self.thread = self._watch(resource_version) return self._items.values() @@ -376,7 +382,7 @@ class RookCluster(object): } if spec.namespace: - rook_nfsgw["spec"]["rados"]["namespace"] = spec.namespace + rook_nfsgw["spec"]["rados"]["namespace"] = spec.namespace # type: ignore with self.ignore_409("NFS cluster '{0}' already exists".format(spec.name)): self.rook_api_post("cephnfses/", body=rook_nfsgw) @@ -525,7 +531,7 @@ class RookCluster(object): if directories: pd["directories"] = [{'path': p} for p in directories] - patch.append({ "op": "add", "path": "/spec/storage/nodes/-", "value": pd }) + patch.append({ "op": "add", "path": "/spec/storage/nodes/-", "value": pd }) # type: ignore else: # Extend existing node node_idx = None @@ -544,7 +550,7 @@ class RookCluster(object): patch.append({ "op": "add", "path": "/spec/storage/nodes/{0}/devices/-".format(node_idx), - "value": {'name': n} + "value": {'name': n} # type: ignore }) new_dirs = list(set(directories) - set(current_node['directories'])) @@ -552,7 +558,7 @@ class RookCluster(object): patch.append({ "op": "add", "path": "/spec/storage/nodes/{0}/directories/-".format(node_idx), - "value": {'path': p} + "value": {'path': p} # type: ignore }) if len(patch) == 0: diff --git a/src/pybind/mgr/tox.ini b/src/pybind/mgr/tox.ini index 15ea35bcaa8f8..31a661a8f88d4 100644 --- a/src/pybind/mgr/tox.ini +++ b/src/pybind/mgr/tox.ini @@ -12,4 +12,4 @@ basepython = python3 deps = -r requirements.txt mypy -commands = mypy --config-file=../../mypy.ini orchestrator.py ssh/module.py \ No newline at end of file +commands = mypy --config-file=../../mypy.ini orchestrator.py ssh/module.py rook/module.py \ No newline at end of file -- 2.47.3