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

[BV-599] Kubeconfig helper #281

Merged
merged 19 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
tweaks + simplifications
  • Loading branch information
Franr committed Nov 23, 2024
commit 0af474190d20d4b579c5ee474f0664db6c3170b5
18 changes: 8 additions & 10 deletions leverage/containers/kubectl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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)
16 changes: 6 additions & 10 deletions leverage/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading