my xfce4 dotfiles
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
6.7 KiB

3 years ago
  1. kube-ps1: Kubernetes prompt for bash and zsh
  2. ============================================
  3. A script that lets you add the current Kubernetes context and namespace
  4. configured on `kubectl` to your Bash/Zsh prompt strings (i.e. the `$PS1`).
  5. Inspired by several tools used to simplify usage of `kubectl`.
  6. ## Installing
  7. ### MacOS
  8. Homebrew package manager:
  9. ```
  10. $ brew update
  11. $ brew install kube-ps1
  12. ```
  13. ### From Source
  14. 1. Clone this repository
  15. 2. Source the kube-ps1.sh in your `~/.zshrc` or your `~/.bashrc`
  16. ### Arch Linux
  17. AUR Package available at [https://aur.archlinux.org/packages/kube-ps1/](https://aur.archlinux.org/packages/kube-ps1/).
  18. #### Zsh
  19. ```sh
  20. source /path/to/kube-ps1.sh
  21. PROMPT='$(kube_ps1)'$PROMPT
  22. ```
  23. #### Bash
  24. ```sh
  25. source /path/to/kube-ps1.sh
  26. PS1='[\u@\h \W $(kube_ps1)]\$ '
  27. ```
  28. ### Zsh Plugin Managers
  29. #### Using [zplugin](https://github.com/zdharma/zplugin)
  30. Update `.zshrc` with:
  31. ```sh
  32. zplugin light jonmosco/kube-ps1
  33. PROMPT='$(kube_ps1)'$PROMPT
  34. ```
  35. ## Requirements
  36. The default prompt assumes you have the `kubectl` command line utility installed.
  37. Official installation instructions and binaries are available:
  38. [Install and Set up kubectl](https://kubernetes.io/docs/tasks/tools/install-kubectl/)
  39. If using this with OpenShift, the `oc` tool needs installed. It can be obtained
  40. from brew ports:
  41. ```
  42. brew install openshift-cli
  43. ```
  44. or the source can be downloaded:
  45. [OC Client Tools](https://www.openshift.org/download.html)
  46. Set the binary to `oc` with the following environment variable:
  47. ```
  48. KUBE_PS1_BINARY=oc
  49. ```
  50. If neither binary is available, the prompt will print the following:
  51. ```
  52. (<symbol>|BINARY-N/A:N/A)
  53. ```
  54. ## Helper utilities
  55. There are several great tools that make using kubectl very enjoyable:
  56. - [`kubectx` and `kubens`](https://github.com/ahmetb/kubectx) are great for
  57. fast switching between clusters and namespaces.
  58. ## Tmux port
  59. I have begun porting kube-ps1 to tmux as a status line plugin. If you prefer
  60. tmux, and like the functionality provided by kube-ps1, checkout the
  61. [kube-tmux](https://github.com/jonmosco/kube-tmux) project
  62. ## Prompt Structure
  63. The default prompt layout is:
  64. ```
  65. (<symbol>|<context>:<namespace>)
  66. ```
  67. If the current-context is not set, kube-ps1 will return the following:
  68. ```
  69. (<symbol>|N/A:N/A)
  70. ```
  71. ## Enabling/Disabling
  72. If you want to stop showing Kubernetes status on your prompt string temporarily
  73. run `kubeoff`. To disable the prompt for all shell sessions, run `kubeoff -g`.
  74. You can enable it again in the current shell by running `kubeon`, and globally
  75. with `kubeon -g`.
  76. ```
  77. kubeon : turn on kube-ps1 status for this shell. Takes precedence over
  78. global setting for current session
  79. kubeon -g : turn on kube-ps1 status globally
  80. kubeoff : turn off kube-ps1 status for this shell. Takes precedence over
  81. global setting for current session
  82. kubeoff -g : turn off kube-ps1 status globally
  83. ```
  84. ## Customization
  85. The default settings can be overridden in `~/.bashrc` or `~/.zshrc` by setting
  86. the following environment variables:
  87. | Variable | Default | Meaning |
  88. | :------- | :-----: | ------- |
  89. | `KUBE_PS1_BINARY` | `kubectl` | Default Kubernetes binary |
  90. | `KUBE_PS1_NS_ENABLE` | `true` | Display the namespace. If set to `false`, this will also disable `KUBE_PS1_DIVIDER` |
  91. | `KUBE_PS1_PREFIX` | `(` | Prompt opening character |
  92. | `KUBE_PS1_SYMBOL_ENABLE` | `true ` | Display the prompt Symbol. If set to `false`, this will also disable `KUBE_PS1_SEPARATOR` |
  93. | `KUBE_PS1_SYMBOL_DEFAULT` | `⎈ ` | Default prompt symbol. Unicode `\u2388` |
  94. | `KUBE_PS1_SYMBOL_USE_IMG` | `false` | ☸️ , Unicode `\u2638` as the prompt symbol |
  95. | `KUBE_PS1_SEPARATOR` | &#124; | Separator between symbol and context name |
  96. | `KUBE_PS1_DIVIDER` | `:` | Separator between context and namespace |
  97. | `KUBE_PS1_SUFFIX` | `)` | Prompt closing character |
  98. | `KUBE_PS1_CLUSTER_FUNCTION` | No default, must be user supplied | Function to customize how cluster is displayed |
  99. | `KUBE_PS1_NAMESPACE_FUNCTION` | No default, must be user supplied | Function to customize how namespace is displayed |
  100. For terminals that do not support UTF-8, the symbol will be replaced with the
  101. string `k8s`.
  102. To disable a feature, set it to an empty string:
  103. ```
  104. KUBE_PS1_SEPARATOR=''
  105. ```
  106. ## Colors
  107. The default colors are set with the following environment variables:
  108. | Variable | Default | Meaning |
  109. | :------- | :-----: | ------- |
  110. | `KUBE_PS1_SYMBOL_COLOR` | `blue` | Set default color of the Kubernetes symbol |
  111. | `KUBE_PS1_CTX_COLOR` | `red` | Set default color of the context |
  112. | `KUBE_PS1_NS_COLOR` | `cyan` | Set default color of the namespace |
  113. | `KUBE_PS1_BG_COLOR` | `null` | Set default color of the prompt background |
  114. Blue was used for the default symbol to match the Kubernetes color as closely
  115. as possible. Red was chosen as the context name to stand out, and cyan for the
  116. namespace.
  117. Set the variable to an empty string if you do not want color for each
  118. prompt section:
  119. ```
  120. KUBE_PS1_CTX_COLOR=''
  121. ```
  122. Names are usable for the following colors:
  123. ```
  124. black, red, green, yellow, blue, magenta, cyan
  125. ```
  126. 256 colors are available by specifying the numerical value as the variable
  127. argument.
  128. ## Customize display of cluster name and namespace
  129. You can change how the cluster name and namespace are displayed using the
  130. `KUBE_PS1_CLUSTER_FUNCTION` and `KUBE_PS1_NAMESPACE_FUNCTION` variables
  131. respectively.
  132. For the following examples let's assume the following:
  133. cluster name: `sandbox.k8s.example.com`
  134. namespace: `alpha`
  135. If you're using domain style cluster names, your prompt will get quite long
  136. very quickly. Let's say you only want to display the first portion of the
  137. cluster name (`sandbox`), you could do that by adding the following:
  138. ```sh
  139. function get_cluster_short() {
  140. echo "$1" | cut -d . -f1
  141. }
  142. KUBE_PS1_CLUSTER_FUNCTION=get_cluster_short
  143. ```
  144. The same pattern can be followed to customize the display of the namespace.
  145. Let's say you would prefer the namespace to be displayed in all uppercase
  146. (`ALPHA`), here's one way you could do that:
  147. ```sh
  148. function get_namespace_upper() {
  149. echo "$1" | tr '[:lower:]' '[:upper:]'
  150. }
  151. export KUBE_PS1_NAMESPACE_FUNCTION=get_namespace_upper
  152. ```
  153. In both cases, the variable is set to the name of the function, and you must have defined the function in your shell configuration before kube_ps1 is called. The function must accept a single parameter and echo out the final value.
  154. ### Bug Reports and shell configuration
  155. Due to the vast ways of customizing the shell, please try the prompt with a
  156. minimal configuration before submitting a bug report.
  157. This can be done as follows for each shell before loading kube-ps1:
  158. Bash:
  159. ```bash
  160. bash --norc
  161. ```
  162. Zsh:
  163. ```bash
  164. zsh -f
  165. or
  166. zsh --no-rcs
  167. ```
  168. ## Contributors
  169. * [Ahmet Alp Balkan](https://github.com/ahmetb)
  170. * Jared Yanovich