kubectl.plugin.zsh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. if (( $+commands[kubectl] )); then
  2. __KUBECTL_COMPLETION_FILE="${ZSH_CACHE_DIR}/kubectl_completion"
  3. if [[ ! -f $__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. # get pod by label: kgpl "app=myapp" -n myns
  36. alias kgpl='kgp -l'
  37. # get pod by namespace: kgpn kube-system"
  38. alias kgpn='kgp -n'
  39. # Service management.
  40. alias kgs='kubectl get svc'
  41. alias kgsa='kubectl get svc --all-namespaces'
  42. alias kgsw='kgs --watch'
  43. alias kgswide='kgs -o wide'
  44. alias kes='kubectl edit svc'
  45. alias kds='kubectl describe svc'
  46. alias kdels='kubectl delete svc'
  47. # Ingress management
  48. alias kgi='kubectl get ingress'
  49. alias kgia='kubectl get ingress --all-namespaces'
  50. alias kei='kubectl edit ingress'
  51. alias kdi='kubectl describe ingress'
  52. alias kdeli='kubectl delete ingress'
  53. # Namespace management
  54. alias kgns='kubectl get namespaces'
  55. alias kens='kubectl edit namespace'
  56. alias kdns='kubectl describe namespace'
  57. alias kdelns='kubectl delete namespace'
  58. alias kcn='kubectl config set-context $(kubectl config current-context) --namespace'
  59. # ConfigMap management
  60. alias kgcm='kubectl get configmaps'
  61. alias kgcma='kubectl get configmaps --all-namespaces'
  62. alias kecm='kubectl edit configmap'
  63. alias kdcm='kubectl describe configmap'
  64. alias kdelcm='kubectl delete configmap'
  65. # Secret management
  66. alias kgsec='kubectl get secret'
  67. alias kgseca='kubectl get secret --all-namespaces'
  68. alias kdsec='kubectl describe secret'
  69. alias kdelsec='kubectl delete secret'
  70. # Deployment management.
  71. alias kgd='kubectl get deployment'
  72. alias kgda='kubectl get deployment --all-namespaces'
  73. alias kgdw='kgd --watch'
  74. alias kgdwide='kgd -o wide'
  75. alias ked='kubectl edit deployment'
  76. alias kdd='kubectl describe deployment'
  77. alias kdeld='kubectl delete deployment'
  78. alias ksd='kubectl scale deployment'
  79. alias krsd='kubectl rollout status deployment'
  80. kres(){
  81. kubectl set env $@ REFRESHED_AT=$(date +%Y%m%d%H%M%S)
  82. }
  83. # Rollout management.
  84. alias kgrs='kubectl get rs'
  85. alias krh='kubectl rollout history'
  86. alias kru='kubectl rollout undo'
  87. # Statefulset management.
  88. alias kgss='kubectl get statefulset'
  89. alias kgssa='kubectl get statefulset --all-namespaces'
  90. alias kgssw='kgss --watch'
  91. alias kgsswide='kgss -o wide'
  92. alias kess='kubectl edit statefulset'
  93. alias kdss='kubectl describe statefulset'
  94. alias kdelss='kubectl delete statefulset'
  95. alias ksss='kubectl scale statefulset'
  96. alias krsss='kubectl rollout status statefulset'
  97. # Port forwarding
  98. alias kpf="kubectl port-forward"
  99. # Tools for accessing all information
  100. alias kga='kubectl get all'
  101. alias kgaa='kubectl get all --all-namespaces'
  102. # Logs
  103. alias kl='kubectl logs'
  104. alias kl1h='kubectl logs --since 1h'
  105. alias kl1m='kubectl logs --since 1m'
  106. alias kl1s='kubectl logs --since 1s'
  107. alias klf='kubectl logs -f'
  108. alias klf1h='kubectl logs --since 1h -f'
  109. alias klf1m='kubectl logs --since 1m -f'
  110. alias klf1s='kubectl logs --since 1s -f'
  111. # File copy
  112. alias kcp='kubectl cp'
  113. # Node Management
  114. alias kgno='kubectl get nodes'
  115. alias keno='kubectl edit node'
  116. alias kdno='kubectl describe node'
  117. alias kdelno='kubectl delete node'
  118. # PVC management.
  119. alias kgpvc='kubectl get pvc'
  120. alias kgpvca='kubectl get pvc --all-namespaces'
  121. alias kgpvcw='kgpvc --watch'
  122. alias kepvc='kubectl edit pvc'
  123. alias kdpvc='kubectl describe pvc'
  124. alias kdelpvc='kubectl delete pvc'
  125. # Colored JSON output
  126. kj() {
  127. kubectl "$@" -o json | jq
  128. }
  129. compdef kj=kubectl
  130. kjx() {
  131. kubectl "$@" -o json | fx
  132. }
  133. compdef kjx=kubectl
  134. # Colored YAML output
  135. ky() {
  136. kubectl "$@" -o yaml | yh
  137. }
  138. compdef ky=kubectl