2023-11-08 23:10:59 +00:00
|
|
|
#! /usr/bin/env bash
|
2023-11-08 23:47:13 +00:00
|
|
|
BASE_0="$(basename ${0#-})"
|
|
|
|
BASE_SHELL="$(basename "$SHELL")"
|
|
|
|
if [[ -z "${BASE_0}" || "${BASE_0}" == "$BASE_SHELL" ]]; then
|
2023-11-08 23:10:59 +00:00
|
|
|
|
2023-11-09 01:54:07 +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
|
2023-11-08 23:10:59 +00:00
|
|
|
unset kubectl 2>/dev/null || unalias kubectl 2>/dev/null || true
|
|
|
|
|
|
|
|
export KUBECONFIG=/etc/rancher/k3s/k3s.yaml
|
|
|
|
fi
|
|
|
|
|
|
|
|
[[ 'function' == $(type -t kubectl-fix) ]] && unset kubectl-fix
|
|
|
|
|
|
|
|
if [[ -n "$(type -t 'k3s')" && -z "$(type -t 'kubectl')" ]]; then
|
|
|
|
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
|
2023-11-09 01:54:07 +00:00
|
|
|
FIX_KUBECTL_RECALL=1 . \"$SCRIPT_FULL_PATH\"
|
2023-11-08 23:10:59 +00:00
|
|
|
}"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -n "$(type -t 'kubectl')" ]]; then
|
|
|
|
|
|
|
|
. <( kubectl completion ${SHELL##*/} )
|
|
|
|
|
|
|
|
if [[ -n "$(type -t 'kubectl-cnpg')" ]]; then
|
|
|
|
. <( kubectl cnpg completion ${SHELL##*/} )
|
|
|
|
else
|
2023-11-09 04:05:04 +00:00
|
|
|
DL_VER=v1.21.1
|
2023-11-08 23:10:59 +00:00
|
|
|
GH_URL=https://github.com/cloudnative-pg/cloudnative-pg
|
2023-11-08 23:47:13 +00:00
|
|
|
DL_FILE="kubectl-cnpg_${DL_VER}_linux_x86_64.deb"
|
|
|
|
URL=${GH_URL}/releases/download/${DL_VER}/${DL_FILE}
|
2023-11-08 23:10:59 +00:00
|
|
|
printf '%s\n' \
|
2023-11-08 23:47:13 +00:00
|
|
|
'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
|
|
|
|
|
2023-11-08 23:47:13 +00:00
|
|
|
if [[ -z "$(type -t 'kubetui')" ]]; then
|
|
|
|
DL_VER='v1.4.2'
|
|
|
|
GH_URL=https://github.com/sarub0b0/kubetui
|
|
|
|
DL_FILE=kubetui-x86_64-unknown-linux-musl-rustls
|
|
|
|
URL=${GH_URL}/releases/download/${DL_VER}/${DL_FILE}
|
|
|
|
printf '%s\n' \
|
|
|
|
'To download and install kubetui, run:' \
|
|
|
|
" curl -sSL ${URL} > ~/bin/kubetui" \
|
|
|
|
" chmod +x ~/bin/kubetui"
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -z "$(type -t 'k3s')" ]]; then
|
|
|
|
printf '%s\n' \
|
|
|
|
'To download and install k3s, run:' \
|
|
|
|
" curl -sS https://webinstall.dev/k9s | bash"
|
|
|
|
fi
|
|
|
|
|
2023-11-08 23:10:59 +00:00
|
|
|
if [[ -n "$(type -t 'k3s')" ]]; then
|
|
|
|
. <( k3s completion ${SHELL##*/} )
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [[ -n "$(type -t 'helm')" ]]; then
|
|
|
|
. <( helm completion ${SHELL##*/} 2>/dev/null )
|
|
|
|
fi
|
|
|
|
|
|
|
|
kg() {
|
|
|
|
kubectl get ${NS:+--namespace=}${NS:---all-namespaces} "${@}" | { sed -u 1q; sort; }
|
|
|
|
}
|
|
|
|
fi
|
|
|
|
elif [[ "$1" == '-' ]]; then
|
2023-11-09 01:54:07 +00:00
|
|
|
printf "SCRIPT_FULL_PATH='%s'\n" "${BASH_SOURCE[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
|