├── kubectl-watch.gif ├── README.md └── kubectl-watch /kubectl-watch.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lee0c/kubectl-watch/HEAD/kubectl-watch.gif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # kubectl-watch :eyes: 2 | 3 | [Kubernetes CLI plugin](https://kubernetes.io/docs/tasks/extend-kubectl/kubectl-plugins/) for better resource watching output. 4 | 5 | ![GIF of the plugin in use, showing information about the pods of a Prometheus deployment updating on the console](./kubectl-watch.gif) 6 | 7 | ## Installation 8 | 9 | ```bash 10 | git clone https://github.com/lee0c/kubectl-watch.git 11 | cd kubectl-watch 12 | chmod +x kubectl-watch 13 | ln -s /path/to/kubectl-watch/kubectl-watch /usr/local/bin/kubectl-watch 14 | ``` 15 | 16 | You can then run `kubectl plugin list` to confirm that this is available as a kubectl plugin, and use `kubectl watch ---` as a kubectl command. 17 | 18 | ## Use 19 | 20 | `kubectl watch` will take any arguments `kubectl get` takes. It additionally adds a few options which **must be specified before any standard kubectl arguments**. Arguments that require values are shown with their default value. 21 | 22 | | Options | | 23 | | :------ | --- | 24 | | -c, --calls 60 | Make 60 calls before exiting (pass 0 to disable progress bar/exiting) | 25 | | -s, --sleep 1 | Sleep for the specified number of seconds between calls | 26 | | -h, --help | Displays help text. | 27 | | --simple | Force use of non-UTF-8 progress bar chars on a terminal with UTF-8 support. | 28 | 29 | ## Notes 30 | 31 | By default, this uses UTF-8 characters in the progress bar. It should switch to standard ANSI characters if your terminal doesn't support UTF-8, but if you're having issues with it try using the `--simple` flag - and [put in a :bug:](https://github.com/lee0c/kubectl-watch/issues) -------------------------------------------------------------------------------- /kubectl-watch: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | help-text() { 4 | cat << END 5 | Watches Kubernetes resources. 6 | Author: Lee Cattarin - github.com/lee0c/kubectl-watch 7 | v0.4.2 8 | 9 | Use as a kubectl action by adding this to your path under the name kubectl-watch. 10 | Available arguments mirrors kubectl get. 11 | 12 | Options must be specified before standard kubectl commands. 13 | 14 | Additional options (defaults shown): 15 | kubectl watch -c, --calls 60 : Make 60 calls before exiting (pass 0 to disable progress bar/exiting). 16 | kubectl watch -s, --sleep 1 : Sleep for the specified number of seconds between calls. 17 | kubectl watch -h, --help : Displays this text. 18 | kubectl watch --simple : Forces use of non-UTF-8 progress bar chars on a terminal with UTF-8 support. 19 | END 20 | } 21 | 22 | error-text() { 23 | cat << END 24 | USAGE: kubectl watch (-h|--help) | [options] RESOURCE 25 | See kubectl watch --help for more information. 26 | END 27 | } 28 | 29 | # === 30 | 31 | if [[ $# = 0 ]] 32 | then 33 | error-text 34 | exit 0 35 | fi 36 | 37 | # Defaults 38 | USE_PROGRESS=1 39 | SIMPLE_PROGRESS=0 40 | 41 | total_calls=60 42 | sleeplen=1 43 | 44 | # check args 45 | while [[ $# > 0 ]] 46 | do 47 | case "$1" in 48 | -h|--help) 49 | help-text 50 | exit 0 51 | ;; 52 | -c|--calls) 53 | shift 54 | if [[ $# > 0 && $1 =~ ^[0-9]$ ]] 55 | then 56 | total_calls=$1 57 | if [[ $total_calls = 0 ]]; then USE_PROGRESS=0; fi 58 | shift 59 | else 60 | error-text 61 | exit 0 62 | fi 63 | ;; 64 | -s|--sleep) 65 | shift 66 | if [[ $# > 0 && $1 =~ ^[0-9]$ ]] 67 | then 68 | sleeplen=$1 69 | shift 70 | else 71 | error-text 72 | exit 0 73 | fi 74 | ;; 75 | --simple) 76 | shift 77 | SIMPLE_PROGRESS=1 78 | ;; 79 | *) 80 | break 81 | ;; 82 | esac 83 | done 84 | 85 | # progress bar & output width vars 86 | calls_left=$total_calls 87 | 88 | prog_full="\u25A0" 89 | pad=$( printf "\u25A1%.0s" $( seq 1 $calls_left ) ) 90 | 91 | if [[ ! $(echo -en $prog_full | wc -m) = 1 || $SIMPLE_PROGRESS = 1 ]] 92 | then 93 | prog_full="#" 94 | pad=$( printf ".%.0s" $( seq 1 $calls_left ) ) 95 | fi 96 | 97 | byte_size=$(echo -en $prog_full | wc -c) 98 | progress="" 99 | 100 | while true 101 | do 102 | # exit check 103 | if [[ $USE_PROGRESS = 1 && $calls_left = 0 ]] 104 | then 105 | exit 0 106 | fi 107 | 108 | columns=$(tput cols) 109 | 110 | response=$(kubectl get $@ 2>&1) 111 | 112 | # read response into array of lines 113 | lines=() 114 | while IFS=\n read line 115 | do 116 | lines+=($line) 117 | done <<< $response 118 | 119 | # backtrack to the top of the output 120 | for (( ; numlines>0; numlines-- )) 121 | do 122 | echo -en "\r\e[1A\e[K" 123 | done 124 | 125 | # print the new output 126 | echo -e "$response" 127 | 128 | # create & print progress bar 129 | if [[ $USE_PROGRESS = 1 ]] 130 | then 131 | progress="$progress$prog_full" 132 | printf "[%b%0.*b]\n" "$progress" $(( --calls_left * byte_size )) "$pad" 133 | fi 134 | 135 | # calculate number of output lines 136 | numlines=$(wc -l <<< $response) 137 | 138 | # account for output being wider than the screen 139 | for line in "${lines[@]}" 140 | do 141 | if (( $(wc -c <<< $line) > columns )) 142 | then 143 | (( numlines++ )) 144 | fi 145 | done 146 | 147 | if [[ $USE_PROGRESS = 1 ]] 148 | then 149 | (( numlines++ )) 150 | # account for progress bar being wider than the screen 151 | if (( total_calls + 2 > columns )) 152 | then 153 | (( numlines++ )) 154 | fi 155 | fi 156 | 157 | sleep $sleeplen 158 | done 159 | --------------------------------------------------------------------------------