Skip to content

Instantly share code, notes, and snippets.

@chancez
Last active April 16, 2019 16:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chancez/682a9885b6a26d7047ce3c914a6e172a to your computer and use it in GitHub Desktop.
Save chancez/682a9885b6a26d7047ce3c914a6e172a to your computer and use it in GitHub Desktop.
k() {
command kubectl --namespace="${_kube_ns:-default}" $@
}
alias kgp='k get pods'
alias kgs='k get svc'
alias kgn='k get ns'
kubectl() {
k "$@"
}
kn() {
kns "$@"
}
kc() {
local config_dir="$HOME/.kube/configs"
local configs="$(find $config_dir -name '*.yaml')"
# get just the name of the file without the base path or the yaml extension
local clean_configs=$(echo "$configs" | xargs basename | sed 's/\.yaml//')
# if our KUBECONFIG is already set, then check if it's set to a kubeconfig
# in our kubeconfig dir
if [ -n "$KUBECONFIG" ]; then
local current="$KUBECONFIG"
local current_clean="$(basename $current | sed 's/\.yaml//')"
# if the current kubeconfig is one of the available options, bolden it
# for selection
if echo $configs | grep -q "$current"; then
clean_configs=$(echo "$clean_configs" | sed "s/$current_clean/$(tput bold)$current_clean$(tput sgr0)/")
fi
fi
local fzf_cmd=(fzf --select-1 --ansi)
if [ -z "$DISABLE_KC_PREVIEW" ]; then
local jq_expr='del(.users,.clusters[].cluster["certificate-authority-data"])'
fzf_cmd+=(--preview 'faq -f yaml '"'$jq_expr'"' '"$config_dir"'/{}.yaml | head -$LINES')
fi
export KUBECONFIG="$(echo "$clean_configs" | "${fzf_cmd[@]}" | xargs printf "$config_dir/%s.yaml")"
}
kns() {
local ns="${_kube_ns:-default}"
local namespaces="$(command kubectl get ns -o=custom-columns=:.metadata.name --no-headers)"
local fzf_cmd=(fzf --select-1 --ansi)
if [ -z "$DISABLE_KNS_PREVIEW" ]; then
fzf_cmd+=(--preview 'command kubectl get pods --namespace {} | head -$LINES')
fi
export _kube_ns="$(echo "$namespaces" | sed "s/$ns/$(tput bold)$ns$(tput sgr0)/" | "${fzf_cmd[@]}" )"
}
kcns() {
kc && kns
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment