kubectl.plugin.zsh 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. if (( $+commands[kubectl] )); then
  2. # If the completion file does not exist, generate it and then source it
  3. # Otherwise, source it and regenerate in the background
  4. if [[ ! -f "$ZSH_CACHE_DIR/completions/_kubectl" ]]; then
  5. kubectl completion zsh | tee "$ZSH_CACHE_DIR/completions/_kubectl" >/dev/null
  6. source "$ZSH_CACHE_DIR/completions/_kubectl"
  7. else
  8. source "$ZSH_CACHE_DIR/completions/_kubectl"
  9. kubectl completion zsh | tee "$ZSH_CACHE_DIR/completions/_kubectl" >/dev/null &|
  10. fi
  11. fi
  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 kgpa='kubectl get pods --all-namespaces'
  33. alias kgpw='kgp --watch'
  34. alias kgpwide='kgp -o wide'
  35. alias kep='kubectl edit pods'
  36. alias kdp='kubectl describe pods'
  37. alias kdelp='kubectl delete pods'
  38. alias kgpall='kubectl get pods --all-namespaces -o wide'
  39. # get pod by label: kgpl "app=myapp" -n myns
  40. alias kgpl='kgp -l'
  41. # get pod by namespace: kgpn kube-system"
  42. alias kgpn='kgp -n'
  43. # Service management.
  44. alias kgs='kubectl get svc'
  45. alias kgsa='kubectl get svc --all-namespaces'
  46. alias kgsw='kgs --watch'
  47. alias kgswide='kgs -o wide'
  48. alias kes='kubectl edit svc'
  49. alias kds='kubectl describe svc'
  50. alias kdels='kubectl delete svc'
  51. # Ingress management
  52. alias kgi='kubectl get ingress'
  53. alias kgia='kubectl get ingress --all-namespaces'
  54. alias kei='kubectl edit ingress'
  55. alias kdi='kubectl describe ingress'
  56. alias kdeli='kubectl delete ingress'
  57. # Namespace management
  58. alias kgns='kubectl get namespaces'
  59. alias kens='kubectl edit namespace'
  60. alias kdns='kubectl describe namespace'
  61. alias kdelns='kubectl delete namespace'
  62. alias kcn='kubectl config set-context --current --namespace'
  63. # ConfigMap management
  64. alias kgcm='kubectl get configmaps'
  65. alias kgcma='kubectl get configmaps --all-namespaces'
  66. alias kecm='kubectl edit configmap'
  67. alias kdcm='kubectl describe configmap'
  68. alias kdelcm='kubectl delete configmap'
  69. # Secret management
  70. alias kgsec='kubectl get secret'
  71. alias kgseca='kubectl get secret --all-namespaces'
  72. alias kdsec='kubectl describe secret'
  73. alias kdelsec='kubectl delete secret'
  74. # Deployment management.
  75. alias kgd='kubectl get deployment'
  76. alias kgda='kubectl get deployment --all-namespaces'
  77. alias kgdw='kgd --watch'
  78. alias kgdwide='kgd -o wide'
  79. alias ked='kubectl edit deployment'
  80. alias kdd='kubectl describe deployment'
  81. alias kdeld='kubectl delete deployment'
  82. alias ksd='kubectl scale deployment'
  83. alias krsd='kubectl rollout status deployment'
  84. function kres(){
  85. kubectl set env $@ REFRESHED_AT=$(date +%Y%m%d%H%M%S)
  86. }
  87. # Rollout management.
  88. alias kgrs='kubectl get replicaset'
  89. alias kdrs='kubectl describe replicaset'
  90. alias kers='kubectl edit replicaset'
  91. alias krh='kubectl rollout history'
  92. alias kru='kubectl rollout undo'
  93. # Statefulset management.
  94. alias kgss='kubectl get statefulset'
  95. alias kgssa='kubectl get statefulset --all-namespaces'
  96. alias kgssw='kgss --watch'
  97. alias kgsswide='kgss -o wide'
  98. alias kess='kubectl edit statefulset'
  99. alias kdss='kubectl describe statefulset'
  100. alias kdelss='kubectl delete statefulset'
  101. alias ksss='kubectl scale statefulset'
  102. alias krsss='kubectl rollout status statefulset'
  103. # Port forwarding
  104. alias kpf="kubectl port-forward"
  105. # Tools for accessing all information
  106. alias kga='kubectl get all'
  107. alias kgaa='kubectl get all --all-namespaces'
  108. # Logs
  109. alias kl='kubectl logs'
  110. alias kl1h='kubectl logs --since 1h'
  111. alias kl1m='kubectl logs --since 1m'
  112. alias kl1s='kubectl logs --since 1s'
  113. alias klf='kubectl logs -f'
  114. alias klf1h='kubectl logs --since 1h -f'
  115. alias klf1m='kubectl logs --since 1m -f'
  116. alias klf1s='kubectl logs --since 1s -f'
  117. # File copy
  118. alias kcp='kubectl cp'
  119. # Node Management
  120. alias kgno='kubectl get nodes'
  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 kgdsw='kgds --watch'
  137. alias keds='kubectl edit daemonset'
  138. alias kdds='kubectl describe daemonset'
  139. alias kdelds='kubectl delete daemonset'
  140. # CronJob management.
  141. alias kgcj='kubectl get cronjob'
  142. alias kecj='kubectl edit cronjob'
  143. alias kdcj='kubectl describe cronjob'
  144. alias kdelcj='kubectl delete cronjob'
  145. # Job management.
  146. alias kgj='kubectl get job'
  147. alias kej='kubectl edit job'
  148. alias kdj='kubectl describe job'
  149. alias kdelj='kubectl delete job'
  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