kubectl.plugin.zsh 5.6 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 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 >| "$ZSH_CACHE_DIR/completions/_kubectl"
  13. source "$ZSH_CACHE_DIR/completions/_kubectl"
  14. else
  15. source "$ZSH_CACHE_DIR/completions/_kubectl"
  16. kubectl completion zsh >| "$ZSH_CACHE_DIR/completions/_kubectl" &|
  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 -ti'
  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 rs'
  96. alias krh='kubectl rollout history'
  97. alias kru='kubectl rollout undo'
  98. # Statefulset management.
  99. alias kgss='kubectl get statefulset'
  100. alias kgssa='kubectl get statefulset --all-namespaces'
  101. alias kgssw='kgss --watch'
  102. alias kgsswide='kgss -o wide'
  103. alias kess='kubectl edit statefulset'
  104. alias kdss='kubectl describe statefulset'
  105. alias kdelss='kubectl delete statefulset'
  106. alias ksss='kubectl scale statefulset'
  107. alias krsss='kubectl rollout status statefulset'
  108. # Port forwarding
  109. alias kpf="kubectl port-forward"
  110. # Tools for accessing all information
  111. alias kga='kubectl get all'
  112. alias kgaa='kubectl get all --all-namespaces'
  113. # Logs
  114. alias kl='kubectl logs'
  115. alias kl1h='kubectl logs --since 1h'
  116. alias kl1m='kubectl logs --since 1m'
  117. alias kl1s='kubectl logs --since 1s'
  118. alias klf='kubectl logs -f'
  119. alias klf1h='kubectl logs --since 1h -f'
  120. alias klf1m='kubectl logs --since 1m -f'
  121. alias klf1s='kubectl logs --since 1s -f'
  122. # File copy
  123. alias kcp='kubectl cp'
  124. # Node Management
  125. alias kgno='kubectl get nodes'
  126. alias keno='kubectl edit node'
  127. alias kdno='kubectl describe node'
  128. alias kdelno='kubectl delete node'
  129. # PVC management.
  130. alias kgpvc='kubectl get pvc'
  131. alias kgpvca='kubectl get pvc --all-namespaces'
  132. alias kgpvcw='kgpvc --watch'
  133. alias kepvc='kubectl edit pvc'
  134. alias kdpvc='kubectl describe pvc'
  135. alias kdelpvc='kubectl delete pvc'
  136. # Service account management.
  137. alias kdsa="kubectl describe sa"
  138. alias kdelsa="kubectl delete sa"
  139. # DaemonSet management.
  140. alias kgds='kubectl get daemonset'
  141. alias kgdsw='kgds --watch'
  142. alias keds='kubectl edit daemonset'
  143. alias kdds='kubectl describe daemonset'
  144. alias kdelds='kubectl delete daemonset'
  145. # CronJob management.
  146. alias kgcj='kubectl get cronjob'
  147. alias kecj='kubectl edit cronjob'
  148. alias kdcj='kubectl describe cronjob'
  149. alias kdelcj='kubectl delete cronjob'
  150. # Only run if the user actually has kubectl installed
  151. if (( ${+_comps[kubectl]} )); then
  152. function kj() { kubectl "$@" -o json | jq; }
  153. function kjx() { kubectl "$@" -o json | fx; }
  154. function ky() { kubectl "$@" -o yaml | yh; }
  155. compdef kj=kubectl
  156. compdef kjx=kubectl
  157. compdef ky=kubectl
  158. fi