truestuff/fix-kubectl

114 lines
4.1 KiB
Plaintext
Raw Normal View History

2023-11-08 23:10:59 +00:00
#! /usr/bin/env bash
2024-04-30 03:22:05 +00:00
# shellcheck disable=SC2034,SC1090
# SC2034 variables appear unsued. export if used externally
# - uneeded, as this checked whether script is sourced
# SC1090 Can't follow non-constant source
2024-04-30 03:22:05 +00:00
# Helper function
is_sourced() {
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}")"
2023-11-08 23:10:59 +00:00
[[ -n "$KUBECONFIG" && -r "$KUBECONFIG" ]] || unset KUBECONFIG
[[ ! -r "$HOME/.kube/config" ]] \
|| export KUBECONFIG="${KUBECONFIG:-$HOME/.kube/config}"
if [[ -z "$KUBECONFIG" && -z "$FIX_KUBECTL_RECALL" ]]; then
2024-04-30 03:22:05 +00:00
(unset kubectl 2>/dev/null) || (unalias kubectl 2>/dev/null) || true
2023-11-08 23:10:59 +00:00
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
fi
2024-04-30 03:22:05 +00:00
(unset kubectl-fix 2> /dev/null) || true
2023-11-08 23:10:59 +00:00
2024-04-30 03:22:05 +00:00
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"
2023-11-08 23:10:59 +00:00
if [[ -r "${KUBECONFIG}" ]]; then
kubectl() { k3s kubectl "${@}"; }
elif [[ -n "${KUBECONFIG}" && -e "${KUBECONFIG}" ]]; then
eval "
kubectl-fix() {
mkdir \"$HOME/.kube\" -p
if [[ -e \"\${KUBECONFIG}\" && ! -r \"$HOME/.kube/config\" ]]; then
sudo install --mod 600 --owner \"$USER\" \"\${KUBECONFIG}\" \"$HOME/.kube/config\"
unset KUBECONFIG
fi
2024-04-30 03:22:05 +00:00
FIX_KUBECTL_RECALL=1 . \"${SCRIPT_DIR}/${BASE_0}\"
2023-11-08 23:10:59 +00:00
}"
fi
fi
2024-04-30 03:22:05 +00:00
if (type 'kubectl' >/dev/null 2>&1); then
2023-11-08 23:10:59 +00:00
2024-04-30 03:22:05 +00:00
. <( kubectl completion "${BASE_SHELL}" )
2023-11-08 23:10:59 +00:00
2024-04-30 03:22:05 +00:00
if (type 'kubectl-cnpg' >/dev/null 2>&1 ); then
. <( kubectl cnpg completion "${BASE_SHELL}" )
fi
2024-04-30 03:22:05 +00:00
if ! (type 'kubectl-cnpg' >/dev/null 2>&1) || [[ -n "$UPGRADE" ]]; then
DL_VER=1.22.0
GH_URL=https://github.com/cloudnative-pg/cloudnative-pg
DL_FILE="kubectl-cnpg_${DL_VER}_linux_x86_64.deb"
URL=${GH_URL}/releases/download/v${DL_VER}/${DL_FILE}
printf '%s\n' \
'To download and install cnpg kubectl plugin, run:' \
" curl -sSLO ${URL}" \
" sudo dpkg -i $DL_FILE"
2023-11-08 23:10:59 +00:00
fi
2024-04-30 03:22:05 +00:00
if ! (type 'kubetui' >/dev/null 2>&1) || [[ -n "$UPGRADE" ]]; then
DL_VER=1.5.0
GH_URL=https://github.com/sarub0b0/kubetui
DL_FILE=kubetui-x86_64-unknown-linux-musl-rustls
URL=${GH_URL}/releases/download/v${DL_VER}/${DL_FILE}
printf '%s\n' \
'To download and install kubetui, run:' \
" curl -sSL ${URL} > ~/bin/kubetui" \
" chmod +x ~/bin/kubetui"
2023-11-08 23:47:13 +00:00
fi
2024-04-30 03:22:05 +00:00
if ! (type 'k9s' >/dev/null 2>&1) || [[ -n "$UPGRADE" ]]; then
printf '%s\n' \
'To download and install k9s, run:' \
" curl -sS https://webinstall.dev/k9s | bash"
2023-11-08 23:47:13 +00:00
fi
2024-04-30 03:22:05 +00:00
if (type 'k3s' >/dev/null 2>&1 ); then
. <( k3s completion "${SHELL##*/}" )
2023-11-08 23:10:59 +00:00
fi
2024-04-30 03:22:05 +00:00
if (type 'helm' >/dev/null 2>&1); then
. <( helm completion "${SHELL##*/}" 2>/dev/null )
2023-11-08 23:10:59 +00:00
fi
kg() {
2024-04-30 03:22:05 +00:00
kubectl get "${NS:+--namespace=}${NS:---all-namespaces}" "${@}" | { sed -u 1q; sort; }
2023-11-08 23:10:59 +00:00
}
fi
elif [[ "$1" == '-' ]]; then
2024-04-30 03:22:05 +00:00
printf 'BASE_0="%s"\n' "${BASE_0}"
2023-11-08 23:47:13 +00:00
cat "${BASH_SOURCE[0]}"
2023-11-08 23:10:59 +00:00
else
2023-11-08 23:47:13 +00:00
SCRIPT_NAME="$BASE_0"
2023-11-08 23:10:59 +00:00
printf '%s\n' \
"It seems $SCRIPT_NAME was invoked as a script. It should be sourced instead." \
'The easiest way is to call it like this:' \
" $ . <( $SCRIPT_NAME - ) # Note the '-' after the script's name" \
''
fi