Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add namespace management and add functions #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 146 additions & 45 deletions kubectl_aliases
Original file line number Diff line number Diff line change
@@ -1,57 +1,158 @@
#!/bin/bash
##!/bin/bash

## apply default namestace
## kn namespace-name (will set amespace-name as fefault for all commands)
## kn (will clear default namespace)
function kn() {
if [ -n "$1" ]; then
export KUBECTL_NAMESPACE=$1
echo "k implicit namespace set to $1"
else
unset KUBECTL_NAMESPACE
echo "k implicit namespace cleared for"
fi
}

function k() {
local kubens
if [ -n "$KUBECTL_NAMESPACE" ]; then
local kubens="-n=$KUBECTL_NAMESPACE"
fi
kubectl $kubens $@
}

## search pod by short name
function kf() {
local name=$(kgp -o custom-columns=NAME:.metadata.name | grep "$1" | head -1 | cut -d" " -f1)
echo "$name"
}

function klog() {
if [[ -z ${2:-} ]] ; then
local pod_name=$(kf $1)
k logs -f --tail=500 $pod_name
else
k logs -f --tail=500 $@
fi
}

function kgp() {
if [ -n "$1" ] ; then
k get pod $@
else
k get pod -o wide --sort-by=.metadata.creationTimestamp
fi
}

function kx() {
local pod_name=$(kf $1)
shift
if [[ -z ${1:-} ]] ; then args_pass="bash" ; else args_pass=$@ fi
k exec -ti $pod_name -- $args_pass
}

# port forward to service
function kpf() {
if [[ $(k get svc $1 2>/dev/null) ]] ; then
local service_name=$1
shift
if [[ -z ${1:-} ]] ; then
local port_number=$(k get svc $service_name -o=jsonpath='{.spec.ports[0].port}' | grep -v "Applying implicit namespace")
k port-forward svc/$service_name "${port_number}:${port_number}"
else
k port-forward svc/$service_name $@
fi
else
k port-forward $@
fi
}

## get list of secret and show values of secrets if specified
function ksec() {
if [ -n "$1" ] ; then
k get secret $1 -o json | jq '.data | map_values(@base64d)'
else
k get secret | grep -v 'sh.helm.release' | grep -v 'kubernetes.io/service-account-token'
fi
}

# other
alias k='kubectl'
alias kl='kubectl logs'
alias kexec='kubectl exec -it'
alias kpf='kubectl port-forward'
alias kaci='kubectl auth can-i'
alias kat='kubectl attach'
alias kl='k logs'
alias kexec='k exec -it'
alias kaci='k auth can-i'
alias kat='k attach'
alias ktopall='k top pod --all-namespaces --no-headers --sort-by=cpu | head -20'
alias ktop='k top pod --no-headers --sort-by=cpu | head -20'
alias kev="k get events --sort-by='.metadata.creationTimestamp' | tail -100"

# get
alias kg='kubectl get'
alias kgns='kubectl get ns'
alias kgp='kubectl get pods'
alias kgd='kubectl get deployments'
alias kgrs='kubectl get rs'
alias kgss='kubectl get ss'
alias kgds='kubectl get ds'
alias kgs='kubectl get svc -o wide'
alias kgn='kubectl get nodes -o wide'
alias kg='k get'
alias kgns='k get ns'
alias kgd='k get deployments'
alias kgpa='k get po --all-namespaces -o wide --sort-by=.metadata.creationTimestamp'
alias kgrs='k get rs'
alias kgss='k get ss'
alias kgds='k get ds'
alias kgs='k get svc -o wide'
alias kgn='k get nodes -o wide'

# describe
alias kdes='kubectl describe'
alias kdesns='kubectl describe ns'
alias kdesp='kubectl describe pod'
alias kdesd='kubectl describe deployment'
alias kdesrs='kubectl describe rs'
alias kdesss='kubectl describe ss'
alias kdesds='kubectl describe ds'
alias kdess='kubectl describe svc'
alias kdes='k describe'
alias kdesns='k describe ns'
alias kdesp='k describe pod'
alias kdesd='k describe deployment'
alias kdesrs='k describe rs'
alias kdesss='k describe ss'
alias kdesds='k describe ds'
alias kdess='k describe svc'

# delete
alias kd='kubectl delete'
alias kdns='kubectl delete ns'
alias kdp='kubectl delete pod'
alias kdd='kubectl delete deployment'
alias kdrs='kubectl delete rs'
alias kdss='kubectl delete ss'
alias kdds='kubectl delete ds'
alias kds='kubectl delete svc'
alias kd='k delete'
alias kdns='k delete ns'
alias kdp='k delete pod'
alias kdd='k delete deployment'
alias kdrs='k delete rs'
alias kdss='k delete ss'
alias kdds='k delete ds'
alias kds='k delete svc'

# edit
alias ke='kubectl edit'
alias kens='kubectl edit ns'
alias ked='kubectl edit deployment'
alias kers='kubectl edit rs'
alias kess='kubectl edit ss'
alias keds='kubectl edit ds'
alias kes='kubectl edit svc'
alias ke='k edit'
alias kens='k edit ns'
alias ked='k edit deployment'
alias kers='k edit rs'
alias kess='k edit ss'
alias keds='k edit ds'
alias kes='k edit svc'

# config
alias kcfg='kubectl config'
alias kcfgns='kubectl config set-context --current --namespace'
alias kcfgcurrent='kubectl config current-context'
alias kcfggc='kubectl config get-contexts'
alias kcfgsc='kubectl config set-context'
alias kcfguc='kubectl config use-context'
alias kcfg='k config'
alias kcfgns='k config set-context --current --namespace'
alias kcfgcurrent='k config current-context'
alias kcfggc='k config get-contexts'
alias kcfgsc='k config set-context'
alias kcfguc='k config use-context'

# select from available context
function kctx
{
local PS3="Please select a context: "
local COLUMNS=20
local CONTEXTS=`k config view | grep -e "^- name: " | sed -n -e 's/- name: \(.*\)/\1/p'`
local CURRENT=`k config current-context`

select CTX in `echo $CONTEXTS`;
do
case $CTX in
$CURRENT)
echo "Already on that context: $CURRENT"
break
;;
*)
echo "Switching context: $CTX ($REPLY)"
k config use-context $CTX
break
;;
esac
done
}