Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kubectl binary search #145

Open
Crazy-Hopper opened this issue Jul 25, 2023 · 4 comments
Open

kubectl binary search #145

Crazy-Hopper opened this issue Jul 25, 2023 · 4 comments

Comments

@Crazy-Hopper
Copy link

Hi!

Script fails to find kubectl binary in case path hashing is disabled with set +o hashall or set +h.

@johanhaleby
Copy link
Owner

johanhaleby commented Jul 26, 2023 via email

@Crazy-Hopper
Copy link
Author

Crazy-Hopper commented Jul 26, 2023

Mmm.. I guess we have two options. Either switch from using hash function to something else like whereis or which, or add a set -o hashall in the beginning of the script.

@johanhaleby
Copy link
Owner

@Crazy-Hopper I'm not familiar with this, unfortunately. Do you know if this will have any other impact? Will it be backward-compatible?

@Crazy-Hopper
Copy link
Author

Crazy-Hopper commented Aug 17, 2023

It seems to me that set -o hashall would be the easiest way, i.e. least changes to the current code. Given that man page for this option says it's enabled by default, turning this option on won't change anything at all for most of the users. It will only fix the script for those who voluntarily disabled it for their shells.

But frankly, I'd rewrite the initial code as follows:

diff --git a/kubetail b/kubetail
index 9c7464b..d34ab38 100755
--- a/kubetail
+++ b/kubetail
@@ -1,15 +1,15 @@
 #!/bin/bash
+
 if [ -z "${KUBECTL_BIN}" ]; then
-       if hash kubectl 2>/dev/null; then
-               KUBECTL_BIN='kubectl'
-       elif hash kubectl.exe 2>/dev/null; then
-               KUBECTL_BIN='kubectl.exe'
-       elif hash microk8s 2>/dev/null; then
-               KUBECTL_BIN='microk8s.kubectl'
-       fi
+       for bin in kubectl kubectl.exe microk8s.kubectl; do
+               if type $bin &>/dev/null; then
+                       KUBECTL_BIN="$bin"
+                       break
+               fi
+       done
 fi

-if ! hash "${KUBECTL_BIN}" 2>/dev/null; then
+if ! type "${KUBECTL_BIN}" &>/dev/null; then
        echo >&2 "kubectl is not installed"
        exit 1
 fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants