From e01bff16b21dc47d0843a661b546df7a39a4805d 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(ClientWithArguments): make pretty parameter customizable --- .github/workflows/python-test.yml | 4 +-- CHANGELOG.md | 4 +++ VERSION | 2 +- .../kubernetes_client_additional_arguments.py | 32 +++++++++++-------- src/wiremind_kubernetes/kubernetes_helper.py | 16 ++++++---- ...rnetes_client_additional_arguments_test.py | 18 +++++++++++ 6 files changed, 52 insertions(+), 24 deletions(-) diff --git a/.github/workflows/python-test.yml b/.github/workflows/python-test.yml index 48abb50..3703a9a 100644 --- a/.github/workflows/python-test.yml +++ b/.github/workflows/python-test.yml @@ -18,7 +18,7 @@ jobs: strategy: fail-fast: false matrix: - python-version: [3.7, 3.8, 3.9, 3.10.0] + python-version: ['3.7', '3.8', '3.9', '3.10'] steps: - uses: actions/checkout@v2 @@ -26,7 +26,7 @@ jobs: - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v2 with: - python-version: ${{ matrix.python-version }} + python-version: "${{ matrix.python-version }}" - name: Install dependencies run: | diff --git a/CHANGELOG.md b/CHANGELOG.md index e57d3b7..cd2ee6b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # wiremind-kubernetes +## v7.1.0 (2022-12-27) +### Feature +- ClientWithArguments: make pretty parameter customizable + ## v7.0.0 (2022-09-27) ### BREAKING CHANGE - stop_pods: neutralize the HPA as `HPAScaleToZero` may be in use (HPA may scale up the Deployment even if replicas=0), a more straightforward solution will diff --git a/VERSION b/VERSION index 4122521..a3fcc71 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -7.0.0 \ 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 53734c3..a6fadd4 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, dry_run: bool = False): + def __init__(self, client, 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 some functions + # do not support this arg like: delete_cluster_custom_object + 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, **kwargs): class CoreV1ApiWithArguments(ClientWithArguments): - def __init__(self, *args, dry_run: bool = False, **kwargs): - super().__init__(client=kubernetes.client.CoreV1Api, dry_run=dry_run) + def __init__(self, *args, dry_run: bool = False, pretty: bool = True, **kwargs): + super().__init__(client=kubernetes.client.CoreV1Api, dry_run=dry_run, pretty=pretty) class AppV1ApiWithArguments(ClientWithArguments): - def __init__(self, *args, dry_run: bool = False, **kwargs): - super().__init__(client=kubernetes.client.AppsV1Api, dry_run=dry_run) + def __init__(self, *args, dry_run: bool = False, pretty: bool = True, **kwargs): + super().__init__(client=kubernetes.client.AppsV1Api, dry_run=dry_run, pretty=pretty) class BatchV1ApiWithArguments(ClientWithArguments): - def __init__(self, *args, dry_run: bool = False, **kwargs): - super().__init__(client=kubernetes.client.BatchV1Api, dry_run=dry_run) + def __init__(self, *args, dry_run: bool = False, pretty: bool = True, **kwargs): + super().__init__(client=kubernetes.client.BatchV1Api, dry_run=dry_run, pretty=pretty) class AutoscalingV1ApiWithArguments(ClientWithArguments): - def __init__(self, *args, dry_run: bool = False, **kwargs): - super().__init__(client=kubernetes.client.AutoscalingV1Api, dry_run=dry_run) + def __init__(self, *args, dry_run: bool = False, pretty: bool = True, **kwargs): + super().__init__(client=kubernetes.client.AutoscalingV1Api, dry_run=dry_run, pretty=pretty) class CustomObjectsApiWithArguments(ClientWithArguments): - def __init__(self, *args, dry_run: bool = False, **kwargs): - super().__init__(client=kubernetes.client.CustomObjectsApi, dry_run=dry_run) + def __init__(self, *args, dry_run: bool = False, pretty: bool = True, **kwargs): + super().__init__(client=kubernetes.client.CustomObjectsApi, dry_run=dry_run, pretty=pretty) class RbacAuthorizationV1ApiWithArguments(ClientWithArguments): - def __init__(self, *args, dry_run: bool = False, **kwargs): - super().__init__(client=kubernetes.client.RbacAuthorizationV1Api, dry_run=dry_run) + def __init__(self, *args, dry_run: bool = False, pretty: bool = True, **kwargs): + 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 c801054..e2b3019 100644 --- a/src/wiremind_kubernetes/kubernetes_helper.py +++ b/src/wiremind_kubernetes/kubernetes_helper.py @@ -1,7 +1,7 @@ import logging import pprint import time -from typing import Any, Dict, List, Optional, Union, Generator +from typing import Any, Dict, Generator, List, Optional, Union import kubernetes @@ -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 6309c46..fc7965c 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 @@ -17,3 +17,21 @@ def test_kubernetes_client_additional_arguments_core_v1_api(mocker): 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): + """ + 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")