From 5e4a8464c5789184bbb74dfe920df7e45767e41a Mon Sep 17 00:00:00 2001 From: Bryan Joshua Pedini Date: Thu, 16 Apr 2026 19:21:35 +0200 Subject: [PATCH] feat(kubernetes): Guard kubectl initialization and completions Ensures kubectl-related aliases and functions are only sourced if the `kubectl` command is available. --- bashrc_overrides/kubernetes | 52 +++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/bashrc_overrides/kubernetes b/bashrc_overrides/kubernetes index 76f4321..b045d5c 100644 --- a/bashrc_overrides/kubernetes +++ b/bashrc_overrides/kubernetes @@ -1,25 +1,27 @@ -source <(kubectl completion bash) -alias k="kubectl" -alias kcontext="kubectl config use-context" -kns() { - if [[ $# -eq 0 ]]; then - printf '%s\n' "$(kubectl config view --minify -o jsonpath='{.contexts[0].context.namespace}')" - else - local ns="${1?Namespace required}" - kubectl config set-context --current --namespace "$ns" - printf 'Switched current context to namespace: %s\n' "$ns" - fi -} -_kns_completion() { - local cur - cur=${COMP_WORDS[COMP_CWORD]} - COMPREPLY=( $(compgen -W "$(kubectl get namespaces -o name | sed 's|namespace/||')" -- "$cur") ) -} -_kcontext_completion() { - local cur - cur=${COMP_WORDS[COMP_CWORD]} - COMPREPLY=( $(compgen -W "$(kubectl config get-contexts -o name)" -- $cur) ) -} -complete -F __start_kubectl k -complete -F _kns_completion kns -complete -F _kcontext_completion kcontext +if command -v kubectl >/dev/null 2>&1; then + source <(kubectl completion bash) + alias k="kubectl" + alias kcontext="kubectl config use-context" + kns() { + if [[ $# -eq 0 ]]; then + printf '%s\n' "$(kubectl config view --minify -o jsonpath='{.contexts[0].context.namespace}')" + else + local ns="${1?Namespace required}" + kubectl config set-context --current --namespace "$ns" + printf 'Switched current context to namespace: %s\n' "$ns" + fi + } + _kns_completion() { + local cur + cur=${COMP_WORDS[COMP_CWORD]} + COMPREPLY=( $(compgen -W "$(kubectl get namespaces -o name | sed 's|namespace/||')" -- "$cur") ) + } + _kcontext_completion() { + local cur + cur=${COMP_WORDS[COMP_CWORD]} + COMPREPLY=( $(compgen -W "$(kubectl config get-contexts -o name)" -- $cur) ) + } + complete -F __start_kubectl k + complete -F _kns_completion kns + complete -F _kcontext_completion kcontext +fi \ No newline at end of file