Skip to content

Commit

Permalink
adding more comprehensive kubeletConfig class
Browse files Browse the repository at this point in the history
  • Loading branch information
jessebot committed Jan 11, 2024
1 parent 978db3c commit b497b1b
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 23 deletions.
24 changes: 18 additions & 6 deletions smol_k8s_lab/tui/distro_screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))

Expand Down Expand Up @@ -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)
87 changes: 70 additions & 17 deletions smol_k8s_lab/tui/distro_widgets/kubelet_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand All @@ -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:
"""
Expand All @@ -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()

Expand Down

0 comments on commit b497b1b

Please sign in to comment.