rice

personal dot files and scripts for linux and macOS
Log | Files | Refs | README | LICENSE

kubernetes.zsh (2351B)


      1 # Load in zsh completions if they are available
      2 [ -f "$HOME.kube/completion.zsh.inc" ] && source "$HOME.kube/completion.zsh.inc"
      3 
      4 # Commonly used kubectl commands
      5 alias k="kubectl"
      6 alias kd="kubectl describe"
      7 alias kpw="watch kubectl get pods"
      8 alias i="istioctl"
      9 alias ia="istioctl analyze"
     10 alias h="helm"
     11 
     12 
     13 kcc() {
     14     # Switch current kube context to $1 if provided or prompt caller to
     15     # select from the available contexts in the current kube config.
     16     ctx=$([ -z "$1" ] && kubectl config get-contexts -o name | fzf || printf "$1")
     17     [ -z "$ctx" ] || kubectl config use-context $ctx
     18 }
     19 
     20 kc() {
     21     # Automatically apply the --context=$1 to each kubectl command. When
     22     # no $1 is passed the env var is unset.
     23     [ -z "$1" ] && unset KUBECTX || export KUBECTX="$1"
     24 }
     25 
     26 kn() {
     27     # Automatically apply the --namespace=$1 to each kubectl
     28     # command. When no $1 is passed the env var is unset.
     29     [ -z "$1" ] && unset KUBENS || export KUBENS=$1
     30 }
     31 
     32 knn() {
     33     ns=$(kubectl get namespaces -o name | sed 's/^namespace\///' | fzf)
     34     [ -z "$ns" ] || kn $ns
     35 }
     36 
     37 kge() {
     38     # Grab a list of all active generic environments
     39     kubectl --context=dev get ns -l sunday-env=generic --show-labels=true
     40 }
     41 
     42 kw() {
     43     # Override the kubectl command to automatically apply flags based on
     44     # environment variables.
     45     command watch kubectl \
     46             $([ -z "$KUBENS" ] || printf "--namespace=$KUBENS") \
     47             $([ -z "$KUBECTX" ] || printf "--context=$KUBECTX") \
     48             $@
     49 }
     50 
     51 kubectl() {
     52     # Override the kubectl command to automatically apply flags based on
     53     # environment variables.
     54     command kubectl \
     55             $([ -z "$KUBENS" ] || printf "--namespace=$KUBENS") \
     56             $([ -z "$KUBECTX" ] || printf "--context=$KUBECTX") \
     57             $@
     58 }
     59 
     60 helm() {
     61     # Override the helm command to automatically apply flags based on
     62     # environment variables.
     63     command helm \
     64             $([ -z "$KUBENS" ] || printf "--namespace=$KUBENS") \
     65             $([ -z "$KUBECTX" ] || printf "--kube-context=$KUBECTX") \
     66             $@
     67 }
     68 
     69 istioctl() {
     70     # Override the istioctl command to automatically apply flags based on
     71     # environment variables.
     72     command istioctl \
     73             $([ -z "$KUBENS" ] || printf "--namespace=$KUBENS") \
     74             $([ -z "$KUBECTX" ] || printf "--context=$KUBECTX") \
     75             $@
     76 }