kubectl.plugin.zsh 5.6 KB

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