Skip to content

Commit

Permalink
fix(ClientWithArguments): make pretty parameter customizable
Browse files Browse the repository at this point in the history
  • Loading branch information
maxime1907 committed Dec 27, 2022
1 parent 093592a commit e01bff1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ 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

- 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: |
Expand Down
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: 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
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.0.0
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, 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:
Expand Down Expand Up @@ -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)
16 changes: 9 additions & 7 deletions src/wiremind_kubernetes/kubernetes_helper.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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:
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 @@ -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")

0 comments on commit e01bff1

Please sign in to comment.