kubectl.plugin.zsh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. if (( $+commands[kubectl] )); then
  2. # TODO: 2022-01-05: remove this block
  3. # remove old generated files
  4. command rm -f "$ZSH_CACHE_DIR/kubectl_completion"
  5. # TODO: 2022-01-05: remove this bit of code as it exists in oh-my-zsh.sh
  6. # Add completions folder in $ZSH_CACHE_DIR
  7. command mkdir -p "$ZSH_CACHE_DIR/completions"
  8. (( ${fpath[(Ie)"$ZSH_CACHE_DIR/completions"]} )) || fpath=("$ZSH_CACHE_DIR/completions" $fpath)
  9. # If the completion file doesn't exist yet, we need to autoload it and
  10. # bind it to `kubectl`. Otherwise, compinit will have already done that.
  11. if [[ ! -f "$ZSH_CACHE_DIR/completions/_kubectl" ]]; then
  12. typeset -g -A _comps
  13. autoload -Uz _kubectl
  14. _comps[kubectl]=_kubectl
  15. fi
  16. kubectl completion zsh >! "$ZSH_CACHE_DIR/completions/_kubectl" &|
  17. fi
  18. # This command is used a LOT both below and in daily life
  19. alias k=kubectl
  20. # Execute a kubectl command against all namespaces
  21. alias kca='_kca(){ kubectl "$@" --all-namespaces; unset -f _kca; }; _kca'
  22. # Apply a YML file
  23. alias kaf='kubectl apply -f'
  24. # Drop into an interactive terminal on a container
  25. alias keti='kubectl exec -ti'
  26. # Manage configuration quickly to switch contexts between local, dev ad staging.
  27. alias kcuc='kubectl config use-context'
  28. alias kcsc='kubectl config set-context'
  29. alias kcdc='kubectl config delete-context'
  30. alias kccc='kubectl config current-context'
  31. # List all contexts
  32. alias kcgc='kubectl config get-contexts'
  33. # General aliases
  34. alias kdel='kubectl delete'
  35. alias kdelf='kubectl delete -f'
  36. # Pod management.
  37. alias kgp='kubectl get pods'
  38. alias kgpa='kubectl get pods --all-namespaces'
  39. alias kgpw='kgp --watch'
  40. alias kgpwide='kgp -o wide'
  41. alias kep='kubectl edit pods'
  42. alias kdp='kubectl describe pods'
  43. alias kdelp='kubectl delete pods'
  44. alias kgpall='kubectl get pods --all-namespaces -o wide'
  45. # get pod by label: kgpl "app=myapp" -n myns
  46. alias kgpl='kgp -l'
  47. # get pod by namespace: kgpn kube-system"
  48. alias kgpn='kgp -n'
  49. # Service management.
  50. alias kgs='kubectl get svc'
  51. alias kgsa='kubectl get svc --all-namespaces'
  52. alias kgsw='kgs --watch'
  53. alias kgswide='kgs -o wide'
  54. alias kes='kubectl edit svc'
  55. alias kds='kubectl describe svc'
  56. alias kdels='kubectl delete svc'
  57. # Ingress management
  58. alias kgi='kubectl get ingress'
  59. alias kgia='kubectl get ingress --all-namespaces'
  60. alias kei='kubectl edit ingress'
  61. alias kdi='kubectl describe ingress'
  62. alias kdeli='kubectl delete ingress'
  63. # Namespace management
  64. alias kgns='kubectl get namespaces'
  65. alias kens='kubectl edit namespace'
  66. alias kdns='kubectl describe namespace'
  67. alias kdelns='kubectl delete namespace'
  68. alias kcn='kubectl config set-context --current --namespace'
  69. # ConfigMap management
  70. alias kgcm='kubectl get configmaps'
  71. alias kgcma='kubectl get configmaps --all-namespaces'
  72. alias kecm='kubectl edit configmap'
  73. alias kdcm='kubectl describe configmap'
  74. alias kdelcm='kubectl delete configmap'
  75. # Secret management
  76. alias kgsec='kubectl get secret'
  77. alias kgseca='kubectl get secret --all-namespaces'
  78. alias kdsec='kubectl describe secret'
  79. alias kdelsec='kubectl delete secret'
  80. # Deployment management.
  81. alias kgd='kubectl get deployment'
  82. alias kgda='kubectl get deployment --all-namespaces'
  83. alias kgdw='kgd --watch'
  84. alias kgdwide='kgd -o wide'
  85. alias ked='kubectl edit deployment'
  86. alias kdd='kubectl describe deployment'
  87. alias kdeld='kubectl delete deployment'
  88. alias ksd='kubectl scale deployment'
  89. alias krsd='kubectl rollout status deployment'
  90. function kres(){
  91. kubectl set env $@ REFRESHED_AT=$(date +%Y%m%d%H%M%S)
  92. }
  93. # Rollout management.
  94. alias kgrs='kubectl get rs'
  95. alias krh='kubectl rollout history'
  96. alias kru='kubectl rollout undo'
  97. # Statefulset management.
  98. alias kgss='kubectl get statefulset'
  99. alias kgssa='kubectl get statefulset --all-namespaces'
  100. alias kgssw='kgss --watch'
  101. alias kgsswide='kgss -o wide'
  102. alias kess='kubectl edit statefulset'
  103. alias kdss='kubectl describe statefulset'
  104. alias kdelss='kubectl delete statefulset'
  105. alias ksss='kubectl scale statefulset'
  106. alias krsss='kubectl rollout status statefulset'
  107. # Port forwarding
  108. alias kpf="kubectl port-forward"
  109. # Tools for accessing all information
  110. alias kga='kubectl get all'
  111. alias kgaa='kubectl get all --all-namespaces'
  112. # Logs
  113. alias kl='kubectl logs'
  114. alias kl1h='kubectl logs --since 1h'
  115. alias kl1m='kubectl logs --since 1m'
  116. alias kl1s='kubectl logs --since 1s'
  117. alias klf='kubectl logs -f'
  118. alias klf1h='kubectl logs --since 1h -f'
  119. alias klf1m='kubectl logs --since 1m -f'
  120. alias klf1s='kubectl logs --since 1s -f'
  121. # File copy
  122. alias kcp='kubectl cp'
  123. # Node Management
  124. alias kgno='kubectl get nodes'
  125. alias keno='kubectl edit node'
  126. alias kdno='kubectl describe node'
  127. alias kdelno='kubectl delete node'
  128. # PVC management.
  129. alias kgpvc='kubectl get pvc'
  130. alias kgpvca='kubectl get pvc --all-namespaces'
  131. alias kgpvcw='kgpvc --watch'
  132. alias kepvc='kubectl edit pvc'
  133. alias kdpvc='kubectl describe pvc'
  134. alias kdelpvc='kubectl delete pvc'
  135. # Service account management.
  136. alias kdsa="kubectl describe sa"
  137. alias kdelsa="kubectl delete sa"
  138. # DaemonSet management.
  139. alias kgds='kubectl get daemonset'
  140. alias kgdsw='kgds --watch'
  141. alias keds='kubectl edit daemonset'
  142. alias kdds='kubectl describe daemonset'
  143. alias kdelds='kubectl delete daemonset'
  144. # CronJob management.
  145. alias kgcj='kubectl get cronjob'
  146. alias kecj='kubectl edit cronjob'
  147. alias kdcj='kubectl describe cronjob'
  148. alias kdelcj='kubectl delete cronjob'
  149. # Only run if the user actually has kubectl installed
  150. if (( ${+_comps[kubectl]} )); then
  151. function kj() { kubectl "$@" -o json | jq; }
  152. function kjx() { kubectl "$@" -o json | fx; }
  153. function ky() { kubectl "$@" -o yaml | yh; }
  154. compdef kj=kubectl
  155. compdef kjx=kubectl
  156. compdef ky=kubectl
  157. fi