-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Externalize
ModuleHandler
subclasses
Signed-off-by: Tobias Wolf <[email protected]>
- Loading branch information
1 parent
f693cc5
commit 4dcc214
Showing
13 changed files
with
225 additions
and
186 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import json | ||
import rados | ||
from typing import Any, Dict, List | ||
from .exception import ModuleException | ||
|
||
|
||
class Ceph: | ||
def __init__(self, config: Dict[str, Any]): | ||
try: | ||
self.__ceph = rados.Rados( | ||
conffile=config["config"], conf={"keyring": config["keyring"]} | ||
) | ||
self.__ceph.connect() | ||
except rados.ObjectNotFound as err: | ||
raise ModuleException(f"Could not connect to ceph: {err}") | ||
|
||
def __getattr__(self, name: str) -> Any: | ||
return getattr(self.__ceph, name) | ||
|
||
def mon_command(self, command: str, **kwargs: str) -> Dict[str, Any] | List[Any]: | ||
cmd = {"prefix": command, "format": "json"} | ||
cmd.update(**kwargs) | ||
result = self.__ceph.mon_command(json.dumps(cmd), b"") | ||
if result[0] != 0: | ||
raise ModuleException(f"Ceph did return an error: {result}") | ||
data = json.loads(result[1]) | ||
assert isinstance(data, dict) or isinstance(data, list) | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
|
||
class ModuleException(Exception): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import kubernetes | ||
from typing import Any, Callable, Dict, Optional | ||
|
||
|
||
class K8s: | ||
def __init__(self, config: Dict[str, Any]): | ||
k8s_config = kubernetes.config.load_kube_config(config_file=config["config"]) | ||
self.__client = kubernetes.client.ApiClient(k8s_config) | ||
self.__dynamic_client: Optional[kubernetes.dynamic.DynamicClient] = None | ||
|
||
@property | ||
def core_v1_api(self) -> kubernetes.client.CoreV1Api: | ||
return kubernetes.client.CoreV1Api(self.__client) | ||
|
||
@property | ||
def apps_v1_api(self) -> kubernetes.client.AppsV1Api: | ||
return kubernetes.client.AppsV1Api(self.__client) | ||
|
||
@property | ||
def node_v1_api(self) -> kubernetes.client.NodeV1Api: | ||
return kubernetes.client.NodeV1Api(self.__client) | ||
|
||
@property | ||
def custom_objects_api(self) -> kubernetes.client.CustomObjectsApi: | ||
return kubernetes.client.CustomObjectsApi(self.__client) | ||
|
||
@property | ||
def dynamic_client(self) -> kubernetes.dynamic.DynamicClient: | ||
if not self.__dynamic_client: | ||
self.__dynamic_client = kubernetes.dynamic.DynamicClient(self.__client) | ||
return self.__dynamic_client | ||
|
||
def crd_api( | ||
self, api_version: str, kind: str | ||
) -> kubernetes.dynamic.resource.Resource: | ||
return self.dynamic_client.resources.get(api_version=api_version, kind=kind) | ||
|
||
def crd_api_apply( | ||
self, manifest: Dict[Any, Any] | ||
) -> kubernetes.dynamic.resource.ResourceInstance: | ||
""" | ||
This applies a manifest for custom CRDs | ||
See https://github.com/kubernetes-client/python/issues/1792 for more information | ||
:param manifest: Dict of the kubernetes manifest | ||
""" | ||
api_version = manifest["apiVersion"] | ||
kind = manifest["kind"] | ||
resource_name = manifest["metadata"]["name"] | ||
namespace = manifest["metadata"]["namespace"] | ||
crd_api = self.crd_api(api_version=api_version, kind=kind) | ||
|
||
try: | ||
crd_api.get(namespace=namespace, name=resource_name) | ||
return crd_api.patch( | ||
body=manifest, content_type="application/merge-patch+json" | ||
) | ||
except kubernetes.dynamic.exceptions.NotFoundError: | ||
return crd_api.create(body=manifest, namespace=namespace) | ||
|
||
def watch_events( | ||
self, | ||
callback_func: Callable[[Any], Any], | ||
func: Callable[[Any], Any], | ||
*args: Any, | ||
**kwargs: Any, | ||
) -> Any: | ||
watcher = kubernetes.watch.Watch() | ||
|
||
stream = watcher.stream(func, *args, **kwargs) | ||
|
||
try: | ||
for event in stream: | ||
try: | ||
result = callback_func(event["object"]) | ||
except StopIteration: | ||
continue | ||
|
||
if result is not None: | ||
return result | ||
finally: | ||
watcher.stop() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.