├── LICENSE ├── README.md ├── bin ├── __common ├── kcopy ├── kctx ├── kexec ├── klogs ├── kns ├── kpod └── ktools ├── completion ├── __completion └── kubernetes-tools.fish └── gif ├── kcopy.gif ├── kctx.gif ├── kexec.gif ├── kns.gif ├── kpod.gif └── ktools.gif /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Shawn Wang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Kubernetes Tools 2 | 3 | Kubernetes Tools is a set of scripts that simplifies daily Kubernetes operations. 4 | 5 | #### Available tools 6 | 7 | **kctx**: List contexts, switch context 8 | 9 | ![](gif/kctx.gif) 10 | 11 | **kns**: List namespaces, select default namespace 12 | 13 | ![](gif/kns.gif) 14 | 15 | **kpod**: List pods in current namespace, describe pod 16 | 17 | ![](gif/kpod.gif) 18 | 19 | **kexec**: Get a shell of a selected container 20 | 21 | ![](gif/kexec.gif) 22 | 23 | **kcopy**: Copy common cli tools to selected container 24 | 25 | ![](gif/kcopy.gif) 26 | 27 | #### How to use 28 | To list all the available tools, run ```ktools``` 29 | 30 | For usage of each tool, run ```[tool_name] -h``` 31 | 32 | ![](gif/ktools.gif) 33 | 34 | ## Installation 35 | 36 | ### Install using brew 37 | ```sh 38 | brew tap shawnxlw/homebrew-tap 39 | brew install kubernetes-tools 40 | ktools --init 41 | ``` 42 | 43 | ### Manual installation 44 | ```sh 45 | cd ~ 46 | git clone https://github.com/shawnxlw/kubernetes-tools 47 | # add the follow to your .bash_profile or .zshrc 48 | PATH=$HOME/kubernetes-tools/bin:$PATH 49 | # set up tab completion 50 | ktools --init 51 | ``` 52 | 53 | ##### BASH completion 54 | Add the following into your `.bash_profile`: 55 | ```sh 56 | source $HOME/kubernetes-tools/completion/__completion 57 | ``` 58 | 59 | ##### ZSH completion 60 | Add the following into your `.zshrc`: 61 | ```sh 62 | autoload -U compaudit compinit bashcompinit 63 | compaudit && compinit && bashcompinit 64 | source $HOME/kubernetes-tools/completion/__completion 65 | ``` 66 | 67 | ## License 68 | This software is licensed under the [MIT License](https://opensource.org/licenses/MIT). 69 | 70 | ## Release Note 71 | v2.1.0 - 26/10/2018 72 | - Added klogs 73 | - Added fish-shell completions (PR by @nesl247) 74 | 75 | v2.0.1 - 23/10/2018 76 | - Updated kcp from binary copy to download 77 | - Fixed pod name auto completion 78 | 79 | v2.0.0 - 15/10/2018 80 | - Changed to developer workflow 81 | - Updated kctx, kns, kpod, kcopy, kctx 82 | - Removed kbak, kds, klogs 83 | 84 | v1.2.0 - 08/08/2018 85 | - Updated kexec, kns, kpod, kctx, kds 86 | - Added kcp 87 | 88 | v1.0.0 - 07/08/2017 89 | - Initial release 90 | -------------------------------------------------------------------------------- /bin/__common: -------------------------------------------------------------------------------- 1 | __style_end="\033[0m" 2 | __style_red="\033[31m" 3 | __style_green="\033[32m" 4 | __style_blue="\033[34m" 5 | __style_yellow="\033[33m" 6 | __style_cyan="\033[36m" 7 | __style_bold="\033[1m" 8 | __style_dim="\033[2m" 9 | __style_underlined="\033[4m" 10 | __style_inverted="\033[7m" 11 | 12 | __app_name="Kubernetes Tools" 13 | __version="v2.1.0" 14 | __title="$__style_yellow$__app_name $__version - $(basename -- $0)$__style_end" 15 | 16 | __current_namespace() { 17 | namespace="$(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"$(current_context)\")].context.namespace}")" 18 | if [[ -z "${namespace}" ]]; then 19 | echo "default" 20 | else 21 | echo "${namespace}" 22 | fi 23 | } -------------------------------------------------------------------------------- /bin/kcopy: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Kubernetes Tools - Copyright (C) 2017 Shawn Wang 4 | # https://github.com/shawnxlw/kubernetes-tools 5 | # 6 | # You may use, distribute or modify this software under the terms of the MIT license. 7 | BUSY_BOX_URL="https://busybox.net/downloads/binaries/1.27.1-i686/busybox" 8 | BUSY_BOX_SHA="b51b9328eb4e60748912e1c1867954a5cf7e9d5294781cae59ce225ed110523c" 9 | BUSY_BOX_PATH="/tmp/${BUSY_BOX_SHA}" 10 | 11 | source __common 12 | 13 | # if the busybox directory doesn't exist, download busybox 14 | function get_busybox() { 15 | if [[ ! -d $BUSY_BOX_PATH ]]; then 16 | echo "No busybox present, downloading..." 17 | mkdir $BUSY_BOX_PATH && cd $BUSY_BOX_PATH 18 | curl -sSo busybox $BUSY_BOX_URL 19 | fi 20 | } 21 | 22 | function copy_tools() { 23 | # check/download busybox 24 | get_busybox 25 | # checkout shasum 26 | echo "${BUSY_BOX_SHA} ${BUSY_BOX_PATH}/busybox" | shasum -ca 256 - > /dev/null 27 | 28 | pod=$1 29 | PS3="Please select a container:" 30 | # exit if pod not found 31 | if ! kubectl get pod $pod > /dev/null; then exit 1; fi 32 | 33 | containers=($(kubectl get pod $pod -o jsonpath='{.spec.containers[*].name}')) 34 | 35 | # if there is only one container, get a shell 36 | if [[ ${#containers[@]} -eq 1 ]] ; then 37 | selected_container=$containers 38 | else 39 | # if there are more than one container, prompt select 40 | select container in "${containers[@]}" 41 | do 42 | selected_container=$container 43 | break 44 | done 45 | fi 46 | 47 | # copy busybox 48 | dir=`dirname $0` 49 | echo $dir 50 | echo -e "\nCopying tools to container $selected_container...\n" 51 | kubectl cp $BUSY_BOX_PATH/busybox $namespace/$pod:/tmp -c $selected_container 52 | # setup busybox 53 | kubectl exec $pod -c $selected_container -ti -- sh -c "chmod 755 /tmp/busybox && /tmp/busybox --install /usr/bin" 54 | 55 | echo -e "\nGetting you a shell in $selected_container...\n" 56 | # if bash doesn't work, try sh 57 | kubectl exec $pod -c $selected_container -ti bash 2>/dev/null || kubectl exec $pod -c $selected_container -ti sh 58 | exit 0 59 | } 60 | 61 | # show help 62 | show_help() { 63 | echo -e $__title 64 | echo 65 | echo -e "- Copy common CLI tools to selected container in specified pod: ${__style_underlined}kcopy [pod_name]\n${__style_end}" 66 | } 67 | 68 | # parse arguments 69 | while [ "$1" != "" ]; do 70 | case $1 in 71 | -n | --namespace ) shift 72 | copy_tools $1 73 | ;; 74 | -h | --help ) shift 75 | show_help 76 | exit 0 77 | ;; 78 | * ) copy_tools $1 79 | esac 80 | shift 81 | done 82 | 83 | # prompt pod selection if no arguments 84 | if [ $# -eq 0 ]; then show_help; fi 85 | -------------------------------------------------------------------------------- /bin/kctx: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Kubernetes Tools - Copyright (C) 2017 Shawn Wang 4 | # https://github.com/shawnxlw/kubernetes-tools 5 | # 6 | # You may use, distribute or modify this software under the terms of the MIT license. 7 | 8 | source __common 9 | show_context() { 10 | echo -e "Current Context: \"$(kubectl config current-context)\"\n" 11 | echo "All Contexts:" 12 | kubectl config get-contexts 13 | } 14 | 15 | switch_context() { 16 | echo "Switching from \"$(kubectl config current-context)\" to \"$1\"" 17 | kubectl config use-context $1 18 | echo "Current Context: \"$(kubectl config current-context)\"" 19 | exit 0 20 | } 21 | 22 | # sohw help 23 | show_help() { 24 | echo -e $__title 25 | echo 26 | echo -e "- List current and all contexts: ${__style_underlined}kctx${__style_end}" 27 | echo -e "- Switch to specified [context]: ${__style_underlined}kctx [context]\n${__style_end}" 28 | } 29 | 30 | # parse arguments 31 | while [ "$1" != "" ]; do 32 | case $1 in 33 | -c | --context ) shift 34 | switch_context $1 35 | ;; 36 | -h | --help ) shift 37 | show_help 38 | exit 0 39 | ;; 40 | * ) switch_context $1 41 | esac 42 | shift 43 | done 44 | 45 | # show current context if no argument specified 46 | if [ $# -eq 0 ]; then show_context; fi -------------------------------------------------------------------------------- /bin/kexec: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Kubernetes Tools - Copyright (C) 2017 Shawn Wang 4 | # https://github.com/shawnxlw/kubernetes-tools 5 | # 6 | # You may use, distribute or modify this software under the terms of the MIT license. 7 | 8 | source __common 9 | get_shell() { 10 | pod=$1 11 | 12 | PS3="Please select a container:" 13 | containers=($(/bin/sh -c "kubectl get pod $pod -o jsonpath='{.spec.containers[*].name}'")) 14 | 15 | # if there is only one container, get a shell 16 | if [[ ${#containers[@]} -eq 1 ]] ; then 17 | selected_container=$containers 18 | else 19 | # if there are more than one container, prompt select 20 | select container in "${containers[@]}" 21 | do 22 | selected_container=$container 23 | break 24 | done 25 | fi 26 | 27 | echo -e "\nGetting you a shell in $selected_container...\n" 28 | # if bash doesn't work, try sh 29 | kubectl exec $pod -c $selected_container -ti -- bash 2>/dev/null || kubectl exec $pod -c $selected_container -ti -- sh 30 | exit 0 31 | 32 | } 33 | 34 | # sohw help 35 | show_help() { 36 | echo -e $__title 37 | echo 38 | echo -e "- Get a shell of a selected container: ${__style_underlined}kexec [pod_name]${__style_end}\n" 39 | } 40 | 41 | # parse arguments 42 | while [ "$1" != "" ]; do 43 | case $1 in 44 | -n | --namespace ) shift 45 | get_shell "$1" 46 | ;; 47 | -h | --help ) shift 48 | show_help 49 | exit 0 50 | ;; 51 | * ) get_shell "$1" 52 | esac 53 | shift 54 | done 55 | 56 | # show help if no arguments 57 | if [ $# -eq 0 ]; then show_help; fi -------------------------------------------------------------------------------- /bin/klogs: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Kubernetes Tools - Copyright (C) 2017 Shawn Wang 4 | # https://github.com/shawnxlw/kubernetes-tools 5 | # 6 | # You may use, distribute or modify this software under the terms of the MIT license. 7 | 8 | source __common 9 | get_logs() { 10 | pod=$1 11 | 12 | PS3="Please select a container:" 13 | containers=($(/bin/sh -c "kubectl get pod $pod -o jsonpath='{.spec.containers[*].name}'")) 14 | 15 | # if there is only one container, set it as selected container 16 | if [[ ${#containers[@]} -eq 1 ]] ; then 17 | selected_container=$containers 18 | else 19 | # if there are more than one container, prompt select 20 | select container in "${containers[@]}" 21 | do 22 | selected_container=$container 23 | break 24 | done 25 | fi 26 | 27 | echo -e "\nGetting you logs of $selected_container...\n" 28 | # get logs of the slected pod 29 | kubectl logs $pod -c $selected_container 30 | exit 0 31 | 32 | } 33 | 34 | # sohw help 35 | show_help() { 36 | echo -e $__title 37 | echo 38 | echo -e "- Get logs of a selected container: ${__style_underlined}klogs [pod_name]${__style_end}\n" 39 | } 40 | 41 | # parse arguments 42 | while [ "$1" != "" ]; do 43 | case $1 in 44 | -n | --namespace ) shift 45 | get_logs "$1" 46 | ;; 47 | -h | --help ) shift 48 | show_help 49 | exit 0 50 | ;; 51 | * ) get_logs "$1" 52 | esac 53 | shift 54 | done 55 | 56 | # show help if no arguments 57 | if [ $# -eq 0 ]; then show_help; fi 58 | -------------------------------------------------------------------------------- /bin/kns: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Kubernetes Tools - Copyright (C) 2017 Shawn Wang 4 | # https://github.com/shawnxlw/kubernetes-tools 5 | # 6 | # You may use, distribute or modify this software under the terms of the MIT license. 7 | 8 | source __common 9 | 10 | current_context() { 11 | kubectl config current-context 12 | } 13 | 14 | list_namespaces() { 15 | echo "Current Namespace: $(__current_namespace)" 16 | echo "Current Context: $(current_context)" 17 | echo 18 | echo "All namespaces:" 19 | kubectl get ns | sed ''/$(__current_namespace)/s//`printf "\033[32m$(__current_namespace)\033[0m"`/'' 20 | exit 21 | } 22 | 23 | # prompt ns select 24 | select_ns() { 25 | echo -e "Setting default namespace:\n" 26 | PS3="Please select a namespace:" 27 | OLD_IFS="$IFS" 28 | IFS=$'\n' 29 | NAMESPACES=($(kubectl get ns |awk '{print $1}'|sed '1d')) 30 | IFS="$OLD_IFS" 31 | 32 | select namespace in "${NAMESPACES[@]}" 33 | do 34 | update_context $namespace 35 | break 36 | done 37 | } 38 | 39 | # update context 40 | update_context() { 41 | echo -e "\nSetting $1 as default namespace in current context...\n" 42 | kubectl config set-context $(kubectl config current-context) --namespace=$1 43 | exit 44 | } 45 | 46 | # sohw help 47 | show_help() { 48 | echo -e $__title 49 | echo 50 | echo -e "- List current and all namespaces: ${__style_underlined}kns${__style_end}" 51 | echo -e "- Select a default namespace: ${__style_underlined}kns [-s|--select]${__style_end}" 52 | echo -e "- Specify a default namespace: ${__style_underlined}kns [namespace]${__style_end}\n" 53 | } 54 | 55 | # parse arguments 56 | while [ "$1" != "" ]; do 57 | case $1 in 58 | -n | --namespace ) shift 59 | update_context $1 60 | ;; 61 | -s | --select ) shift 62 | select_ns 63 | ;; 64 | -h | --help ) shift 65 | show_help 66 | exit 0 67 | ;; 68 | * ) update_context $1 69 | esac 70 | shift 71 | done 72 | 73 | # list namespaces if no arguments 74 | if [ $# -eq 0 ]; then list_namespaces; fi -------------------------------------------------------------------------------- /bin/kpod: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Kubernetes Tools - Copyright (C) 2017 Shawn Wang 4 | # https://github.com/shawnxlw/kubernetes-tools 5 | # 6 | # You may use, distribute or modify this software under the terms of the MIT license. 7 | 8 | source __common 9 | # get pods 10 | get_pods() { 11 | kubectl get pod $1 12 | exit 13 | } 14 | 15 | describe_pod() { 16 | kubectl describe pod $1 17 | exit 18 | } 19 | 20 | # sohw help 21 | show_help() { 22 | echo -e $__title 23 | echo 24 | echo -e "- Get pods in current namespace: ${__style_underlined}kpod${__style_end}" 25 | echo -e "- Describe pod: ${__style_underlined}kpod [pod_name]${__style_end}" 26 | echo -e "- Get pods in all namespaces: ${__style_underlined}kpod [-a]${__style_end}\n" 27 | } 28 | 29 | # parse arguments 30 | while [ "$1" != "" ]; do 31 | case $1 in 32 | -n | --namespace ) shift 33 | get_pods "-n $1" 34 | ;; 35 | -a | --all) shift 36 | get_pods "--all-namespaces" 37 | ;; 38 | -h | --help ) shift 39 | show_help 40 | exit 0 41 | ;; 42 | * ) describe_pod "$1" 43 | esac 44 | shift 45 | done 46 | 47 | # get pods in current namespace if no arguments specified 48 | if [ $# -eq 0 ]; then get_pods; fi -------------------------------------------------------------------------------- /bin/ktools: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Kubernetes Tools - Copyright (C) 2017 Shawn Wang 4 | # https://github.com/shawnxlw/kubernetes-tools 5 | # 6 | # You may use, distribute or modify this software under the terms of the MIT license. 7 | 8 | 9 | source __common 10 | function auto_completion() { 11 | echo "Adding bash completion" 12 | path=$1/completion/__completion 13 | 14 | if [[ $SHELL == *"zsh"* ]]; then 15 | echo -e "\nZSH detected. The following lines will be added to $HOME/.zshrc" 16 | echo -e " autoload -U compaudit compinit bashcompinit\n compaudit && compinit && bashcompinit\n source $path\n" 17 | read -p "Press [Enter] to continue..." 18 | 19 | echo -e "\n#Kubernetes Tools zsh completion start\nautoload -U compaudit compinit bashcompinit\ncompaudit && compinit && bashcompinit\nsource $path\n#Kubernetes Tools zsh completion end\n" >> $HOME/.zshrc 20 | echo -e "\n.zshrc updated. Start a new shell to enjoy tab completion!" 21 | elif [[ $SHELL == *"bash"* ]]; then 22 | echo -e "\nBASH detected. The following lines will be added to $HOME/.bashrc" 23 | echo -e " source $path\n" 24 | read -p "Press [Enter] to continue..." 25 | 26 | echo -e "\n#Kubernetes Tools zsh completion start\nsource $path\n#Kubernetes Tools zsh completion end\n" >> $HOME/.bashrc 27 | echo -e "\n.bashrc updated. Start a new shell to enjoy tab completion!" 28 | elif [[ $SHELL = *"fish"* ]]; then 29 | source_path=$1/completion/kubernetes-tools.fish 30 | destination_path=$HOME/.config/fish/completions/kubernetes-tools.fish 31 | 32 | echo -e "\nFISH detected. The following lines will be added to $HOME/.config/fish/config.fish" 33 | echo -e " source $destination_path\n" 34 | read -p "Press [Enter] to continue..." 35 | 36 | cp $source_path $destination_path 37 | echo -e "\n# Kubernetes Tools fish completion start\nsource $destination_path\n" >> $HOME/.config/fish/config.fish 38 | echo -e "\n.config.fish updated." 39 | source $destination_path 40 | else 41 | echo "Only BASH, FISH and ZSH are supported at this stage. Sorry." 42 | fi 43 | } 44 | 45 | function init() { 46 | path=$(greadlink -f "$0") 47 | app_root=$(dirname $(dirname ${path:-"$0"})) 48 | 49 | # set up tab completion 50 | auto_completion $app_root 51 | } 52 | 53 | # sohw help 54 | show_help() { 55 | echo -e $__title 56 | echo 57 | echo -e "- List all tools: ${__style_underlined}ktools${__style_end}" 58 | echo -e "- Setup auto completion: ${__style_underlined}ktools [-i|--init]${__style_end}" 59 | echo 60 | echo -e "${__style_green}kctx${__style_end}: List contexts, switch context" 61 | echo -e "${__style_green}kns${__style_end}: List namespaces, select default namespace" 62 | echo -e "${__style_green}kpod${__style_end}: List pods in current namespace, describe pod" 63 | echo -e "${__style_green}kexec${__style_end}: Get a shell of a selected container" 64 | echo -e "${__style_green}klogs${__style_end}: Get logs of a selected container" 65 | echo -e "${__style_green}kcopy${__style_end}: Copy common cli tools to selected container" 66 | echo 67 | echo -e "For usage of each tool, run ${__style_underlined}[tool_name] -h${__style_end}\n" 68 | } 69 | 70 | # parse arguments 71 | while [ "$1" != "" ]; do 72 | case $1 in 73 | -i | --init ) shift 74 | init 75 | exit 76 | ;; 77 | -h | --help ) shift 78 | show_help 79 | exit 80 | ;; 81 | * ) exit 0 82 | esac 83 | shift 84 | done 85 | 86 | # show help message if no arguments specified 87 | if [ $# -eq 0 ]; then show_help; fi 88 | -------------------------------------------------------------------------------- /completion/__completion: -------------------------------------------------------------------------------- 1 | current_namespace() { 2 | namespace="$(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"$(kubectl config current-context)\")].context.namespace}")" 3 | if [[ -z "${namespace}" ]]; then 4 | echo default 5 | else 6 | echo $namespace 7 | fi 8 | } 9 | 10 | __pods () 11 | { 12 | # pointer to current completion word. 13 | local cur 14 | 15 | # array variable storing the possible completions. 16 | COMPREPLY=() 17 | cur=${COMP_WORDS[COMP_CWORD]} 18 | namespaces=$(kubectl get pod -n $(current_namespace)|sed '1d'|awk '{print $1}') 19 | 20 | COMPREPLY=( $( compgen -W "$namespaces" -- $cur ) ) 21 | 22 | case "$cur" in 23 | -*) 24 | # generate the completion matches and load them into $COMPREPLY array. 25 | COMPREPLY=( $( compgen -W '-h -s' -- $cur ) );; 26 | esac 27 | 28 | return 0 29 | } 30 | 31 | __namespaces () 32 | { 33 | # pointer to current completion word. 34 | local cur 35 | 36 | # array variable storing the possible completions. 37 | COMPREPLY=() 38 | cur=${COMP_WORDS[COMP_CWORD]} 39 | namespaces=$(kubectl get ns |sed '1d'|awk '{print $1}') 40 | 41 | COMPREPLY=( $( compgen -W "$namespaces" -- $cur ) ) 42 | 43 | case "$cur" in 44 | -*) 45 | # generate the completion matches and load them into $COMPREPLY array. 46 | COMPREPLY=( $( compgen -W '-h -s' -- $cur ) );; 47 | esac 48 | 49 | return 0 50 | } 51 | 52 | __contexts () 53 | { 54 | # pointer to current completion word. 55 | local cur 56 | 57 | # array variable storing the possible completions. 58 | COMPREPLY=() 59 | cur=${COMP_WORDS[COMP_CWORD]} 60 | contexts=$(kubectl config get-contexts |sed -e "s/\*//"|sed '1d'|awk '{print $1}') 61 | 62 | COMPREPLY=( $( compgen -W "$contexts" -- $cur ) ) 63 | 64 | case "$cur" in 65 | -*) 66 | # generate the completion matches and load them into $COMPREPLY array. 67 | COMPREPLY=( $( compgen -W '-h -s' -- $cur ) );; 68 | esac 69 | 70 | return 0 71 | } 72 | 73 | __nodes () 74 | { 75 | # pointer to current completion word. 76 | local cur 77 | 78 | # array variable storing the possible completions. 79 | COMPREPLY=() 80 | cur=${COMP_WORDS[COMP_CWORD]} 81 | ips=$(kubectl get nodes -o json | jq -r '.items[].status.addresses[].address' | paste - - - | cut -f 1) 82 | 83 | COMPREPLY=( $( compgen -W "$ips" -- $cur ) ) 84 | 85 | case "$cur" in 86 | -*) 87 | # generate the completion matches and load them into $COMPREPLY array. 88 | COMPREPLY=( $( compgen -W '-h -s' -- $cur ) );; 89 | esac 90 | 91 | return 0 92 | } 93 | 94 | # apply autocompletions 95 | complete -F __pods klogs kexec kcopy kpod 96 | complete -F __namespaces kns 97 | complete -F __contexts kctx 98 | 99 | # remove autocompletion cache files e.g. .zcompdump after making changes -------------------------------------------------------------------------------- /completion/kubernetes-tools.fish: -------------------------------------------------------------------------------- 1 | function __ktools_current_namespace 2 | set -l current_context (kubectl config current-context) 3 | set -l namespace (kubectl config view -o=jsonpath="{.contexts[?(@.name==\"$current_context\")].context.namespace}") 4 | 5 | if test -z "$namespace" 6 | echo default 7 | else 8 | echo $namespace 9 | end 10 | end 11 | 12 | function __ktools_pods 13 | set -l pods (kubectl get pods -n (__ktools_current_namespace) --ignore-not-found | sed '1d' | awk '{print $1}') 14 | 15 | if set -q pods[1] 16 | printf "%s\tPod\n" $pods 17 | end 18 | end 19 | 20 | function __ktools_namespaces 21 | printf "%s\tNamespace\n" (kubectl get ns | sed '1d'| awk '{print $1}') 22 | end 23 | 24 | function __ktools_contexts 25 | printf "%s\tContext\n" (kubectl config get-contexts | sed -e "s/\*//"| sed '1d'| awk '{print $1}') 26 | end 27 | 28 | function __ktools_nodes 29 | printf "%s\tNode\n" (kubectl get nodes -o json | jq -r '.items[].status.addresses[].address' | paste - - - | cut -f 1) 30 | end 31 | 32 | complete -c klogs -f -a '(__ktools_pods)' 33 | complete -c kcopy -f -a '(__ktools_pods)' 34 | complete -c kexec -f -a '(__ktools_pods)' 35 | complete -c kpod -f -a '(__ktools_pods)' 36 | complete -c kns -f -a '(__ktools_namespaces)' 37 | complete -c kctx -f -a '(__ktools_contexts)' 38 | -------------------------------------------------------------------------------- /gif/kcopy.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shawnsw/kubernetes-tools/6b716e1806c9e9c28f09aa62b86a0e9d0d625f2d/gif/kcopy.gif -------------------------------------------------------------------------------- /gif/kctx.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shawnsw/kubernetes-tools/6b716e1806c9e9c28f09aa62b86a0e9d0d625f2d/gif/kctx.gif -------------------------------------------------------------------------------- /gif/kexec.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shawnsw/kubernetes-tools/6b716e1806c9e9c28f09aa62b86a0e9d0d625f2d/gif/kexec.gif -------------------------------------------------------------------------------- /gif/kns.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shawnsw/kubernetes-tools/6b716e1806c9e9c28f09aa62b86a0e9d0d625f2d/gif/kns.gif -------------------------------------------------------------------------------- /gif/kpod.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shawnsw/kubernetes-tools/6b716e1806c9e9c28f09aa62b86a0e9d0d625f2d/gif/kpod.gif -------------------------------------------------------------------------------- /gif/ktools.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/shawnsw/kubernetes-tools/6b716e1806c9e9c28f09aa62b86a0e9d0d625f2d/gif/ktools.gif --------------------------------------------------------------------------------