Last active
October 25, 2024 18:00
-
-
Save jtyr/c842df66d7a69130bf20825e71940b5e to your computer and use it in GitHub Desktop.
kubectl aliases
This file contains 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
# Usage: source <(curl -L https://jtyr.io/source/kubectl) | |
TOOLS_DIR="${TOOLS_DIR:-/tmp/tools}" | |
TOOLS_BIN_DIR="$TOOLS_DIR/bin" | |
function get_goyq() { | |
YQ="$TOOLS_BIN_DIR/yq" | |
YQ_VERSION='4.30.4' | |
if [[ -e $YQ ]]; then | |
return | |
fi | |
curl -fSL -o "$YQ" "https://github.com/mikefarah/yq/releases/download/v$YQ_VERSION/yq_linux_amd64" | |
chmod +x "$YQ" | |
} | |
function get_gojq() { | |
GOJQ="$TOOLS_BIN_DIR/gojq" | |
GOJQ_VERSION='0.12.9' | |
if [[ -e $GOJQ ]]; then | |
return | |
fi | |
curl -fSL -o - "https://github.com/itchyny/gojq/releases/download/v$GOJQ_VERSION/gojq_v${GOJQ_VERSION}_linux_amd64.tar.gz" | tar xzf - -C "$TOOLS_DIR" gojq_v${GOJQ_VERSION}_linux_amd64/gojq | |
mv "$TOOLS_DIR/gojq_v${GOJQ_VERSION}_linux_amd64/gojq" "$TOOLS_BIN_DIR" | |
rm -fr "$TOOLS_DIR/gojq_v${GOJQ_VERSION}_linux_amd64" | |
} | |
function get_kubecolor() { | |
KUBECOLOR="$TOOLS_BIN_DIR/kubecolor" | |
KUBECOLOR_VERSION='0.0.21' | |
if [[ -e $KUBECOLOR ]]; then | |
return | |
fi | |
curl -fSL -o - "https://github.com/kubecolor/kubecolor/releases/download/v$KUBECOLOR_VERSION/kubecolor_${KUBECOLOR_VERSION}_Linux_x86_64.tar.gz" | tar xzf - -C "$TOOLS_BIN_DIR" kubecolor | |
} | |
function get_tools() { | |
mkdir -p "$TOOLS_BIN_DIR" | |
if [[ ${PATH:0:${#TOOLS_BIN_DIR}} != "$TOOLS_BIN_DIR" ]]; then | |
export PATH="$TOOLS_BIN_DIR:$PATH" | |
fi | |
get_goyq | |
get_gojq | |
get_kubecolor | |
} | |
function set_ps1() { | |
PS1='($(kubectl config get-contexts --no-headers | awk "function n(x) {return x==\"\"?\"default\":x} \\$1 == \"*\" {print(\"\[\033[01;33m\]\"\\$2\"\[\033[00m\]/\[\033[01;36m\]\"n(\\$5)\"\[\033[00m\]\")}")) \u@\h \[\033[01;34m\]\w\[\033[00m\] \[\033[01;32m\]\$\[\033[00m\] ' | |
} | |
function config_vim() { | |
cat <<END > ~/.vimrc | |
set paste | |
set pastetoggle=<F4> | |
set tabstop=2 | |
set shiftwidth=2 | |
set expandtab | |
END | |
} | |
function config_tmux() { | |
cat <<END > ~/.tmux.conf | |
unbind-key C-b | |
set-option -g prefix C-a | |
bind-key C-a send-prefix | |
#set -g mouse on | |
END | |
} | |
if [[ -n $KUBECOLOR ]] && [[ -e $KUBECOLOR ]]; then | |
alias k='kubecolor' | |
else | |
alias k='kubectl' | |
fi | |
# Get | |
alias kg='k get' | |
alias kgy='kgy_f() { k get -o yaml "$@" | cy; }; kgy_f' | |
alias kgyy='kgyy_f() { k get -o yaml "$@" | yq e -; }; kgyy_f' | |
alias kga='k get all' | |
alias kgp='k get pod' | |
alias kgpy='kgpy_f() { k get pod -o yaml "$@" | cy; }; kgpy_f' | |
alias kgpyy='kgpyy_f() { k get pod -o yaml "$@" | yq e -; }; kgpyy_f' | |
alias kgj='k get job' | |
alias kgjy='kgjy_f() { k get job -o yaml "$@" | cy; }; kgjy_f' | |
alias kgjyy='kgjyy_f() { k get job -o yaml "$@" | yq e -; }; kgjyy_f' | |
alias kgs='k get svc' | |
alias kgsy='kgsy_f() { k get svc -o yaml "$@" | cy; }; kgsy_f' | |
alias kgsyy='kgsyy_f() { k get svc -o yaml "$@" | yq e -; }; kgsyy_f' | |
alias kgd='k get deployment' | |
alias kgdy='kgdy_f() { k get deployment -o yaml "$@" | cy; }; kgdy_f' | |
alias kgdyy='kgdyy_f() { k get deployment -o yaml "$@" | yq e -; }; kgdyy_f' | |
alias kgn='k get node' | |
alias kgny='kgny_f() { k get node -o yaml "$@" | cy; }; kgny_f' | |
alias kgnyy='kgnyy_f() { k get node -o yaml "$@" | yq e -; }; kgnyy_f' | |
alias kgns='k get ns' | |
alias kgnsy='kgnsy_f() { k get ns -o yaml "$@" | cy; }; kgnsy_f' | |
alias kgnsyy='kgnsyy_f() { k get ns -o yaml "$@" | yq e -; }; kgnsyy_f' | |
alias kgi='k get ingress' | |
alias kgiy='kgiy_f() { k get ingress -o yaml "$@" | cy; }; kgiy_f' | |
alias kgiyy='kgiyy_f() { k get ingress -o yaml "$@" | yq e -; }; kgiyy_f' | |
alias kgsec='k get secret' | |
alias kgsecy='kgsecy_f() { k get secret -o yaml "$@" | cy; }; kgsecy_f' | |
alias kgsecyy='kgsecyy_f() { k get secret -o yaml "$@" | yq e -; }; kgsecyy_f' | |
alias kgpv='k get pv' | |
alias kgpvc='k get pvc' | |
alias kgpsc='k get sc' | |
# Describe | |
alias kd='k describe' | |
alias kdp='k describe pod' | |
alias kdj='k describe job' | |
alias kdd='k describe deployment' | |
alias kds='k describe svc' | |
alias kdn='k describe node' | |
alias kdns='k describe namespace' | |
alias kdi='k describe ingress' | |
alias kdsec='k describe secret' | |
alias kdpv='k describe pv' | |
alias kdpvc='k describe pvc' | |
alias kdpsc='k describe sc' | |
# Edit | |
alias ke='k edit' | |
alias kep='k edit pod' | |
alias kej='k edit job' | |
alias ked='k edit deployment' | |
alias kes='k edit svc' | |
alias kens='k edit ns' | |
alias kei='k edit ingress' | |
alias kesec='k edit secret' | |
# Delete | |
alias kdel='k delete' | |
alias kdelf='k delete -f' | |
alias kdelp='k delete pod' | |
alias kdelj='k delete job' | |
alias kdeld='k delete deployment' | |
alias kdels='k delete svc' | |
alias kdelns='k delete ns' | |
alias kdeli='k delete ingress' | |
alias kdelsec='k delete secret' | |
alias kdelfin='k patch -p "{\"metadata\":{\"finalizers\":null}}" --type=merge' | |
# Other | |
alias kv='k version --short' | |
alias kar='k api-resources --sort-by name' | |
alias kac='k auth can-i' | |
alias ktn='k top node' | |
alias ktp='k top pod' | |
alias kex='k expose' | |
alias kexpl='k explain' | |
alias kexplr='k explain --recursive=true' | |
alias kc='k create' | |
alias kcd='k create --dry-run=client -o yaml' | |
alias kx='k exec' | |
alias kl='k logs' | |
alias klf='k logs -f' | |
alias ka='k apply' | |
alias kaf='k apply -f' | |
alias krun='k run' | |
alias krund='k run --dry-run=client -o yaml' | |
alias kshell='k shell' | |
alias kgansr='k api-resources --verbs=list --namespaced -o name | xargs -n 1 kubectl get --show-kind --ignore-not-found' | |
alias kctx='k config use-context' | |
alias knsf='k config set-context --current --namespace' | |
alias kns='fn(){ k get ns --field-selector "metadata.name=$1" --ignore-not-found --no-headers | grep -E "^.+$" 1>/dev/null && k config set-context --current --namespace $1 || echo "No namespace \"$1\" found." && return 1; unset -f fn; }; fn' | |
alias cleanyaml="yq e '"'del(.. | select(tag == "!!map") | (.status, .creationTimestamp, .generation, .selfLink, .uid, .resourceVersion, .managedFields, ."kubectl.kubernetes.io/last-applied-configuration"))'"' -" | |
alias cleanjson="gojq '"'del(.. | select(. == "" or . == null)) | walk(if type == "object" then del(.status, .creationTimestamp, .generation, .selfLink, .uid, .resourceVersion, .managedFields, ."kubectl.kubernetes.io/last-applied-configuration") else . end) | del(.. | select(. == {}))'"'" | |
alias cy='cleanyaml' | |
alias cyy="cleanyaml | yq e '"'del(.. | select((. == "" and tag == "!!str") or tag == "!!null")) | del(... | select(tag == "!!map" and length == 0))'"' -" | |
alias cj='cleanjson' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment