New key bindings

This commit is contained in:
Digital Studium 2024-12-18 23:39:37 +03:00
parent d871cf645f
commit 987c24756b
2 changed files with 122 additions and 70 deletions

View File

@ -4,12 +4,12 @@
`kls` is a cli tool based on `kubectl` for managing kubernetes cluster resources. `kls` is a cli tool based on `kubectl` for managing kubernetes cluster resources.
Inspired by `lf` and `ranger` file managers, written in python. Inspired by `lf` and `ranger` file managers, written in python.
It is lightweight (~300 lines of code) and easy to customize. It is lightweight (~400 lines of code) and easy to customize.
Supports keyboard navigation and mouse navigation could be enabled (set MOUSE_ENABLED=True in a line #47). Supports keyboard navigation and mouse navigation could be enabled (set MOUSE_ENABLED=True in a line #54).
## Key bindings ## Key bindings
### For kubectl ### For kubectl
You can customize these bindings or add extra bindings in `KEY_BINDINGS` variable of `kls` in a line #7: You can customize these bindings or add extra bindings in `KEY_BINDINGS` variable of `kls` in a line #10:
- `Ctrl+y` - get yaml of resource - `Ctrl+y` - get yaml of resource
- `Ctrl+d` - describe resource - `Ctrl+d` - describe resource
- `Ctrl+e` - edit resource - `Ctrl+e` - edit resource
@ -29,12 +29,14 @@ You can customize these bindings or add extra bindings in `KEY_BINDINGS` variabl
## Dependencies ## Dependencies
- `python3` - `python3`
- `kubectl` - `kubectl`
- `bat` - `bat` - yaml viewer
- `lnav` - log viewer
- `yq` - yaml manipulation
## Installation ## Installation
Install `batcat`: Install `batcat`:
``` ```
sudo apt install bat -y sudo apt install bat lnav yq -y
``` ```
Download and install the latest `kls`: Download and install the latest `kls`:
``` ```

180
kls
View File

@ -4,53 +4,68 @@ import curses
import curses.ascii import curses.ascii
import asyncio import asyncio
# ****************************** #
# START OF CONFIGURATION SECTION #
# ****************************** #
KEY_BINDINGS = { # can be extended KEY_BINDINGS = { # can be extended
"^Y": { "^Y": { # Ctrl + y
"description": "View resource in YAML format", "description": "view YAML",
"command": 'kubectl -n {namespace} get {api_resource} {resource} -o yaml | batcat -l yaml' "command": 'kubectl -n {namespace} get {api_resource} {resource} -o yaml | batcat -l yaml'
}, },
"^D": { "^D": { # Ctrl + d
"description": "Describe resource", "description": "describe",
"command": 'kubectl -n {namespace} describe {api_resource} {resource} | batcat -l yaml' "command": 'kubectl -n {namespace} describe {api_resource} {resource} | batcat -l yaml'
}, },
"^E": { "^E": { # Ctrl + e
"description": "Edit resource", "description": "edit",
"command": 'kubectl -n {namespace} edit {api_resource} {resource}' "command": 'kubectl -n {namespace} edit {api_resource} {resource}'
}, },
"^L": { "^L": { # Ctrl + l
"description": "View logs", "description": "view logs",
"command": 'kubectl -n {namespace} logs {resource} | batcat -l log' "command": 'kubectl -n {namespace} logs {resource} | lnav'
}, },
"^X": { "^X": { # Ctrl + x
"description": "Exec into pod", "description": "exec pod",
"command": 'kubectl -n {namespace} exec -it {resource} sh' "command": 'kubectl -n {namespace} exec -it {resource} sh'
}, },
"^N": { "^N": { # Ctrl + n
"description": "Network debug", "description": "network debug",
"command": 'kubectl -n {namespace} debug {resource} -it --image=nicolaka/netshoot' "command": 'kubectl -n {namespace} debug {resource} -it --image=nicolaka/netshoot'
}, },
"KEY_DC": { "Delete": { # It is actually KEY_DC
"description": "Delete resource", "description": "delete",
"command": 'kubectl -n {namespace} delete {api_resource} {resource}' "command": 'kubectl -n {namespace} delete {api_resource} {resource}'
},
"^P": { # Ctrl + p (p means proxy! :-))
"description": "exec istio-proxy",
"command": 'kubectl -n {namespace} exec -it {resource} -c istio-proxy bash'
},
"^R": { # Ctrl + r (r means reveal! :-))
"description": "reveal secret",
"command": "kubectl get secret {resource} -n {namespace} -o yaml | yq '.data |= with_entries(.value |= @base64d)' -y | batcat -l yaml"
} }
} }
BATCAT_STYLE = " --paging always --style numbers"
# which api resources are on the top of menu? # which api resources are on the top of menu?
TOP_API_RESOURCES = ["pods", "services", "configmaps", "secrets", "persistentvolumeclaims", "ingresses", "nodes", TOP_API_RESOURCES = ["pods", "services", "configmaps", "secrets", "persistentvolumeclaims", "ingresses", "nodes",
"deployments", "statefulsets", "daemonsets", "storageclasses", "serviceentries", "deployments", "statefulsets", "daemonsets", "storageclasses", "serviceentries",
"destinationrules", "virtualservices", "gateways"] "destinationrules", "virtualservices", "gateways", "telemetry"]
QUERY_API_RESOURCES = False # Should we merge TOP_API_RESOURCES with all other api resources from cluster?
BATCAT_STYLE = " --paging always --style numbers" # style of batcat
MOUSE_ENABLED = False
# **************************** #
# END OF CONFIGURATION SECTION #
# **************************** #
# Dynamically generate HELP_TEXT based on KEY_BINDINGS descriptions # Dynamically generate HELP_TEXT based on KEY_BINDINGS descriptions
HELP_TEXT = ", ".join(f"{key}: {binding['description']}" for key, binding in KEY_BINDINGS.items()) HELP_TEXT = ", ".join(f"{key}: {binding['description']}" for key, binding in KEY_BINDINGS.items())
HELP_TEXT += ", /: filter mode, Esc: exit filter mode or exit kls, arrows/TAB/PgUp/PgDn: navigation" HELP_TEXT += ", /: filter mode, Esc: exit filter mode or exit kls, arrows/TAB/PgUp/PgDn: navigation"
MOUSE_ENABLED = False
SCREEN = curses.initscr() # screen initialization, needed for ROWS_HEIGHT working SCREEN = curses.initscr() # screen initialization, needed for ROWS_HEIGHT working
HEADER_HEIGHT = 4 # in rows HEADER_HEIGHT = 4 # in rows
FOOTER_HEIGHT = 3 FOOTER_HEIGHT = 3
ROWS_HEIGHT = curses.LINES - HEADER_HEIGHT - FOOTER_HEIGHT - 3 # maximum number of visible rows indices ROWS_HEIGHT = curses.LINES - HEADER_HEIGHT - FOOTER_HEIGHT - 3 # maximum number of visible rows indices
WIDTH = curses.COLS WIDTH = curses.COLS
QUERY_API_RESOURCES = False THIRD_MENU_LOCK = asyncio.Lock()
THIRD_MENU_TASK = None # It needs to be global because we should have ability to cancel it from anywhere
# classes # classes
@ -105,38 +120,52 @@ def draw_menu(menu: Menu):
async def refresh_third_menu(namespace, api_resource): async def refresh_third_menu(namespace, api_resource):
menu = menus[2] try:
previous_menu_rows = menu.rows async with THIRD_MENU_LOCK:
if api_resource and namespace: menu = menus[2]
try: previous_menu_rows = menu.rows
menu.rows = await kubectl_async(f"-n {namespace} get {api_resource} --no-headers --ignore-not-found") if api_resource and namespace:
except subprocess.CalledProcessError: try:
menu.rows = [] # Fallback to an empty list if the command fails menu.rows = await kubectl_async(
else: f"-n {namespace} get {api_resource} --no-headers --ignore-not-found")
menu.rows = [] except subprocess.CalledProcessError:
index_before_update = menu.filtered_rows.index menu.rows = [] # Fallback to an empty list if the command fails
menu.filtered_rows = CircularList([x for x in menu.rows if menu.filter in x]) # update filtered rows else:
menu.filtered_rows.index = index_before_update menu.rows = []
if menu.visible_row_index >= len(menu.visible_rows()): index_before_update = menu.filtered_rows.index
menu.visible_row_index = 0 menu.filtered_rows = CircularList([x for x in menu.rows if menu.filter in x]) # update filtered rows
if previous_menu_rows != menu.rows: menu.filtered_rows.index = index_before_update
draw_menu(menu) if menu.visible_row_index >= len(menu.visible_rows()):
menu.visible_row_index = 0
if previous_menu_rows != menu.rows:
draw_menu(menu)
except asyncio.CancelledError:
raise
async def handle_key_bindings(key: str, namespace: str, api_resource: str, resource: str): async def handle_key_bindings(key: str, namespace: str, api_resource: str, resource: str):
if not resource: if not resource:
return return
if key in ("l", "x", "n") and api_resource != "pods" and not resource.startswith("pod/"): if key in ("l", "x", "n") and api_resource != "pods":
return return
curses.def_prog_mode() # save the previous terminal state if key == "KEY_DC":
curses.endwin() # without this, there are problems after exiting vim key = "Delete"
command = KEY_BINDINGS[key]["command"].format(namespace=namespace, api_resource=api_resource, resource=resource) if THIRD_MENU_TASK is not None:
if "batcat" in command: THIRD_MENU_TASK.cancel()
command += BATCAT_STYLE try:
await subprocess_call_async(command) await THIRD_MENU_TASK
curses.reset_prog_mode() # restore the previous terminal state except asyncio.CancelledError:
SCREEN.refresh() pass
enable_mouse_support() async with THIRD_MENU_LOCK:
curses.def_prog_mode() # save the previous terminal state
curses.endwin() # without this, there are problems after exiting vim
command = KEY_BINDINGS[key]["command"].format(namespace=namespace, api_resource=api_resource, resource=resource)
if "batcat" in command:
command += BATCAT_STYLE
await subprocess_call_async(command)
curses.reset_prog_mode() # restore the previous terminal state
SCREEN.refresh()
enable_mouse_support()
async def handle_filter_state(key: str, menu: Menu): async def handle_filter_state(key: str, menu: Menu):
@ -150,7 +179,7 @@ async def handle_filter_state(key: str, menu: Menu):
else: else:
globals().update(selected_menu=None) # Exit program globals().update(selected_menu=None) # Exit program
elif menu.filter_mode: # Only process filter input in filter mode elif menu.filter_mode: # Only process filter input in filter mode
if key in ["KEY_BACKSPACE", "\x08"]: if key in ["KEY_BACKSPACE", "\x08"] and menu.filter:
menu.filter = menu.filter[:-1] # Remove last character menu.filter = menu.filter[:-1] # Remove last character
elif key.isalnum() or key == "-": # Allow letters, numbers, and dashes elif key.isalnum() or key == "-": # Allow letters, numbers, and dashes
menu.filter += key.lower() menu.filter += key.lower()
@ -251,36 +280,45 @@ async def get_key_async(popup: curses.window) -> str:
return await asyncio.to_thread(popup.getkey) return await asyncio.to_thread(popup.getkey)
async def kubectl_async(command: str) -> list:
process = await asyncio.create_subprocess_shell(
f"kubectl {command} 2> /dev/null", stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await process.communicate()
if stderr:
raise subprocess.CalledProcessError(process.returncode, command, stderr=stderr)
return stdout.decode().strip().split("\n")
async def catch_input(menu: Menu): async def catch_input(menu: Menu):
global THIRD_MENU_TASK
while True: # refresh third menu until key pressed while True: # refresh third menu until key pressed
try: try:
key = await get_key_async(SCREEN) key = await get_key_async(SCREEN)
break break
except curses.error: except curses.error:
await refresh_third_menu(namespace(), api_resource()) if THIRD_MENU_TASK is None or THIRD_MENU_TASK.done() or THIRD_MENU_TASK.cancelled():
THIRD_MENU_TASK = asyncio.create_task(refresh_third_menu(namespace(), api_resource()))
await asyncio.sleep(0.1) await asyncio.sleep(0.1)
if key in ["\t", "KEY_RIGHT", "KEY_BTAB", "KEY_LEFT"]: if key in ["\t", "KEY_RIGHT", "KEY_BTAB", "KEY_LEFT"]:
await handle_horizontal_navigation(key, menu) await handle_horizontal_navigation(key, menu)
elif key in ["KEY_UP", "KEY_DOWN", "KEY_NPAGE", "KEY_PPAGE", "KEY_HOME", "KEY_END"]: elif key in ["KEY_UP", "KEY_DOWN", "KEY_NPAGE", "KEY_PPAGE", "KEY_HOME", "KEY_END"]:
if THIRD_MENU_TASK is not None:
THIRD_MENU_TASK.cancel()
try:
# Wait for the THIRD_MENU_TASK to handle cancellation
await THIRD_MENU_TASK
except asyncio.CancelledError:
pass
await handle_vertical_navigation(key, menu) await handle_vertical_navigation(key, menu)
elif key == "KEY_MOUSE": elif key == "KEY_MOUSE":
await handle_mouse(menu) await handle_mouse(menu)
elif key == "KEY_DC" and await confirm_action("Are you sure you want to delete this resource?"): elif key == "KEY_DC" and await confirm_action("Are you sure you want to delete this resource?"):
await handle_key_bindings(key, namespace(), api_resource(), resource()) await handle_key_bindings(key, namespace(), api_resource(), resource())
elif key != "KEY_DC" and curses.ascii.unctrl(key) in KEY_BINDINGS.keys():
await handle_key_bindings(curses.ascii.unctrl(key), namespace(), api_resource(), resource())
elif key in ["/", "\x1b", "KEY_BACKSPACE", "\x08"] or key.isalnum() or key == "-": elif key in ["/", "\x1b", "KEY_BACKSPACE", "\x08"] or key.isalnum() or key == "-":
await handle_filter_state(key, menu) await handle_filter_state(key, menu)
elif key != "KEY_DC" and curses.ascii.unctrl(key) in KEY_BINDINGS.keys():
await handle_key_bindings(curses.ascii.unctrl(key), namespace(), api_resource(), resource())
async def kubectl_async(command: str) -> list:
result = await asyncio.create_subprocess_shell(
f"kubectl {command} 2> /dev/null", stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE
)
stdout, stderr = await result.communicate()
if stderr:
raise subprocess.CalledProcessError(result.returncode, command, stderr=stderr)
return stdout.decode().strip().split("\n")
async def subprocess_call_async(command: str): async def subprocess_call_async(command: str):
@ -297,13 +335,24 @@ def enable_mouse_support():
async def init_menus(): async def init_menus():
global menus, selected_menu, namespace, api_resource, resource global menus, selected_menu, namespace, api_resource, resource
api_resources_kubectl = [x.split()[0] for x in await kubectl_async("api-resources --no-headers --verbs=get")] api_resources_kubectl = [x.split()[0] for x in await kubectl_async("api-resources --no-headers --verbs=get")]
api_resources = list(dict.fromkeys(TOP_API_RESOURCES + api_resources_kubectl)) if QUERY_API_RESOURCES else TOP_API_RESOURCES api_resources = list(
dict.fromkeys(TOP_API_RESOURCES + api_resources_kubectl)) if QUERY_API_RESOURCES else TOP_API_RESOURCES
width_unit = WIDTH // 8 width_unit = WIDTH // 8
namespaces = []
try: try:
namespaces = await kubectl_async("get ns --no-headers -o custom-columns=NAME:.metadata.name") namespaces = await kubectl_async("config view --minify --output 'jsonpath={..namespace}'")
except: except:
namespaces = await kubectl_async( pass
"config view --minify --output 'jsonpath={..namespace}'") try:
all_namespaces = await kubectl_async("get ns --no-headers -o custom-columns=NAME:.metadata.name")
if all_namespaces:
if namespaces:
all_namespaces.remove(namespaces[0])
namespaces = namespaces + all_namespaces
else:
namespaces = all_namespaces
except:
pass
menus = [Menu("Namespaces", namespaces, 0, width_unit, ROWS_HEIGHT), menus = [Menu("Namespaces", namespaces, 0, width_unit, ROWS_HEIGHT),
Menu("API resources", api_resources, width_unit, width_unit * 2, ROWS_HEIGHT), Menu("API resources", api_resources, width_unit, width_unit * 2, ROWS_HEIGHT),
Menu("Resources", [], width_unit * 3, WIDTH - width_unit * 3, ROWS_HEIGHT)] Menu("Resources", [], width_unit * 3, WIDTH - width_unit * 3, ROWS_HEIGHT)]
@ -329,6 +378,7 @@ async def main_async(screen):
while selected_menu: while selected_menu:
await catch_input(selected_menu) await catch_input(selected_menu)
def main(screen): def main(screen):
asyncio.run(main_async(screen)) asyncio.run(main_async(screen))