revamped fix-kubectl

This commit is contained in:
Lockszmith 2024-04-29 23:22:05 -04:00
parent 01cb3798ed
commit a18b8506a4
1 changed files with 38 additions and 21 deletions

View File

@ -1,11 +1,26 @@
#! /usr/bin/env bash #! /usr/bin/env bash
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) # shellcheck disable=SC2034,SC1090
USER_HOME=$HOME # SC2034 variables appear unsued. export if used externally
[[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")" # - uneeded, as this checked whether script is sourced
# SC1090 Can't follow non-constant source
BASE_0="$(basename ${0#-})" # Helper function
BASE_SHELL="$(basename "$SHELL")" is_sourced() {
if [[ -z "${BASE_0}" || "${BASE_0}" == "$BASE_SHELL" ]]; then if [ -n "$ZSH_VERSION" ]; then
case $ZSH_EVAL_CONTEXT in *:file:*) return 0;; esac
else # Add additional POSIX-compatible shell names here, if needed.
case ${0##*/} in dash|-dash|bash|-bash|ksh|-ksh|sh|-sh) return 0;; esac
fi
return 1; # NOT sourced.
}
BASE_0=${BASE_0:-$0}
BASE_SHELL=$(basename "$SHELL")
if is_sourced; then
DBG_="Is Sourced"
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASE_0}" )" &> /dev/null && pwd )
USER_HOME=$HOME
[[ -n "${SUDO_USER}" ]] && USER_HOME="$(eval "echo ~${SUDO_USER}")"
[[ -n "$KUBECONFIG" && -r "$KUBECONFIG" ]] || unset KUBECONFIG [[ -n "$KUBECONFIG" && -r "$KUBECONFIG" ]] || unset KUBECONFIG
@ -13,14 +28,16 @@ if [[ -z "${BASE_0}" || "${BASE_0}" == "$BASE_SHELL" ]]; then
|| export KUBECONFIG="${KUBECONFIG:-$HOME/.kube/config}" || export KUBECONFIG="${KUBECONFIG:-$HOME/.kube/config}"
if [[ -z "$KUBECONFIG" && -z "$FIX_KUBECTL_RECALL" ]]; then if [[ -z "$KUBECONFIG" && -z "$FIX_KUBECTL_RECALL" ]]; then
unset kubectl 2>/dev/null || unalias kubectl 2>/dev/null || true (unset kubectl 2>/dev/null) || (unalias kubectl 2>/dev/null) || true
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
fi fi
[[ 'function' == $(type -t kubectl-fix) ]] && unset kubectl-fix (unset kubectl-fix 2> /dev/null) || true
if [[ -n "$(type -t 'k3s')" && -z "$(type -t 'kubectl')" ]]; then DBG_="Check if k3s exists, but kubectl isn't available"
if (type 'k3s' >/dev/null 2>&1) && ! (type 'kubectl' >/dev/null 2>&1); then
DBG_="Define kubectl"
if [[ -r "${KUBECONFIG}" ]]; then if [[ -r "${KUBECONFIG}" ]]; then
kubectl() { k3s kubectl "${@}"; } kubectl() { k3s kubectl "${@}"; }
elif [[ -n "${KUBECONFIG}" && -e "${KUBECONFIG}" ]]; then elif [[ -n "${KUBECONFIG}" && -e "${KUBECONFIG}" ]]; then
@ -31,19 +48,19 @@ if [[ -z "${BASE_0}" || "${BASE_0}" == "$BASE_SHELL" ]]; then
sudo install --mod 600 --owner \"$USER\" \"\${KUBECONFIG}\" \"$HOME/.kube/config\" sudo install --mod 600 --owner \"$USER\" \"\${KUBECONFIG}\" \"$HOME/.kube/config\"
unset KUBECONFIG unset KUBECONFIG
fi fi
FIX_KUBECTL_RECALL=1 . \"$SCRIPT_FULL_PATH\" FIX_KUBECTL_RECALL=1 . \"${SCRIPT_DIR}/${BASE_0}\"
}" }"
fi fi
fi fi
if [[ -n "$(type -t 'kubectl')" ]]; then if (type 'kubectl' >/dev/null 2>&1); then
. <( kubectl completion ${SHELL##*/} ) . <( kubectl completion "${BASE_SHELL}" )
if [[ -n "$(type -t 'kubectl-cnpg')" ]]; then if (type 'kubectl-cnpg' >/dev/null 2>&1 ); then
. <( kubectl cnpg completion ${SHELL##*/} ) . <( kubectl cnpg completion "${BASE_SHELL}" )
fi fi
if [[ -z "$(type -t 'kubectl-cnpg')" || -n "$UPGRADE" ]]; then if ! (type 'kubectl-cnpg' >/dev/null 2>&1) || [[ -n "$UPGRADE" ]]; then
DL_VER=1.22.0 DL_VER=1.22.0
GH_URL=https://github.com/cloudnative-pg/cloudnative-pg GH_URL=https://github.com/cloudnative-pg/cloudnative-pg
DL_FILE="kubectl-cnpg_${DL_VER}_linux_x86_64.deb" DL_FILE="kubectl-cnpg_${DL_VER}_linux_x86_64.deb"
@ -54,7 +71,7 @@ if [[ -z "${BASE_0}" || "${BASE_0}" == "$BASE_SHELL" ]]; then
" sudo dpkg -i $DL_FILE" " sudo dpkg -i $DL_FILE"
fi fi
if [[ -z "$(type -t 'kubetui')" || -n "$UPGRADE" ]]; then if ! (type 'kubetui' >/dev/null 2>&1) || [[ -n "$UPGRADE" ]]; then
DL_VER=1.5.0 DL_VER=1.5.0
GH_URL=https://github.com/sarub0b0/kubetui GH_URL=https://github.com/sarub0b0/kubetui
DL_FILE=kubetui-x86_64-unknown-linux-musl-rustls DL_FILE=kubetui-x86_64-unknown-linux-musl-rustls
@ -65,26 +82,26 @@ if [[ -z "${BASE_0}" || "${BASE_0}" == "$BASE_SHELL" ]]; then
" chmod +x ~/bin/kubetui" " chmod +x ~/bin/kubetui"
fi fi
if [[ -z "$(type -t 'k9s')" || -n "$UPGRADE" ]]; then if ! (type 'k9s' >/dev/null 2>&1) || [[ -n "$UPGRADE" ]]; then
printf '%s\n' \ printf '%s\n' \
'To download and install k9s, run:' \ 'To download and install k9s, run:' \
" curl -sS https://webinstall.dev/k9s | bash" " curl -sS https://webinstall.dev/k9s | bash"
fi fi
if [[ -n "$(type -t 'k3s')" ]]; then if (type 'k3s' >/dev/null 2>&1 ); then
. <( k3s completion "${SHELL##*/}" ) . <( k3s completion "${SHELL##*/}" )
fi fi
if [[ -n "$(type -t 'helm')" ]]; then if (type 'helm' >/dev/null 2>&1); then
. <( helm completion "${SHELL##*/}" 2>/dev/null ) . <( helm completion "${SHELL##*/}" 2>/dev/null )
fi fi
kg() { kg() {
kubectl get ${NS:+--namespace=}${NS:---all-namespaces} "${@}" | { sed -u 1q; sort; } kubectl get "${NS:+--namespace=}${NS:---all-namespaces}" "${@}" | { sed -u 1q; sort; }
} }
fi fi
elif [[ "$1" == '-' ]]; then elif [[ "$1" == '-' ]]; then
printf '%s\n' "SCRIPT_FULL_PATH='$SCRIPT_DIR/$BASE_0'" printf 'BASE_0="%s"\n' "${BASE_0}"
cat "${BASH_SOURCE[0]}" cat "${BASH_SOURCE[0]}"
else else
SCRIPT_NAME="$BASE_0" SCRIPT_NAME="$BASE_0"