Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ClientWithArguments): make pretty parameter customizable #16

Merged
merged 1 commit into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.0.1
7.1.0
32 changes: 18 additions & 14 deletions src/wiremind_kubernetes/kubernetes_client_additional_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
14 changes: 8 additions & 6 deletions src/wiremind_kubernetes/kubernetes_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(
self,
use_kubeconfig: bool = False,
dry_run: bool = False,
pretty: bool = True,
should_load_kubernetes_config: bool = True,
context: Optional[str] = None,
):
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")