diff --git a/leverage/containers/kubectl.py b/leverage/containers/kubectl.py index a6f79bf..d8ffd39 100644 --- a/leverage/containers/kubectl.py +++ b/leverage/containers/kubectl.py @@ -4,7 +4,7 @@ from click.exceptions import Exit from docker.types import Mount -from ruamel.yaml import YAML +import ruamel.yaml from simple_term_menu import TerminalMenu from leverage import logger @@ -25,6 +25,7 @@ class KubeCtlContainer(TerraformContainer): KUBECTL_CLI_BINARY = "/usr/local/bin/kubectl" KUBECTL_CONFIG_PATH = Path(f"/home/{TerraformContainer.CONTAINER_USER}/.kube") KUBECTL_CONFIG_FILE = KUBECTL_CONFIG_PATH / Path("config") + METADATA_FILENAME = "metadata.yaml" def __init__(self, client): super().__init__(client) @@ -81,16 +82,19 @@ def _get_eks_kube_config(self) -> str: def _scan_clusters(self): """ - Scan all the subdirectories in search of "cluster" layers. + Scan all the subdirectories in search of "cluster" metadata files. """ for root, dirs, files in os.walk(self.paths.cwd): # exclude hidden directories dirs[:] = [d for d in dirs if not d[0] == "."] - if "metadata.yaml" in files: - cluster_file = Path(root) / "metadata.yaml" + for file in files: + if file != self.METADATA_FILENAME: + continue + + cluster_file = Path(root) / file try: - data = YAML().load(cluster_file) + data = ruamel.yaml.load(cluster_file) assert data["type"] == "k8s-eks-cluster" except AssertionError: continue @@ -101,6 +105,9 @@ def _scan_clusters(self): yield Path(root), data def discover(self): + """ + TODO: documentation + """ cluster_files = [(path, data) for path, data in self._scan_clusters()] if not cluster_files: raise ExitError(1, "No clusters found.") diff --git a/leverage/modules/kubectl.py b/leverage/modules/kubectl.py index 6f61a63..1910a33 100644 --- a/leverage/modules/kubectl.py +++ b/leverage/modules/kubectl.py @@ -18,7 +18,6 @@ def kubectl(context, state, args): """Run Kubectl commands in a custom containerized environment.""" state.container = KubeCtlContainer(get_docker_client()) state.container.ensure_image() - # state.container.paths.check_for_layer_location() _handle_subcommand(context=context, cli_container=state.container, args=args) diff --git a/tests/test_containers/test_kubectl.py b/tests/test_containers/test_kubectl.py index da67bb2..dbefd98 100644 --- a/tests/test_containers/test_kubectl.py +++ b/tests/test_containers/test_kubectl.py @@ -1,4 +1,5 @@ -from pathlib import Path +from pathlib import Path, PosixPath +from unittest import mock from unittest.mock import Mock, patch import pytest @@ -121,3 +122,20 @@ def test_start_shell_sso(mock_refresh, kubectl_container): # make sure we are pointing to the right AWS credentials: /tmp/ folder for SSO assert container_args["environment"]["AWS_CONFIG_FILE"] == "/home/leverage/tmp/test/config" assert container_args["environment"]["AWS_SHARED_CREDENTIALS_FILE"] == "/home/leverage/tmp/test/credentials" + + +def test_scan_clusters(kubectl_container: KubeCtlContainer): + # mock and call + with mock.patch("os.walk") as mock_walk: + with mock.patch("ruamel.yaml.load") as mock_yaml: + mock_walk.return_value = [ + ("/foo", ["bar"], ("baz",)), + ("/foo/bar", [], ("spam", "metadata.yaml")), + ] + mock_yaml.return_value = {"type": "k8s-eks-cluster"} + + first_found = next(kubectl_container._scan_clusters()) + + # compare + assert first_found[0] == PosixPath("/foo/bar/") + assert first_found[1]["type"] == "k8s-eks-cluster"