Last active
May 11, 2026 18:21
-
-
Save eznix86/a529f7ec05553d418c9e0d0c6f3f6c85 to your computer and use it in GitHub Desktop.
Transparent kubectl wrapper with kubecolor, automatic in-memory SOPS decryption, prod-context confirmations, recursive -f support, completion, and k aliases.
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
| # kubecolor | |
| # SOPS decryption | |
| # prod confirmation | |
| # completion | |
| # kubectl watch -> kubectl klock | |
| unalias kubectl 2>/dev/null | |
| unalias k 2>/dev/null | |
| KUBE_CONFIRM_CONTEXTS='(prod|preprod)' | |
| kubectl() { | |
| # Allow completion internals | |
| [[ "$1" == "__complete" || $# -eq 0 ]] && { | |
| command kubecolor "$@" | |
| return | |
| } | |
| # kubectl watch ... -> kubectl klock ... | |
| if [[ "$1" == "watch" ]]; then | |
| shift | |
| command kubecolor klock "$@" | |
| return | |
| fi | |
| local args=() | |
| local sources=() | |
| local recursive=0 | |
| while [[ $# -gt 0 ]]; do | |
| case "$1" in | |
| -f|--filename) | |
| sources+=("$2") | |
| shift 2 | |
| ;; | |
| --filename=*) | |
| sources+=("${1#--filename=}") | |
| shift | |
| ;; | |
| -R|--recursive) | |
| recursive=1 | |
| args+=("$1") | |
| shift | |
| ;; | |
| *) | |
| args+=("$1") | |
| shift | |
| ;; | |
| esac | |
| done | |
| local ctx | |
| ctx=$(command kubectl config current-context 2>/dev/null) | |
| if [[ "${ctx:l}" =~ ${~KUBE_CONFIRM_CONTEXTS} ]] && | |
| [[ ! "${args[1]}" =~ ^(get|describe)$ ]]; then | |
| print -P "%F{yellow}Context:%f $ctx" | |
| echo "kubectl ${args[@]} ${sources:+-f ${sources[*]}}" | |
| read -q "REPLY?Proceed? [y/N] " || { | |
| echo | |
| return 1 | |
| } | |
| echo | |
| fi | |
| process_file() { | |
| local file="$1" | |
| echo "---" | |
| if grep -qE '(^sops:|^[[:space:]]*"sops"[[:space:]]*:)' "$file"; then | |
| sops --decrypt "$file" | |
| else | |
| cat "$file" | |
| fi | |
| } | |
| if [[ ${#sources[@]} -eq 0 ]]; then | |
| command kubecolor "${args[@]}" | |
| return | |
| fi | |
| { | |
| for src in "${sources[@]}"; do | |
| if [[ -f "$src" ]]; then | |
| process_file "$src" | |
| elif [[ -d "$src" ]]; then | |
| if [[ "$recursive" -eq 1 ]]; then | |
| find "$src" -type f \( -name '*.yaml' -o -name '*.yml' -o -name '*.json' \) | sort | | |
| while read -r file; do | |
| process_file "$file" | |
| done | |
| else | |
| find "$src" -maxdepth 1 -type f \( -name '*.yaml' -o -name '*.yml' -o -name '*.json' \) | sort | | |
| while read -r file; do | |
| process_file "$file" | |
| done | |
| fi | |
| else | |
| echo "unsupported source: $src" >&2 | |
| return 1 | |
| fi | |
| done | |
| } | command kubecolor "${args[@]}" -f - | |
| } | |
| alias k=kubectl | |
| source <(command kubectl completion zsh) | |
| compdef _kubectl kubectl | |
| compdef _kubectl k |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment