From 9f0ea55c3e7e173c92e57d52c1974dfd4f84ed20 Mon Sep 17 00:00:00 2001 From: Bikalpa Dhakal Date: Fri, 15 Dec 2023 12:48:52 +0545 Subject: [PATCH] Format with `tox -e fmt` --- spark8t/backend/__init__.py | 8 +-- spark8t/backend/interface.py | 8 +-- spark8t/backend/kubectl.py | 13 +--- spark8t/backend/lightkube.py | 12 +--- spark8t/cli/params.py | 2 +- spark8t/cli/service_account_registry.py | 3 +- spark8t/cli/spark_shell.py | 2 +- spark8t/domain.py | 1 + spark8t/registry/__init__.py | 6 +- spark8t/registry/in_memory_registry.py | 6 +- spark8t/registry/interface.py | 9 +-- spark8t/registry/k8s_registry.py | 9 +-- spark8t/spark_interface.py | 17 ++--- spark8t/utils.py | 2 - tests/integration/conftest.py | 2 +- tests/integration/test_kube_interface.py | 3 +- tests/integration/test_service_accounts.py | 84 +++++++++++----------- tests/unittest/test_domain.py | 2 +- tests/unittest/test_logging.py | 2 +- tests/unittest/test_services.py | 5 +- 20 files changed, 78 insertions(+), 118 deletions(-) diff --git a/spark8t/backend/__init__.py b/spark8t/backend/__init__.py index 73b667a..67a093f 100644 --- a/spark8t/backend/__init__.py +++ b/spark8t/backend/__init__.py @@ -1,9 +1,5 @@ from .interface import AbstractKubeInterface -from .lightkube import LightKube from .kubectl import KubeInterface +from .lightkube import LightKube -__all__ = [ - "AbstractKubeInterface", - "LightKube", - "KubeInterface" -] \ No newline at end of file +__all__ = ["AbstractKubeInterface", "LightKube", "KubeInterface"] diff --git a/spark8t/backend/interface.py b/spark8t/backend/interface.py index c32a87e..b810090 100644 --- a/spark8t/backend/interface.py +++ b/spark8t/backend/interface.py @@ -6,13 +6,9 @@ import yaml -from spark8t.domain import ( - KubernetesResourceType -) +from spark8t.domain import KubernetesResourceType from spark8t.exceptions import AccountNotFound -from spark8t.utils import ( - WithLogging, -) +from spark8t.utils import WithLogging class AbstractKubeInterface(WithLogging, metaclass=ABCMeta): diff --git a/spark8t/backend/kubectl.py b/spark8t/backend/kubectl.py index d876404..79ea8a2 100644 --- a/spark8t/backend/kubectl.py +++ b/spark8t/backend/kubectl.py @@ -4,16 +4,10 @@ import subprocess from typing import Any, Dict, List, Optional, Union -from spark8t.domain import ( - KubernetesResourceType, -) +from spark8t.domain import KubernetesResourceType from spark8t.exceptions import AccountNotFound, K8sResourceNotFound -from spark8t.literals import MANAGED_BY_LABELNAME, PRIMARY_LABELNAME, SPARK8S_LABEL -from spark8t.utils import ( - execute_command_output, - listify, - parse_yaml_shell_output, -) +from spark8t.utils import execute_command_output, listify, parse_yaml_shell_output + from .interface import AbstractKubeInterface @@ -338,4 +332,3 @@ def select_by_master(self, master: str): if self.context_name in contexts_for_api_server else self.with_context(contexts_for_api_server[0]) ) - diff --git a/spark8t/backend/lightkube.py b/spark8t/backend/lightkube.py index 5c5a742..2bbf01d 100644 --- a/spark8t/backend/lightkube.py +++ b/spark8t/backend/lightkube.py @@ -15,15 +15,10 @@ from lightkube.resources.rbac_authorization_v1 import Role, RoleBinding from lightkube.types import PatchType -from spark8t.domain import ( - Defaults, - KubernetesResourceType, -) +from spark8t.domain import Defaults, KubernetesResourceType from spark8t.exceptions import K8sResourceNotFound -from spark8t.utils import ( - filter_none, - PropertyFile -) +from spark8t.utils import PropertyFile, filter_none + from .interface import AbstractKubeInterface @@ -403,4 +398,3 @@ def exists( if "not found" in e.status.message: return False raise e - diff --git a/spark8t/cli/params.py b/spark8t/cli/params.py index 7f43008..b50cfbc 100644 --- a/spark8t/cli/params.py +++ b/spark8t/cli/params.py @@ -2,8 +2,8 @@ from argparse import ArgumentParser, Namespace from typing import Callable, List, Optional -from spark8t.cli import defaults from spark8t.backend import AbstractKubeInterface, KubeInterface, LightKube +from spark8t.cli import defaults from spark8t.utils import DEFAULT_LOGGING_FILE, config_from_file, environ diff --git a/spark8t/cli/service_account_registry.py b/spark8t/cli/service_account_registry.py index 1a80c4d..0edf24a 100644 --- a/spark8t/cli/service_account_registry.py +++ b/spark8t/cli/service_account_registry.py @@ -15,8 +15,7 @@ from spark8t.domain import ServiceAccount from spark8t.exceptions import AccountNotFound, PrimaryAccountNotFound from spark8t.registry import K8sServiceAccountRegistry -from spark8t.utils import setup_logging, parse_conf_overrides -from spark8t.utils import PropertyFile +from spark8t.utils import PropertyFile, parse_conf_overrides, setup_logging def build_service_account_from_args(args, registry) -> ServiceAccount: diff --git a/spark8t/cli/spark_shell.py b/spark8t/cli/spark_shell.py index a54aca9..8dd3d67 100644 --- a/spark8t/cli/spark_shell.py +++ b/spark8t/cli/spark_shell.py @@ -16,8 +16,8 @@ ) from spark8t.domain import ServiceAccount from spark8t.exceptions import AccountNotFound, PrimaryAccountNotFound -from spark8t.spark_interface import SparkInterface from spark8t.registry import K8sServiceAccountRegistry +from spark8t.spark_interface import SparkInterface from spark8t.utils import setup_logging diff --git a/spark8t/domain.py b/spark8t/domain.py index b1d4387..e49d78a 100644 --- a/spark8t/domain.py +++ b/spark8t/domain.py @@ -5,6 +5,7 @@ from spark8t.utils import PropertyFile + class Defaults: """Class containing all relevant defaults for the application.""" diff --git a/spark8t/registry/__init__.py b/spark8t/registry/__init__.py index fea0d51..f531c8d 100644 --- a/spark8t/registry/__init__.py +++ b/spark8t/registry/__init__.py @@ -1,9 +1,9 @@ +from .in_memory_registry import InMemoryAccountRegistry from .interface import AbstractServiceAccountRegistry from .k8s_registry import K8sServiceAccountRegistry -from .in_memory_registry import InMemoryAccountRegistry __allo__ = [ "AbstractServiceAccountRegistry", "K8sServiceAccountRegistry", - "InMemoryAccountRegistry" -] \ No newline at end of file + "InMemoryAccountRegistry", +] diff --git a/spark8t/registry/in_memory_registry.py b/spark8t/registry/in_memory_registry.py index 212accc..ae042e0 100644 --- a/spark8t/registry/in_memory_registry.py +++ b/spark8t/registry/in_memory_registry.py @@ -2,13 +2,12 @@ from typing import Dict, List, Optional -from spark8t.domain import ( - ServiceAccount, -) +from spark8t.domain import ServiceAccount from spark8t.exceptions import AccountNotFound from spark8t.registry import AbstractServiceAccountRegistry from spark8t.utils import PropertyFile + class InMemoryAccountRegistry(AbstractServiceAccountRegistry): def __init__(self, cache: Dict[str, ServiceAccount]): self.cache = cache @@ -99,4 +98,3 @@ def set_configurations(self, account_id: str, configurations: PropertyFile) -> s def get(self, account_id: str) -> Optional[ServiceAccount]: return self.cache.get(account_id) - diff --git a/spark8t/registry/interface.py b/spark8t/registry/interface.py index 6c691b9..e28ab54 100644 --- a/spark8t/registry/interface.py +++ b/spark8t/registry/interface.py @@ -3,12 +3,8 @@ from abc import ABC, abstractmethod from typing import List, Optional -from spark8t.domain import ( - ServiceAccount, -) -from spark8t.utils import ( - WithLogging, PropertyFile -) +from spark8t.domain import ServiceAccount +from spark8t.utils import PropertyFile, WithLogging class AbstractServiceAccountRegistry(WithLogging, ABC): @@ -88,4 +84,3 @@ def get(self, account_id: str) -> Optional[ServiceAccount]: account_id: account id to be used for retrieving the service account. """ pass - diff --git a/spark8t/registry/k8s_registry.py b/spark8t/registry/k8s_registry.py index 849818a..1ba2cfa 100644 --- a/spark8t/registry/k8s_registry.py +++ b/spark8t/registry/k8s_registry.py @@ -2,10 +2,8 @@ from typing import Any, Dict, List, Optional -from spark8t.domain import ( - KubernetesResourceType, - ServiceAccount, -) +from spark8t.backend import AbstractKubeInterface +from spark8t.domain import KubernetesResourceType, ServiceAccount from spark8t.exceptions import AccountNotFound, K8sResourceNotFound from spark8t.literals import MANAGED_BY_LABELNAME, PRIMARY_LABELNAME, SPARK8S_LABEL from spark8t.utils import ( @@ -15,7 +13,7 @@ ) from .interface import AbstractServiceAccountRegistry -from spark8t.backend import AbstractKubeInterface + class K8sServiceAccountRegistry(AbstractServiceAccountRegistry): """Class implementing a ServiceAccountRegistry, based on K8s.""" @@ -289,4 +287,3 @@ def get(self, account_id: str) -> Optional[ServiceAccount]: except K8sResourceNotFound: return None return self._build_service_account_from_raw(service_account_raw["metadata"]) - diff --git a/spark8t/spark_interface.py b/spark8t/spark_interface.py index c265413..3a94e22 100644 --- a/spark8t/spark_interface.py +++ b/spark8t/spark_interface.py @@ -4,20 +4,11 @@ import os import socket from enum import Enum -from typing import Any, Dict, List, Optional - - -from spark8t.domain import ( - Defaults, - ServiceAccount, -) -from spark8t.utils import ( - WithLogging, - PropertyFile, - environ, - umask_named_temporary_file, -) +from typing import Any, List, Optional + from spark8t.backend import AbstractKubeInterface +from spark8t.domain import Defaults, ServiceAccount +from spark8t.utils import PropertyFile, WithLogging, environ, umask_named_temporary_file class SparkDeployMode(str, Enum): diff --git a/spark8t/utils.py b/spark8t/utils.py index e5dce73..0504d3a 100644 --- a/spark8t/utils.py +++ b/spark8t/utils.py @@ -66,7 +66,6 @@ class LevelsDict(TypedDict): ) - def config_from_json(path_to_file: str = DEFAULT_LOGGING_FILE) -> None: """ Configure logger from json @@ -568,4 +567,3 @@ def parse_conf_overrides( "Please check input arguments and try again." ) return PropertyFile(conf_overrides) - diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 84f28a1..8dc8706 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -3,8 +3,8 @@ import pytest from lightkube.resources.core_v1 import Namespace -from spark8t.domain import Defaults from spark8t.backend import KubeInterface, LightKube +from spark8t.domain import Defaults from spark8t.registry import K8sServiceAccountRegistry integration_test_flag = bool(int(os.environ.get("IE_TEST", "0"))) diff --git a/tests/integration/test_kube_interface.py b/tests/integration/test_kube_interface.py index fb9ad70..2892d79 100644 --- a/tests/integration/test_kube_interface.py +++ b/tests/integration/test_kube_interface.py @@ -3,8 +3,7 @@ import pytest from spark8t.domain import KubernetesResourceType -from spark8t.utils import PropertyFile -from spark8t.utils import umask_named_temporary_file +from spark8t.utils import PropertyFile, umask_named_temporary_file @pytest.mark.parametrize( diff --git a/tests/integration/test_service_accounts.py b/tests/integration/test_service_accounts.py index 1e9a205..6b8fa89 100644 --- a/tests/integration/test_service_accounts.py +++ b/tests/integration/test_service_accounts.py @@ -1,37 +1,24 @@ import json import subprocess -import pytest import uuid -from spark8t.literals import SPARK8S_LABEL, MANAGED_BY_LABELNAME + +import pytest + +from spark8t.literals import MANAGED_BY_LABELNAME, SPARK8S_LABEL @pytest.fixture def namespace(): namespace_name = str(uuid.uuid4()) - create_command = [ - "kubectl", - "create", - "namespace", - namespace_name - ] + create_command = ["kubectl", "create", "namespace", namespace_name] subprocess.run(create_command, check=True) yield namespace_name - destroy_command = [ - "kubectl", - "delete", - "namespace", - namespace_name - ] + destroy_command = ["kubectl", "delete", "namespace", namespace_name] subprocess.run(destroy_command, check=True) def run_service_account_registry(*args): - command = [ - "python3", - "-m", - "spark8t.cli.service_account_registry", - *args - ] + command = ["python3", "-m", "spark8t.cli.service_account_registry", *args] output = subprocess.run(command, check=True, capture_output=True) return output.stdout, output.stderr @@ -42,17 +29,19 @@ def run_service_account_registry(*args): "services": ["create", "get", "list", "watch", "delete"], } + def parameterize(permissions): parameters = [] for resource, actions in permissions.items(): parameters.extend([(action, resource) for action in actions]) return parameters + @pytest.mark.parametrize("backend", ["kubectl", "lightkube"]) @pytest.mark.parametrize("action, resource", parameterize(ALLOWED_PERMISSIONS)) def test_create_service_account(namespace, backend, action, resource): """Test creation of service account using the CLI. - + Verify that the serviceaccount, role and rolebinding resources are created with appropriate tags applied to them. Also verify that the RBAC permissions for the created serviceaccount. @@ -63,23 +52,23 @@ def test_create_service_account(namespace, backend, action, resource): role_binding_name = f"{username}-role-binding" # Create the service account - run_service_account_registry("create", "--username", username, "--namespace", namespace, "--backend", backend) - + run_service_account_registry( + "create", "--username", username, "--namespace", namespace, "--backend", backend + ) + # Check if service account was created with appropriate labels service_account_result = subprocess.run( ["kubectl", "get", "serviceaccount", username, "-n", namespace, "-o", "json"], check=True, capture_output=True, - text=True + text=True, ) assert service_account_result.returncode == 0 service_account = json.loads(service_account_result.stdout) assert service_account is not None actual_labels = service_account["metadata"]["labels"] - expected_labels = { - MANAGED_BY_LABELNAME: SPARK8S_LABEL - } + expected_labels = {MANAGED_BY_LABELNAME: SPARK8S_LABEL} assert actual_labels == expected_labels # Check if a role was created with appropriate labels @@ -87,42 +76,57 @@ def test_create_service_account(namespace, backend, action, resource): ["kubectl", "get", "role", role_name, "-n", namespace, "-o", "json"], check=True, capture_output=True, - text=True + text=True, ) assert role_result.returncode == 0 role = json.loads(role_result.stdout) assert role is not None actual_labels = role["metadata"]["labels"] - expected_labels = { - MANAGED_BY_LABELNAME: SPARK8S_LABEL - } + expected_labels = {MANAGED_BY_LABELNAME: SPARK8S_LABEL} assert actual_labels == expected_labels # Check if a role binding was created with appropriate labels role_binding_result = subprocess.run( - ["kubectl", "get", "rolebinding", role_binding_name, "-n", namespace, "-o", "json"], + [ + "kubectl", + "get", + "rolebinding", + role_binding_name, + "-n", + namespace, + "-o", + "json", + ], check=True, capture_output=True, - text=True + text=True, ) assert role_binding_result.returncode == 0 role_binding = json.loads(role_binding_result.stdout) assert role_binding is not None actual_labels = role_binding["metadata"]["labels"] - expected_labels = { - MANAGED_BY_LABELNAME: SPARK8S_LABEL - } + expected_labels = {MANAGED_BY_LABELNAME: SPARK8S_LABEL} assert actual_labels == expected_labels # Check for RBAC permissions sa_identifier = f"system:serviceaccount:{namespace}:{username}" rbac_check = subprocess.run( - ["kubectl", "auth", "can-i", action, resource, "--namespace", namespace, "--as", sa_identifier], + [ + "kubectl", + "auth", + "can-i", + action, + resource, + "--namespace", + namespace, + "--as", + sa_identifier, + ], check=True, capture_output=True, - text=True - ) + text=True, + ) assert rbac_check.returncode == 0 - assert rbac_check.stdout.strip() == "yes" + assert rbac_check.stdout.strip() == "yes" diff --git a/tests/unittest/test_domain.py b/tests/unittest/test_domain.py index 3703e7b..431210b 100644 --- a/tests/unittest/test_domain.py +++ b/tests/unittest/test_domain.py @@ -4,7 +4,7 @@ from spark8t.domain import Defaults, ServiceAccount from spark8t.registry import InMemoryAccountRegistry -from spark8t.utils import umask_named_temporary_file, PropertyFile +from spark8t.utils import PropertyFile, umask_named_temporary_file def test_defaults(): diff --git a/tests/unittest/test_logging.py b/tests/unittest/test_logging.py index 2ff3464..abee07a 100644 --- a/tests/unittest/test_logging.py +++ b/tests/unittest/test_logging.py @@ -3,7 +3,7 @@ import pytest -from spark8t.utils import setup_logging, PropertyFile +from spark8t.utils import PropertyFile, setup_logging TEST_LOGGING_FILE = os.path.join( os.path.dirname(__file__), "..", "resources", "logging.yaml" diff --git a/tests/unittest/test_services.py b/tests/unittest/test_services.py index 2b7f1d9..6c7a4b2 100644 --- a/tests/unittest/test_services.py +++ b/tests/unittest/test_services.py @@ -13,13 +13,12 @@ from lightkube.types import PatchType from OpenSSL import crypto +from spark8t.backend import KubeInterface, LightKube from spark8t.cli import defaults from spark8t.domain import KubernetesResourceType, ServiceAccount from spark8t.literals import MANAGED_BY_LABELNAME, PRIMARY_LABELNAME, SPARK8S_LABEL from spark8t.registry import K8sServiceAccountRegistry -from spark8t.backend import KubeInterface, LightKube -from spark8t.utils import parse_conf_overrides, PropertyFile - +from spark8t.utils import PropertyFile, parse_conf_overrides #################################################################################################### # Helpers