Last active
April 21, 2026 23:51
-
-
Save therealparmesh/c67036b07c58bd1368800516ddfd5a28 to your computer and use it in GitHub Desktop.
dailypay kubectl helpers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # kx - kubectl helpers with fzf | |
| # deps: kubectl, fzf, jq, dpcli (mesh plugin for rollouts) | |
| # flags are ours - short aliases for kubectl's verbose ones | |
| # source from .zshrc | |
| __kx_dim() { printf '\033[2m%s\033[0m\n' "$*" >&2; } | |
| __kx_bold() { printf '\033[1m%s\033[0m\n' "$*" >&2; } | |
| __kx_err() { printf '\033[31m%s\033[0m\n' "$*" >&2; } | |
| __kx_ok() { printf '\033[32m%s\033[0m\n' "$*" >&2; } | |
| # fzf picker - skips menu if only one option | |
| __kx_pick() { | |
| local prompt="$1" autoselect="${2:-false}" | |
| local lines | |
| lines=$(cat) | |
| if [[ -z "$lines" ]]; then | |
| __kx_err "no ${prompt}s found" | |
| return 1 | |
| fi | |
| local selection | |
| if [[ "$autoselect" == true && $(echo "$lines" | wc -l | tr -d ' ') -eq 1 ]]; then | |
| selection="$lines" | |
| else | |
| selection=$(echo "$lines" | fzf --height=20 --prompt="$prompt > ") | |
| fi | |
| if [[ -z "$selection" ]]; then | |
| return 1 | |
| fi | |
| __kx_bold "$prompt > $selection" | |
| echo "$selection" | |
| } | |
| __kx_pick_pod() { | |
| __kx_dim "loading pods..." | |
| local out | |
| out=$(__kx_k get pods -o name) || return 1 | |
| echo "$out" | sed 's|^pod/||' | __kx_pick "pod" | |
| } | |
| __kx_pick_secret() { | |
| __kx_dim "loading secrets..." | |
| local out | |
| out=$(__kx_k get secret -o name) || return 1 | |
| echo "$out" | sed 's|^secret/||' | __kx_pick "secret" | |
| } | |
| # context format: spoke-ENV-green-use1/spoke:ENV:REGION:NAMESPACE:ROLE | |
| # namespace is the second-to-last colon segment | |
| __kx_target() { | |
| __kx_dim "loading contexts..." | |
| local out | |
| out=$(kubectl config get-contexts -o name) || return 1 | |
| __kx_ctx=$(echo "$out" | __kx_pick "context" true) || return 1 | |
| __kx_ns="${${__kx_ctx%:*}##*:}" | |
| __kx_bold "namespace > $__kx_ns" | |
| } | |
| __kx_k() { | |
| kubectl "$@" -n "$__kx_ns" --context="$__kx_ctx" | |
| } | |
| # just the pods - -w if you want to watch a deploy | |
| __kx_pods() { | |
| local flags=() | |
| local OPTIND opt | |
| while getopts "w" opt; do | |
| case $opt in | |
| w) flags+=(--watch) ;; | |
| esac | |
| done | |
| __kx_target || return 1 | |
| __kx_dim "loading pods..." | |
| __kx_k get pods "${flags[@]}" | |
| } | |
| # describe a pod - events, probes, env, image pull errors, the works | |
| __kx_desc() { | |
| __kx_target || return 1 | |
| local pod | |
| pod=$(__kx_pick_pod) || return 1 | |
| __kx_dim "describing $pod..." | |
| __kx_k describe pod "$pod" | |
| } | |
| # tail logs - -f to stream, -p for the crashed container, -a to see istio | |
| __kx_logs() { | |
| local all_containers=false tail_lines=50 | |
| local flags=() | |
| local OPTIND opt | |
| while getopts "afpt:" opt; do | |
| case $opt in | |
| a) all_containers=true ;; | |
| f) flags+=(--follow) ;; | |
| p) flags+=(--previous) ;; | |
| t) tail_lines="$OPTARG" ;; | |
| esac | |
| done | |
| __kx_target || return 1 | |
| local pod | |
| pod=$(__kx_pick_pod) || return 1 | |
| __kx_dim "loading containers..." | |
| local raw | |
| raw=$(__kx_k get pod "$pod" -o jsonpath='{.spec.containers[*].name}') || return 1 | |
| # istio-proxy is noise 99% of the time | |
| local containers | |
| containers=$(echo "$raw" | tr ' ' '\n') | |
| [[ "$all_containers" == false ]] && containers=$(echo "$containers" | grep -v '^istio-proxy$') | |
| if [[ -z "$containers" ]]; then | |
| __kx_err "no containers (all filtered - try kx logs -a)" | |
| return 1 | |
| fi | |
| local container | |
| container=$(echo "$containers" | __kx_pick "container" true) || return 1 | |
| __kx_dim "tailing $tail_lines lines from $pod/$container..." | |
| __kx_k logs "$pod" -c "$container" --tail="$tail_lines" "${flags[@]}" | |
| } | |
| # delete a pod - rollout spins up a fresh one, confirms first so you don't nuke prod | |
| __kx_kill() { | |
| __kx_target || return 1 | |
| local pod | |
| pod=$(__kx_pick_pod) || return 1 | |
| printf 'delete %s? [y/N] ' "$pod" >&2 | |
| read -r confirm | |
| if [[ "$confirm" == [yY] ]]; then | |
| __kx_dim "deleting $pod..." | |
| __kx_k delete pod "$pod" | |
| __kx_ok "deleted $pod" | |
| fi | |
| } | |
| # argo rollout status - needs the mesh plugin | |
| __kx_rollout() { | |
| __kx_target || return 1 | |
| __kx_dim "loading rollouts..." | |
| __kx_k get rollout | |
| } | |
| # decode a secret - all keys, base64 unwrapped, as json | |
| __kx_secret() { | |
| __kx_target || return 1 | |
| local name | |
| name=$(__kx_pick_secret) || return 1 | |
| __kx_dim "decoding $name..." | |
| __kx_k get secret "$name" -o json \ | |
| | jq '.data | to_entries | map({key: .key, value: (.value | @base64d)}) | from_entries' | |
| } | |
| # port-forward - kx pf 9001:9001 | |
| __kx_pf() { | |
| if [[ -z "$1" ]]; then | |
| __kx_err "usage: kx pf <local:remote>" | |
| return 1 | |
| fi | |
| local port="$1" | |
| __kx_target || return 1 | |
| local pod | |
| pod=$(__kx_pick_pod) || return 1 | |
| __kx_ok "forwarding $pod > localhost:${port%%:*}" | |
| __kx_k port-forward "$pod" "$port" | |
| } | |
| __kx_usage() { | |
| cat >&2 <<'EOF' | |
| usage: kx <command> [flags] | |
| commands: | |
| pods [-w] list pods | |
| desc describe a pod | |
| logs [-f] [-p] [-a] [-t N] tail a pod's logs | |
| kill delete a pod (confirms) | |
| rollout list argo rollouts | |
| secret decode a secret | |
| pf <local:remote> forward ports to a pod | |
| flags (ours - short aliases for kubectl): | |
| -a include istio-proxy container | |
| -f follow, stream live | |
| -p previous container, crash loops | |
| -t N tail N lines [default: 50] | |
| -w watch, live updates | |
| EOF | |
| } | |
| __KX_CMDS=( | |
| 'pods:list pods [-w]' | |
| 'desc:describe a pod' | |
| 'logs:tail logs [-f] [-p] [-a] [-t N]' | |
| 'kill:delete a pod' | |
| 'rollout:list argo rollouts' | |
| 'secret:decode a secret' | |
| 'pf:forward ports <local:remote>' | |
| ) | |
| kx() { | |
| local cmd="$1" | |
| if [[ -z "$cmd" ]]; then | |
| __kx_usage | |
| return 1 | |
| fi | |
| shift | |
| case "$cmd" in | |
| pods) __kx_pods "$@" ;; | |
| desc) __kx_desc "$@" ;; | |
| logs) __kx_logs "$@" ;; | |
| kill) __kx_kill "$@" ;; | |
| rollout) __kx_rollout "$@" ;; | |
| secret) __kx_secret "$@" ;; | |
| pf) __kx_pf "$@" ;; | |
| help) __kx_usage ;; | |
| *) | |
| __kx_err "unknown: $cmd" | |
| __kx_usage | |
| return 1 | |
| ;; | |
| esac | |
| } | |
| _kx() { | |
| if ((CURRENT == 2)); then | |
| _describe 'command' __KX_CMDS | |
| fi | |
| } | |
| compdef _kx kx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment