Check for kubectl.exe on Windows (#96)

On the various flavors of bash for Windows kubectl won't resolve as the binary is kubectl.exe.
Simple aliasing doesn't seem to work. So test for both otherwise fail with a not found error.
This commit is contained in:
Chad Metcalf 2018-11-26 12:21:21 -08:00 committed by Ahmet Alp Balkan
parent 3aeb4e76d2
commit ec994aff89
2 changed files with 29 additions and 17 deletions

25
kubectx
View File

@ -46,11 +46,11 @@ exit_err() {
}
current_context() {
kubectl config view -o=jsonpath='{.current-context}'
$KUBECTL config view -o=jsonpath='{.current-context}'
}
get_contexts() {
kubectl config get-contexts -o=name | sort -n
$KUBECTL config get-contexts -o=name | sort -n
}
list_contexts() {
@ -93,7 +93,7 @@ save_context() {
}
switch_context() {
kubectl config use-context "${1}"
$KUBECTL config use-context "${1}"
}
choose_context_interactive() {
@ -129,7 +129,7 @@ swap_context() {
}
context_exists() {
grep -q ^"${1}"\$ <(kubectl config get-contexts -o=name)
grep -q ^"${1}"\$ <($KUBECTL config get-contexts -o=name)
}
rename_context() {
@ -142,10 +142,10 @@ rename_context() {
if context_exists "${new_name}"; then
echo "Context \"${new_name}\" exists, deleting..." >&2
kubectl config delete-context "${new_name}" 1>/dev/null 2>&1
$KUBECTL config delete-context "${new_name}" 1>/dev/null 2>&1
fi
kubectl config rename-context "${old_name}" "${new_name}"
$KUBECTL config rename-context "${old_name}" "${new_name}"
}
delete_contexts() {
@ -161,14 +161,19 @@ delete_context() {
ctx="$(current_context)" || exit_err "error getting current context"
fi
echo "Deleting context \"${ctx}\"..." >&2
kubectl config delete-context "${ctx}"
$KUBECTL config delete-context "${ctx}"
}
main() {
if ! hash kubectl 2>/dev/null; then
echo >&2 "kubectl is not installed"
exit 1
if hash kubectl 2>/dev/null; then
KUBECTL=kubectl
elif hash kubectl.exe 2>/dev/null; then
KUBECTL=kubectl.exe
else
echo >&2 "kubectl is not installed"
exit 1
fi
if [[ "$#" -eq 0 ]]; then
if [[ -t 1 && -z "${KUBECTX_IGNORE_FZF:-}" && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then
choose_context_interactive

21
kubens
View File

@ -41,9 +41,11 @@ exit_err() {
current_namespace() {
local cur_ctx
cur_ctx="$(current_context)" || exit_err "error getting current context"
ns="$(kubectl config view -o=jsonpath="{.contexts[?(@.name==\"${cur_ctx}\")].context.namespace}")" \
ns="$($KUBECTL config view -o=jsonpath="{.contexts[?(@.name==\"${cur_ctx}\")].context.namespace}")" \
|| exit_err "error getting current namespace"
if [[ -z "${ns}" ]]; then
echo "default"
else
@ -52,11 +54,11 @@ current_namespace() {
}
current_context() {
kubectl config view -o=jsonpath='{.current-context}'
$KUBECTL config view -o=jsonpath='{.current-context}'
}
get_namespaces() {
kubectl get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}'
$KUBECTL get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}'
}
escape_context_name() {
@ -88,7 +90,7 @@ save_namespace() {
switch_namespace() {
local ctx="${1}"
kubectl config set-context "${ctx}" --namespace="${2}"
$KUBECTL config set-context "${ctx}" --namespace="${2}"
echo "Active namespace is \"${2}\".">&2
}
@ -163,10 +165,15 @@ swap_namespace() {
}
main() {
if ! hash kubectl 2>/dev/null; then
echo >&2 "kubectl is not installed"
exit 1
if hash kubectl 2>/dev/null; then
KUBECTL=kubectl
elif hash kubectl.exe 2>/dev/null; then
KUBECTL=kubectl.exe
else
echo >&2 "kubectl is not installed"
exit 1
fi
if [[ "$#" -eq 0 ]]; then
if [[ -t 1 && -z ${KUBECTX_IGNORE_FZF:-} && "$(type fzf &>/dev/null; echo $?)" -eq 0 ]]; then
choose_namespace_interactive