From 348bd12b83cde4cfeaf51c20b106dd66fac6fb34 Mon Sep 17 00:00:00 2001 From: Maxime Leroy <19607336+maxime1907@users.noreply.github.com> Date: Tue, 27 Dec 2022 11:11:53 +0100 Subject: [PATCH] fix: make pretty parameter customizable for ClientWithArguments and KubernetesHelper --- CHANGELOG.md | 4 +++ VERSION | 2 +- .../kubernetes_client_additional_arguments.py | 32 +++++++++++-------- src/wiremind_kubernetes/kubernetes_helper.py | 14 ++++---- ...rnetes_client_additional_arguments_test.py | 18 +++++++++++ 5 files changed, 49 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c6c677..78333d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # wiremind-kubernetes +## v7.1.0 (2022-12-27) +### Feature +- ClientWithArguments, KubernetesHelper: make pretty parameter customizable + ## v7.0.1 (2022-12-27) ### Fix - setup.cfg correct mypy and flake8 config diff --git a/VERSION b/VERSION index 73a86b1..a3fcc71 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -7.0.1 \ No newline at end of file +7.1.0 diff --git a/src/wiremind_kubernetes/kubernetes_client_additional_arguments.py b/src/wiremind_kubernetes/kubernetes_client_additional_arguments.py index 847fd1f..11296b6 100644 --- a/src/wiremind_kubernetes/kubernetes_client_additional_arguments.py +++ b/src/wiremind_kubernetes/kubernetes_client_additional_arguments.py @@ -10,9 +10,13 @@ class ClientWithArguments: Currently add dry_run support for write functions and pretty to all. """ - def __init__(self, client: Any, dry_run: bool = False): + def __init__(self, client: Any, dry_run: bool = False, pretty: bool = True): self.client = client() # like kubernetes.client.CoreV1Api - self.read_additional_arguments: Dict[str, Any] = dict(pretty=True) + self.read_additional_arguments: Dict[str, Any] = {} + # Only add it when its true because we set pretty client wide, + # read_cluster_custom_object which accepts it will not have it set, but it's ok for now. + if pretty: + self.read_additional_arguments["pretty"] = pretty # Every request, either read or write, will have those arguments added self.additional_arguments: Dict[str, Any] = self.read_additional_arguments.copy() if dry_run: @@ -42,30 +46,30 @@ def fn(*args: Any, **kwargs: Any) -> Any: class CoreV1ApiWithArguments(ClientWithArguments): - def __init__(self, *args: Any, dry_run: bool = False, **kwargs: Any) -> None: - super().__init__(client=kubernetes.client.CoreV1Api, dry_run=dry_run) + def __init__(self, *args: Any, dry_run: bool = False, pretty: bool = False, **kwargs: Any) -> None: + super().__init__(client=kubernetes.client.CoreV1Api, dry_run=dry_run, pretty=pretty) class AppV1ApiWithArguments(ClientWithArguments): - def __init__(self, *args: Any, dry_run: bool = False, **kwargs: Any) -> None: - super().__init__(client=kubernetes.client.AppsV1Api, dry_run=dry_run) + def __init__(self, *args: Any, dry_run: bool = False, pretty: bool = False, **kwargs: Any) -> None: + super().__init__(client=kubernetes.client.AppsV1Api, dry_run=dry_run, pretty=pretty) class BatchV1ApiWithArguments(ClientWithArguments): - def __init__(self, *args: Any, dry_run: bool = False, **kwargs: Any) -> None: - super().__init__(client=kubernetes.client.BatchV1Api, dry_run=dry_run) + def __init__(self, *args: Any, dry_run: bool = False, pretty: bool = False, **kwargs: Any) -> None: + super().__init__(client=kubernetes.client.BatchV1Api, dry_run=dry_run, pretty=pretty) class AutoscalingV1ApiWithArguments(ClientWithArguments): - def __init__(self, *args: Any, dry_run: bool = False, **kwargs: Any) -> None: - super().__init__(client=kubernetes.client.AutoscalingV1Api, dry_run=dry_run) + def __init__(self, *args: Any, dry_run: bool = False, pretty: bool = False, **kwargs: Any) -> None: + super().__init__(client=kubernetes.client.AutoscalingV1Api, dry_run=dry_run, pretty=pretty) class CustomObjectsApiWithArguments(ClientWithArguments): - def __init__(self, *args: Any, dry_run: bool = False, **kwargs: Any) -> None: - super().__init__(client=kubernetes.client.CustomObjectsApi, dry_run=dry_run) + def __init__(self, *args: Any, dry_run: bool = False, pretty: bool = False, **kwargs: Any) -> None: + super().__init__(client=kubernetes.client.CustomObjectsApi, dry_run=dry_run, pretty=pretty) class RbacAuthorizationV1ApiWithArguments(ClientWithArguments): - def __init__(self, *args: Any, dry_run: bool = False, **kwargs: Any) -> None: - super().__init__(client=kubernetes.client.RbacAuthorizationV1Api, dry_run=dry_run) + def __init__(self, *args: Any, dry_run: bool = False, pretty: bool = False, **kwargs: Any) -> None: + super().__init__(client=kubernetes.client.RbacAuthorizationV1Api, dry_run=dry_run, pretty=pretty) diff --git a/src/wiremind_kubernetes/kubernetes_helper.py b/src/wiremind_kubernetes/kubernetes_helper.py index 66f9736..47c8109 100644 --- a/src/wiremind_kubernetes/kubernetes_helper.py +++ b/src/wiremind_kubernetes/kubernetes_helper.py @@ -36,6 +36,7 @@ def __init__( dry_run: bool = False, should_load_kubernetes_config: bool = True, context: Optional[str] = None, + pretty: bool = True, ): """ :param use_kubeconfig: @@ -50,20 +51,21 @@ def __init__( """ if should_load_kubernetes_config: load_kubernetes_config(use_kubeconfig=use_kubeconfig, context=context) - self.client_corev1_api: kubernetes.client.CoreV1Api = CoreV1ApiWithArguments(dry_run=dry_run) - self.client_appsv1_api: kubernetes.client.AppsV1Api = AppV1ApiWithArguments(dry_run=dry_run) - self.client_batchv1_api: kubernetes.client.BatchV1Api = BatchV1ApiWithArguments(dry_run=dry_run) + self.client_corev1_api: kubernetes.client.CoreV1Api = CoreV1ApiWithArguments(dry_run=dry_run, pretty=pretty) + self.client_appsv1_api: kubernetes.client.AppsV1Api = AppV1ApiWithArguments(dry_run=dry_run, pretty=pretty) + self.client_batchv1_api: kubernetes.client.BatchV1Api = BatchV1ApiWithArguments(dry_run=dry_run, pretty=pretty) self.client_autoscalingv1_api: kubernetes.client.AutoscalingV1Api = AutoscalingV1ApiWithArguments( - dry_run=dry_run + dry_run=dry_run, pretty=pretty ) self.client_custom_objects_api: kubernetes.client.CustomObjectsApi = CustomObjectsApiWithArguments( - dry_run=dry_run + dry_run=dry_run, pretty=pretty ) self.client_rbac_authorization_v1_api: kubernetes.client.RbacAuthorizationV1Api = ( - RbacAuthorizationV1ApiWithArguments(dry_run=dry_run) + RbacAuthorizationV1ApiWithArguments(dry_run=dry_run, pretty=pretty) ) self.dry_run: bool = dry_run + self.pretty: bool = pretty def _get_namespace_from_kube() -> str: diff --git a/src/wiremind_kubernetes/tests/unit_tests/kubernetes_client_additional_arguments_test.py b/src/wiremind_kubernetes/tests/unit_tests/kubernetes_client_additional_arguments_test.py index e0ac4da..7586add 100644 --- a/src/wiremind_kubernetes/tests/unit_tests/kubernetes_client_additional_arguments_test.py +++ b/src/wiremind_kubernetes/tests/unit_tests/kubernetes_client_additional_arguments_test.py @@ -19,3 +19,21 @@ def test_kubernetes_client_additional_arguments_core_v1_api(mocker: MockerFixtur kubernetes_helper.client_corev1_api.create_namespaced_pod("foo", "bar") mocked_create_namespaced_pod.assert_called_once_with("foo", "bar", pretty=True, dry_run="All") + + +def test_kubernetes_client_additional_arguments_disabled_core_v1_api(mocker: MockerFixture) -> None: + """ + Test that we do not add args to each function call of kubernetes client + """ + mocked_read_namespaced_pod = mocker.patch("kubernetes.client.CoreV1Api.read_namespaced_pod") + mocked_create_namespaced_pod = mocker.patch("kubernetes.client.CoreV1Api.create_namespaced_pod") + + kubernetes_helper = wiremind_kubernetes.kubernetes_helper.KubernetesHelper( + dry_run=True, pretty=False, should_load_kubernetes_config=False + ) + + kubernetes_helper.client_corev1_api.read_namespaced_pod("foo", "bar") + mocked_read_namespaced_pod.assert_called_once_with("foo", "bar") + + kubernetes_helper.client_corev1_api.create_namespaced_pod("foo", "bar") + mocked_create_namespaced_pod.assert_called_once_with("foo", "bar", dry_run="All")