kubectl.plugin.zsh 5.1 KB

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