From 0af474190d20d4b579c5ee474f0664db6c3170b5 Mon Sep 17 00:00:00 2001 From: Francisco Rivera Date: Sat, 23 Nov 2024 13:54:25 -0300 Subject: [PATCH] tweaks + simplifications --- leverage/containers/kubectl.py | 18 ++++++++---------- leverage/path.py | 16 ++++++---------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/leverage/containers/kubectl.py b/leverage/containers/kubectl.py index d8ffd39..5edcd43 100644 --- a/leverage/containers/kubectl.py +++ b/leverage/containers/kubectl.py @@ -5,7 +5,7 @@ from click.exceptions import Exit from docker.types import Mount import ruamel.yaml -from simple_term_menu import TerminalMenu +import simple_term_menu from leverage import logger from leverage._utils import AwsCredsEntryPoint, ExitError, CustomEntryPoint @@ -94,7 +94,8 @@ def _scan_clusters(self): cluster_file = Path(root) / file try: - data = ruamel.yaml.load(cluster_file) + with open(cluster_file) as cluster_yaml_file: + data = ruamel.yaml.safe_load(cluster_yaml_file) assert data["type"] == "k8s-eks-cluster" except AssertionError: continue @@ -106,13 +107,14 @@ def _scan_clusters(self): def discover(self): """ - TODO: documentation + Do a scan down the tree of subdirectories looking for k8s clusters metadata files. + Open up a menu with all the found items, where you can pick up and configure it on your .kubeconfig file. """ cluster_files = [(path, data) for path, data in self._scan_clusters()] if not cluster_files: raise ExitError(1, "No clusters found.") - terminal_menu = TerminalMenu( + terminal_menu = simple_term_menu.TerminalMenu( [f"{c[1]['data']['cluster_name']}: {str(c[0])}" for c in cluster_files], title="Clusters found:" ) menu_entry_index = terminal_menu.show() @@ -133,10 +135,6 @@ def discover(self): self.container_config["working_dir"] = ( self.paths.guest_base_path / layer_path.relative_to(self.paths.cwd) ).as_posix() - - # TODO: rather than overriding property by propery, maybe a custom .paths object pointing to cluster_path? - self.paths.cwd = layer_path - self.paths.account_config_dir = self.paths._account_config_dir(layer_path) - self.paths.account_conf = self.paths.account_conf_from_layer(layer_path) - + # now simulate we are standing on the chosen layer folder + self.paths.update_cwd(layer_path) self.configure(cluster_info) diff --git a/leverage/path.py b/leverage/path.py index 6fc6c63..5272cf0 100644 --- a/leverage/path.py +++ b/leverage/path.py @@ -170,17 +170,13 @@ def __init__(self, env_conf: dict, container_user: str): self.host_aws_credentials_dir.mkdir(parents=True) self.sso_cache = self.host_aws_credentials_dir / "sso" / "cache" - def account_conf_from_layer(self, layer_path: Path): - return self._conf_from_layer(layer_path, self.ACCOUNT_TF_VARS) + def update_cwd(self, new_cwd): + self.cwd = new_cwd + acc_folder = new_cwd.relative_to(self.root_dir).parts[0] - def _conf_from_layer(self, layer_path: Path, file_name) -> dict: - acc_folder = layer_path.relative_to(self.root_dir).parts[0] - account_config_path = self.root_dir / acc_folder / "config" / file_name - return hcl2.loads(account_config_path.read_text()) - - def _account_config_dir(self, layer_path) -> dict: - acc_folder = layer_path.relative_to(self.root_dir).parts[0] - return self.root_dir / acc_folder / "config" + self.account_config_dir = self.root_dir / acc_folder / "config" + account_config_path = self.account_config_dir / self.ACCOUNT_TF_VARS + self.account_conf = hcl2.loads(account_config_path.read_text()) @property def guest_account_base_path(self):