Add comments

This commit is contained in:
Digital Studium 2024-04-21 11:50:45 +03:00
parent 262b329618
commit 0bda789c98
1 changed files with 16 additions and 12 deletions

20
kls
View File

@ -1,20 +1,27 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import subprocess, curses import subprocess, curses
KEY_BINDINGS = { KEY_BINDINGS = { # can be extended
"1": 'kubectl -n {namespace} get {api_resource} {resource} -o yaml | batcat -l yaml --paging always --style numbers', "1": 'kubectl -n {namespace} get {api_resource} {resource} -o yaml | batcat -l yaml --paging always --style numbers',
"2": 'kubectl -n {namespace} describe {api_resource} {resource} | batcat -l yaml --paging always --style numbers', "2": 'kubectl -n {namespace} describe {api_resource} {resource} | batcat -l yaml --paging always --style numbers',
"3": 'kubectl -n {namespace} edit {api_resource} {resource}', "3": 'kubectl -n {namespace} edit {api_resource} {resource}',
"4": 'kubectl -n {namespace} logs {resource} | batcat -l log --paging always --style numbers' "4": 'kubectl -n {namespace} logs {resource} | batcat -l log --paging always --style numbers'
} }
EXTRA_COLUMNS = { # By default, only the NAME column is displayed:.metadata.name EXTRA_COLUMNS = { # By default, only the NAME column is displayed. Any api resource can be added here
"pods": ',STATUS:.status.phase,NODE:.spec.nodeName', "pods": ',STATUS:.status.phase,NODE:.spec.nodeName',
"nodes": ',STATUS:.status.conditions[-1].type,CAPACITY_CPU:.status.capacity.cpu,CAPACITY_MEM:.status.capacity.memory',
"services": ',TYPE:.spec.type,CLUSTER_IP:.spec.clusterIP', "services": ',TYPE:.spec.type,CLUSTER_IP:.spec.clusterIP',
"ingresses": ',HOSTS:.spec.rules[*].host', "ingresses": ',HOSTS:.spec.rules[*].host',
"deployments": ',DESIRED_REPLICAS:.spec.replicas,READY_REPLICAS:.status.readyReplicas',
"persistentvolumeclaims": ',SIZE:.spec.resources.requests.storage,STORAGE_CLASS:.spec.storageClassName' "persistentvolumeclaims": ',SIZE:.spec.resources.requests.storage,STORAGE_CLASS:.spec.storageClassName'
} }
TOP_API_RESOURCES = ["pods", "services", "ingresses", "secrets", "nodes", "deployments", "statefulsets", "daemonsets",
"configmaps", "persistentvolumeclaims", "storageclasses"] # which api resources are on the top of menu?
HELP_TEXT = "Esc: exit filter mode or exit kls, 1: get yaml, 2: describe, 3: edit, 4: pod logs, arrows/TAB: navigation"
class Menu: class Menu:
def __init__(self, title, rows, begin_x, width): def __init__(self, title, rows, begin_x, width):
@ -35,17 +42,14 @@ class Menu:
SCREEN = curses.initscr() # screen initialization SCREEN = curses.initscr() # screen initialization
# convert command output to list # convert command output to list
kubectl = lambda command: subprocess.check_output("kubectl " + command, shell=True).decode().rstrip().split("\n") kubectl = lambda command: subprocess.check_output("kubectl " + command, shell=True).decode().strip().split("\n")
api_resources_top = ["pods", "services", "ingresses", "deployments", "statefulsets", "daemonsets", "configmaps",
"secrets", "persistentvolumes", "persistentvolumeclaims", "nodes", "storageclasses"]
api_resources_kubectl = kubectl("api-resources --no-headers --verbs=get | awk '{print $1}'") api_resources_kubectl = kubectl("api-resources --no-headers --verbs=get | awk '{print $1}'")
api_resources = list(dict.fromkeys(api_resources_top + api_resources_kubectl)) # so top api resources are at the top api_resources = list(dict.fromkeys(TOP_API_RESOURCES + api_resources_kubectl)) # so top api resources are at the top
width_unit = curses.COLS // 8 width_unit = curses.COLS // 8
MENUS = [Menu("Namespaces", kubectl("get ns --no-headers -o custom-columns=NAME:.metadata.name"), 0, width_unit), MENUS = [Menu("Namespaces", kubectl("get ns --no-headers -o custom-columns=NAME:.metadata.name"), 0, width_unit),
Menu("API resources", api_resources, width_unit, width_unit * 2), Menu("API resources", api_resources, width_unit, width_unit * 2),
Menu("Resources", [], width_unit * 3, curses.COLS - width_unit * 3)] Menu("Resources", [], width_unit * 3, curses.COLS - width_unit * 3)]
SELECTED_MENU = MENUS[0] SELECTED_MENU = MENUS[0]
HELP_TEXT = "Esc: exit filter mode or exit kls, 1: get yaml, 2: describe, 3: edit, 4: pod logs, arrows/TAB: navigation"
HEIGHT = curses.LINES - 9 # maximum number of visible row indices HEIGHT = curses.LINES - 9 # maximum number of visible row indices
namespace = MENUS[0].selected_row # method alias namespace = MENUS[0].selected_row # method alias
api_resource = MENUS[1].selected_row api_resource = MENUS[1].selected_row
@ -75,7 +79,7 @@ def update_menu3():
if namespace() and api_resource(): if namespace() and api_resource():
columns = EXTRA_COLUMNS.get(api_resource(), "") columns = EXTRA_COLUMNS.get(api_resource(), "")
command = f"-n {namespace()} get {api_resource()} --no-headers -o custom-columns=NAME:.metadata.name{columns}" command = f"-n {namespace()} get {api_resource()} --no-headers -o custom-columns=NAME:.metadata.name{columns}"
MENUS[2].rows = [x for x in kubectl(command) if x] # this is needed to remove empty values from the list MENUS[2].rows = kubectl(command)
MENUS[2].filtered_row_index = 0 # reset the selected row index before redrawing MENUS[2].filtered_row_index = 0 # reset the selected row index before redrawing
draw_menu(MENUS[2]) draw_menu(MENUS[2])