From b497b1b7cd04556426a519d79166ac1d33e415bc Mon Sep 17 00:00:00 2001 From: jessebot Date: Thu, 11 Jan 2024 10:20:26 +0100 Subject: [PATCH] adding more comprehensive kubeletConfig class --- smol_k8s_lab/tui/distro_screen.py | 24 +++-- .../tui/distro_widgets/kubelet_config.py | 87 +++++++++++++++---- 2 files changed, 88 insertions(+), 23 deletions(-) diff --git a/smol_k8s_lab/tui/distro_screen.py b/smol_k8s_lab/tui/distro_screen.py index cd1608789..424136d8e 100755 --- a/smol_k8s_lab/tui/distro_screen.py +++ b/smol_k8s_lab/tui/distro_screen.py @@ -220,8 +220,9 @@ def __init__(self, def compose(self) -> ComposeResult: with Grid(classes="k8s-distro-config", id="kind-box"): # take number of nodes from config and make string - nodes = self.metadata.get('nodes', {'control_plane': 1, - 'workers': 0}) + nodes = self.metadata.get('nodes', + {'control_plane': 1, 'workers': 0} + ) control_nodes = str(nodes.get('control_plane', '1')) worker_nodes = str(nodes.get('workers', '0')) @@ -300,7 +301,18 @@ def compose(self) -> ComposeResult: # node input row yield NodeAdjustmentBox(self.distro, control_nodes, worker_nodes) - # take extra k3s args if self.distro is k3s or k3d - yield K3sConfig(self.distro, - self.metadata['k3s_yaml'], - id=f"{self.distro}-widget") + + # Add the TabbedContent widget for kind config + with TabbedContent(initial="kind-networking-tab", id="kind-tabbed-content"): + # tab 1 - networking options + with TabPane("Networking options", id="kind-networking-tab"): + # take extra k3s args if self.distro is k3s or k3d + yield K3sConfig(self.distro, + self.metadata['k3s_yaml'], + id=f"{self.distro}-widget") + + # tab 2 - kubelet options + with TabPane("Kubelet Config Options", id="kind-kubelet-tab"): + # kubelet config section for kind only + kubelet_args = self.metadata['k3s_yaml'].get('kubelet-args', '') + yield KubeletConfig('k3s', kubelet_args) diff --git a/smol_k8s_lab/tui/distro_widgets/kubelet_config.py b/smol_k8s_lab/tui/distro_widgets/kubelet_config.py index 95f82cb00..469d7c11d 100644 --- a/smol_k8s_lab/tui/distro_widgets/kubelet_config.py +++ b/smol_k8s_lab/tui/distro_widgets/kubelet_config.py @@ -10,7 +10,9 @@ VALUE_SUGGESTIONS = SuggestFromList(("ingress-ready=true", "pods-per-core", "resolv-conf", - "max-pods")) + "max-pods", + "system-reserved", + "kube-reserved")) kubelet_help = ( "Add key value pairs to pass to your [steel_blue][b][link=https://kubernetes.io/docs/" @@ -36,8 +38,26 @@ def compose(self) -> ComposeResult: def on_mount(self) -> None: if self.kubelet_extra_args: - for key, value in self.kubelet_extra_args.items(): - self.generate_row(key, str(value)) + if self.distro == "kind": + for key, value in self.kubelet_extra_args.items(): + self.generate_row(key, str(value)) + + if self.distro == "k3s": + for item in self.kubelet_extra_args: + if "kube-reserved" in item: + key = "kube-reserved" + value = item.replace("kube-reserved=", "") + + elif "system-reserved" in item: + key = "system-reserved" + value = item.replace("system-reserved=", "") + + else: + key_value_pair = item.split('=') + key = key_value_pair[0] + value = key_value_pair[1] + + self.generate_row(key, str(value)) def on_button_pressed(self, event: Button.Pressed) -> None: """ @@ -46,26 +66,59 @@ def on_button_pressed(self, event: Button.Pressed) -> None: # lets you delete a kubelet-arg row parent_row = event.button.parent input_key = parent_row.children[1].name - app_yaml = self.app.cfg['k8s_distros'][self.distro]['kubelet_extra_args'] - if input_key and app_yaml.get(input_key, False): - app_yaml.pop(input_key) - self.app.write_yaml() - parent_row.remove() + + app_yaml = self.app.cfg['k8s_distros'][self.distro] + + if self.distro == "kind": + kind_args = app_yaml['kubelet_extra_args'] + + if input_key and kind_args.get(input_key, False): + kind_args.pop(input_key) + self.app.write_yaml() + + parent_row.remove() + + # this isn't beautiful, but should get the job done + if self.distro == "k3s": + k3s_args = app_yaml['k3s_yaml'] + pop_item = None + + if input_key: + for item in k3s_args: + if input_key in item: + pop_item = item + + if pop_item: + k3s_args.pop[pop_item] + self.app.write_yaml() + + parent_row.remove() @on(Input.Submitted) @on(Input.Changed) def update_base_yaml(self, event: Input.Changed | Input.Submitted) -> None: if event.validation_result.is_valid: # grab the user's yaml file from the parent app - args = self.app.cfg['k8s_distros'][self.distro]['kubelet_extra_args'] - - # convert this to an int if its possible - try: - int_value = int(event.input.value) - args[event.input.name] = int_value - # if value can't an int, just set it normally - except ValueError: - args[event.input.name] = event.input.value + distro_cfg = self.app.cfg['k8s_distros'][self.distro] + + # kind uses a different schema than k3s for kubelet args. uses dict + if self.distro == "kind": + args = distro_cfg['kubelet_extra_args'] + # convert this to an int if its possible + try: + int_value = int(event.input.value) + args[event.input.name] = int_value + # if value can't an int, just set it normally. uses list + except ValueError: + args[event.input.name] = event.input.value + + # k3s uses a list + if self.distro == "k3s": + args = distro_cfg["k3s_yaml"] + if isinstance(args, list): + args.append(f"{event.input.name}={event.input.value}") + else: + args = [f"{event.input.name}={event.input.value}"] self.app.write_yaml()