From 7a11ee796f1c7d41a76c412da315091ceb851cee Mon Sep 17 00:00:00 2001 From: grzesuav Date: Thu, 11 Apr 2019 00:57:46 +0200 Subject: [PATCH 1/7] feat: Support for zsh --- kubectl-prompt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kubectl-prompt b/kubectl-prompt index ac41ee7..b3c6e88 100755 --- a/kubectl-prompt +++ b/kubectl-prompt @@ -70,7 +70,7 @@ function add_prompt() { # Add function to check user context when kubectl is invoked. if [[ ! -z "$NAMESPACE" ]] || [[ ! -z "$CLUSTER" ]]; then ex '+g/function kubectl/d' -cwq ~/.bash_profile - echo 'function kubectl() { kube=$(which kubectl); $kube prompt "${@}" && command $kube "${@}" ; }' >> ~/.bash_profile + echo 'function kubectl() { command kubectl prompt "$@" && command kubectl "$@"' >> ~/.bash_profile exit 0 fi } From c28c9042edaaef239e5b42d9e8f857063edbf6e1 Mon Sep 17 00:00:00 2001 From: Jefferson Faseler Date: Thu, 8 Oct 2020 21:05:15 -0500 Subject: [PATCH 2/7] Dynamically install plugin --- install-plugins.sh | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/install-plugins.sh b/install-plugins.sh index 7de1717..27b421c 100755 --- a/install-plugins.sh +++ b/install-plugins.sh @@ -1,17 +1,28 @@ #!/bin/bash +case $SHELL in + *zsh* ) + profile=.zshrc + ;; + * ) + profile=.bash_profile + ;; +esac + +echo "Detected shell: $SHELL" +echo "Installing kubectl-plugins to ~/$profile" # Move plugins and add location to path (required for kubectl 1.12+) chmod +x ./kubectl-* rm -rf ~/.kube/plugins/jordanwilson230 mkdir -p ~/.kube/plugins/jordanwilson230 cp -r ./kubectl-* ~/.kube/plugins/jordanwilson230/ -grep 'PATH=$PATH:~/.kube/plugins/jordanwilson230' ~/.bash_profile 1>/dev/null -[ $? -ne 0 ] && echo 'export PATH=$PATH:~/.kube/plugins/jordanwilson230' >> ~/.bash_profile +grep 'PATH=$PATH:~/.kube/plugins/jordanwilson230' ~/$profile 1>/dev/null +[ $? -ne 0 ] && echo 'export PATH=$PATH:~/.kube/plugins/jordanwilson230' >> ~/$profile # Removes old installer function if exists -ex '+g/function kubectl()/d' -cwq ~/.bash_profile >/dev/null 2>&1 +ex '+g/function kubectl()/d' -cwq ~/$profile >/dev/null 2>&1 # Finished -echo -e "\nDone.\nPlease open a new terminal or run: source ~/.bash_profile\n" +echo -e "\nDone.\nPlease open a new terminal or run: source ~/$profile\n" kubectl plugin list --name-only | sed 's|-| |g' From ff7546959268240b8e7066447c4170ad7802aff4 Mon Sep 17 00:00:00 2001 From: Jefferson Faseler Date: Thu, 8 Oct 2020 21:06:05 -0500 Subject: [PATCH 3/7] Abstract dynamic shell discovery away for other references to profile --- install-plugins.sh | 9 +-------- which-shell.sh | 8 ++++++++ 2 files changed, 9 insertions(+), 8 deletions(-) create mode 100644 which-shell.sh diff --git a/install-plugins.sh b/install-plugins.sh index 27b421c..35633f7 100755 --- a/install-plugins.sh +++ b/install-plugins.sh @@ -1,13 +1,6 @@ #!/bin/bash -case $SHELL in - *zsh* ) - profile=.zshrc - ;; - * ) - profile=.bash_profile - ;; -esac +profile=$(. ./which-shell.sh) echo "Detected shell: $SHELL" echo "Installing kubectl-plugins to ~/$profile" diff --git a/which-shell.sh b/which-shell.sh new file mode 100644 index 0000000..5327177 --- /dev/null +++ b/which-shell.sh @@ -0,0 +1,8 @@ +case $SHELL in + *zsh* ) + echo .zshrc + ;; + * ) + echo .bash_profile + ;; +esac From b9981db465cdafd64f45ac316341b46133e93808 Mon Sep 17 00:00:00 2001 From: Jefferson Faseler Date: Thu, 8 Oct 2020 21:55:25 -0500 Subject: [PATCH 4/7] Replace mentions of bash_profile with dynamic shell profile --- README.md | 25 +++++++++++++++++++++++++ install-plugins.sh | 12 ++++++------ kubectl-image | 9 +++++---- kubectl-prompt | 23 ++++++++++++----------- 4 files changed, 48 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 2aef649..41a62f9 100644 --- a/README.md +++ b/README.md @@ -10,13 +10,23 @@ A collection of plugins for kubectl integration (for Kubectl versions >= 1.12.0) - All coding was written to maintain compatibility across both BSD and GNU. ## Install on Linux/Mac +### bash ```bash git clone https://github.com/jordanwilson230/kubectl-plugins.git cd kubectl-plugins ./install-plugins.sh source ~/.bash_profile ``` + +### Zsh +```zsh +git clone https://github.com/jordanwilson230/kubectl-plugins.git +cd kubectl-plugins +./install-plugins.sh +source ~/.zshrc +``` #### To Uninstall +##### bash ``` rm -rf ~/.kube/plugins/jordanwilson230 ex '+g/jordanwilson230/d' -cwq ~/.bash_profile @@ -31,6 +41,21 @@ ex '+g/function kubectl()/d' -cwq ~/.bash_profile ex '+g/KUBECTL_\(.*\)_PROMPT/d' -cwq ~/.bash_profile ``` +##### Zsh +``` +rm -rf ~/.kube/plugins/jordanwilson230 +ex '+g/jordanwilson230/d' -cwq ~/.zshrc +``` +Remove the ```image``` plugin: +``` +ex '+g/IMG_REGISTRY=/d' -cwq ~/.zshrc +``` +Remove the ```prompt``` plugin: +``` +ex '+g/function kubectl()/d' -cwq ~/.zshrc +ex '+g/KUBECTL_\(.*\)_PROMPT/d' -cwq ~/.zshrc +``` + ### kubectl ssh ![kapssh](https://user-images.githubusercontent.com/22456127/46683069-4152c100-cbbd-11e8-9db5-9fb319bb320b.gif) diff --git a/install-plugins.sh b/install-plugins.sh index 35633f7..8f46c49 100755 --- a/install-plugins.sh +++ b/install-plugins.sh @@ -1,21 +1,21 @@ #!/bin/bash -profile=$(. ./which-shell.sh) +SHELL_PROFILE=$(. ./which-shell.sh) echo "Detected shell: $SHELL" -echo "Installing kubectl-plugins to ~/$profile" +echo "Installing kubectl-plugins to ~/${SHELL_PROFILE}" # Move plugins and add location to path (required for kubectl 1.12+) chmod +x ./kubectl-* rm -rf ~/.kube/plugins/jordanwilson230 mkdir -p ~/.kube/plugins/jordanwilson230 cp -r ./kubectl-* ~/.kube/plugins/jordanwilson230/ -grep 'PATH=$PATH:~/.kube/plugins/jordanwilson230' ~/$profile 1>/dev/null -[ $? -ne 0 ] && echo 'export PATH=$PATH:~/.kube/plugins/jordanwilson230' >> ~/$profile +grep 'PATH=$PATH:~/.kube/plugins/jordanwilson230' ~/${SHELL_PROFILE} 1>/dev/null +[ $? -ne 0 ] && echo 'export PATH=$PATH:~/.kube/plugins/jordanwilson230' >> ~/${SHELL_PROFILE} # Removes old installer function if exists -ex '+g/function kubectl()/d' -cwq ~/$profile >/dev/null 2>&1 +ex '+g/function kubectl()/d' -cwq ~/${SHELL_PROFILE} >/dev/null 2>&1 # Finished -echo -e "\nDone.\nPlease open a new terminal or run: source ~/$profile\n" +echo -e "\nDone.\nPlease open a new terminal or run: source ~/${SHELL_PROFILE}\n" kubectl plugin list --name-only | sed 's|-| |g' diff --git a/kubectl-image b/kubectl-image index 4fac19c..f1cdcee 100755 --- a/kubectl-image +++ b/kubectl-image @@ -1,5 +1,6 @@ #!/bin/bash +SHELL_PROFILE=$(. ./which-shell.sh) usage() { echo -e "Search for docker images.\nUsage: kubectl img -i " && grep " .)\ #" $0; exit 0; } [ $# -eq 0 ] && echo -e "\nERROR: Missing image name [-i].\n" && usage @@ -15,9 +16,9 @@ while getopts ":i:cph" arg; do read -p "Save ${REGISTRY} for future reference? [y/n]: " save if [[ "$save" == "y" ]]; then # Overwrite or add default registry - ex '+g/IMG_REGISTRY=/d' -cwq ~/.bash_profile - echo 'export IMG_REGISTRY="'$REGISTRY'"' >> ~/.bash_profile - echo -e "\nSaved to ~/.bash_profile. Source or open a new terminal to take effect." + ex '+g/IMG_REGISTRY=/d' -cwq ~/${SHELL_PROFILE} + echo 'export IMG_REGISTRY="'$REGISTRY'"' >> ~/${SHELL_PROFILE} + echo -e "\nSaved to ~/${SHELL_PROFILE}. Source or open a new terminal to take effect." fi IMG_REGISTRY="$REGISTRY" ;; @@ -58,7 +59,7 @@ if [[ "$IMAGE" ]]; then echo -e "\nSearching Docker Hub for $IMAGE." available_images=($(docker search --limit=25 "${REGISTRY}/$IMAGE" | sed -e 's|TAG||g; s|\[OK\]||g; s|AUTOMATED||g; s|OFFICIAL||g' | sed 's/[[:blank:]]*$//')) [ "${#available_images[@]}" -le 1 ] && echo -e "\nNo images found for $IMAGE.\n" && exit 1 - count=0 + count=0 ;; esac diff --git a/kubectl-prompt b/kubectl-prompt index b3c6e88..eccf63d 100755 --- a/kubectl-prompt +++ b/kubectl-prompt @@ -5,6 +5,7 @@ LBLUE=$(echo -en '\033[01;34m') YELLOW=$(echo -en '\033[00;33m') RESTORE=$(echo -en '\033[0m') +SHELL_PROFILE=$(. ./which-shell.sh) COMMAND="${@}" export COMMAND="$COMMAND" @@ -54,23 +55,23 @@ function add_prompt() { if [[ "$NAMESPACE" ]]; then read -p "${YELLOW}* Add the namespace ${NAMESPACE} to prompt list [y/n]? ${RESTORE}" answer [[ "$answer" != "y" ]] && exit 0 - echo -e "\n${YELLOW}* Adding ${CLUSTER}${NAMESPACE} to \${KUBECTL_NAMESPACE_PROMPT} in ~/.bash_profile\n- The changes will be applied after starting a new terminal session or after running: source ~/.bash_profile ${RESTORE}" + echo -e "\n${YELLOW}* Adding ${CLUSTER}${NAMESPACE} to \${KUBECTL_NAMESPACE_PROMPT} in ~/${SHELL_PROFILE}\n- The changes will be applied after starting a new terminal session or after running: source ~/${SHELL_PROFILE} ${RESTORE}" KUBECTL_NAMESPACE_PROMPT="${KUBECTL_NAMESPACE_PROMPT[@]} ${NAMESPACE}" - ex '+g/KUBECTL_NAMESPACE_PROMPT=/d' -cwq ~/.bash_profile - echo "export KUBECTL_NAMESPACE_PROMPT=\"${KUBECTL_NAMESPACE_PROMPT}\"" >> ~/.bash_profile + ex '+g/KUBECTL_NAMESPACE_PROMPT=/d' -cwq ~/${SHELL_PROFILE} + echo "export KUBECTL_NAMESPACE_PROMPT=\"${KUBECTL_NAMESPACE_PROMPT}\"" >> ~/${SHELL_PROFILE} elif [[ "$CLUSTER" ]]; then read -p "${YELLOW}* Add the cluster ${CLUSTER} to prompt list [y/n]? ${RESTORE}" answer [[ "$answer" != "y" ]] && exit 0 - echo -e "\n${YELLOW}* Adding ${CLUSTER} to \${KUBECTL_CLUSTER_PROMPT} in ~/.bash_profile\n- The changes will be applied after starting a new terminal session or after running: source ~/.bash_profile ${RESTORE}" + echo -e "\n${YELLOW}* Adding ${CLUSTER} to \${KUBECTL_CLUSTER_PROMPT} in ~/${SHELL_PROFILE}\n- The changes will be applied after starting a new terminal session or after running: source ~/${SHELL_PROFILE} ${RESTORE}" KUBECTL_CLUSTER_PROMPT+="${CLUSTER/%/ }" - ex '+g/KUBECTL_CLUSTER_PROMPT=/d' -cwq ~/.bash_profile - echo "export KUBECTL_CLUSTER_PROMPT=\"${KUBECTL_CLUSTER_PROMPT}\"" >> ~/.bash_profile + ex '+g/KUBECTL_CLUSTER_PROMPT=/d' -cwq ~/${SHELL_PROFILE} + echo "export KUBECTL_CLUSTER_PROMPT=\"${KUBECTL_CLUSTER_PROMPT}\"" >> ~/${SHELL_PROFILE} fi # Add function to check user context when kubectl is invoked. if [[ ! -z "$NAMESPACE" ]] || [[ ! -z "$CLUSTER" ]]; then - ex '+g/function kubectl/d' -cwq ~/.bash_profile - echo 'function kubectl() { command kubectl prompt "$@" && command kubectl "$@"' >> ~/.bash_profile + ex '+g/function kubectl/d' -cwq ~/${SHELL_PROFILE} + echo 'function kubectl() { command kubectl prompt "$@" && command kubectl "$@"' >> ~/${SHELL_PROFILE} exit 0 fi } @@ -79,9 +80,9 @@ function add_prompt() { ### Clear prompt list function remove_prompt() { - ex '+g/KUBECTL_NAMESPACE_PROMPT=/d' -cwq ~/.bash_profile - ex '+g/KUBECTL_CLUSTER_PROMPT=/d' -cwq ~/.bash_profile - ex '+g/function kubectl/d' -cwq ~/.bash_profile + ex '+g/KUBECTL_NAMESPACE_PROMPT=/d' -cwq ~/${SHELL_PROFILE} + ex '+g/KUBECTL_CLUSTER_PROMPT=/d' -cwq ~/${SHELL_PROFILE} + ex '+g/function kubectl/d' -cwq ~/${SHELL_PROFILE} echo -e "\nCleared prompt list.\nPlease open a new terminal for it to take affect.\n" exit 0 } From 628c496c959abb2893f808b1cc8c5f6c8a141b0b Mon Sep 17 00:00:00 2001 From: Jefferson Faseler Date: Thu, 8 Oct 2020 22:25:53 -0500 Subject: [PATCH 5/7] Fix typo in prompt check --- kubectl-prompt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kubectl-prompt b/kubectl-prompt index eccf63d..5105875 100755 --- a/kubectl-prompt +++ b/kubectl-prompt @@ -71,7 +71,7 @@ function add_prompt() { # Add function to check user context when kubectl is invoked. if [[ ! -z "$NAMESPACE" ]] || [[ ! -z "$CLUSTER" ]]; then ex '+g/function kubectl/d' -cwq ~/${SHELL_PROFILE} - echo 'function kubectl() { command kubectl prompt "$@" && command kubectl "$@"' >> ~/${SHELL_PROFILE} + echo 'function kubectl() { command kubectl prompt "$@" && command kubectl "$@" }' >> ~/${SHELL_PROFILE} exit 0 fi } From f8ebf8bf2562b0e65a63f1134265b773448dc944 Mon Sep 17 00:00:00 2001 From: Jefferson Faseler Date: Fri, 9 Oct 2020 09:31:07 -0500 Subject: [PATCH 6/7] Fix missing script bug and update permissions --- install-plugins.sh | 1 + which-shell.sh | 0 2 files changed, 1 insertion(+) mode change 100644 => 100755 which-shell.sh diff --git a/install-plugins.sh b/install-plugins.sh index 8f46c49..4e3a5b3 100755 --- a/install-plugins.sh +++ b/install-plugins.sh @@ -10,6 +10,7 @@ chmod +x ./kubectl-* rm -rf ~/.kube/plugins/jordanwilson230 mkdir -p ~/.kube/plugins/jordanwilson230 cp -r ./kubectl-* ~/.kube/plugins/jordanwilson230/ +cp ./which-shell.sh ~/.kube/plugins/jordanwilson230/ grep 'PATH=$PATH:~/.kube/plugins/jordanwilson230' ~/${SHELL_PROFILE} 1>/dev/null [ $? -ne 0 ] && echo 'export PATH=$PATH:~/.kube/plugins/jordanwilson230' >> ~/${SHELL_PROFILE} diff --git a/which-shell.sh b/which-shell.sh old mode 100644 new mode 100755 From 4b438a9786ae019d1ae10bf15dc02cc0209e9941 Mon Sep 17 00:00:00 2001 From: Jefferson Faseler Date: Wed, 14 Oct 2020 08:21:48 -0500 Subject: [PATCH 7/7] Use absolute path for the shell discovery function --- kubectl-image | 2 +- kubectl-prompt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/kubectl-image b/kubectl-image index 8e604f6..6176380 100755 --- a/kubectl-image +++ b/kubectl-image @@ -1,6 +1,6 @@ #!/bin/bash -SHELL_PROFILE=$(. ./which-shell.sh) +SHELL_PROFILE=$(. ~/.kube/plugins/jordanwilson230/which-shell.sh) usage() { echo -e "Search for docker images.\nUsage: kubectl img -i " && grep " .)\ #" $0; exit 0; } [ $# -eq 0 ] && echo -e "\nERROR: Missing image name [-i].\n" && usage diff --git a/kubectl-prompt b/kubectl-prompt index 5105875..972513b 100755 --- a/kubectl-prompt +++ b/kubectl-prompt @@ -5,7 +5,7 @@ LBLUE=$(echo -en '\033[01;34m') YELLOW=$(echo -en '\033[00;33m') RESTORE=$(echo -en '\033[0m') -SHELL_PROFILE=$(. ./which-shell.sh) +SHELL_PROFILE=$(. ~/.kube/plugins/jordanwilson230/which-shell.sh) COMMAND="${@}" export COMMAND="$COMMAND"