kubectl.plugin.zsh 5.8 KB

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