Added kcontext-k8s

This commit is contained in:
斟酌 鵬兄 2023-10-09 01:30:56 +08:00
parent f13fe397b6
commit 13dc0770b8

View File

@ -6,7 +6,7 @@ if [ $? -ne 0 ]; then
return 1
fi
complete -W "k8s docker s3-arch" kcontext
complete -W "k8s docker s3-arch" kcontext
function kcontext {
case $1 in
@ -23,11 +23,89 @@ function kcontext {
return $?
}
function kcontext-k8s {
local _NAME _CONF
case $1 in
list)
kstore query -list "SELECT SUBSTR( prop, 5 ) FROM store WHERE key = 'kcontext' AND prop LIKE 'k8s.%'"
return $?
;;
save)
_NAME=$( kubectl config current-context )
if [ $? -ne 0 ]; then
return 1
fi
kstore get "kcontext" "k8s.$_NAME" 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
read -p "Replace existing config for \"$_NAME\"? (y/n): " _CONFIRM
if [ "$_CONFIRM" != "y" ]; then
return 0
fi
kubectl config view --minify --flatten | kstore update "kcontext" - "k8s.$_NAME"
return $?
else
kubectl config view --minify --flatten | kstore add "kcontext" - "k8s.$_NAME"
return $?
fi
;;
use)
_NAME=$2
if [ -z "$_NAME" ]; then
echo "Please specify a context name" >&2
return 1
fi
_CONF="$RBASH_HOME/k8s.conf"
kstore get "kcontext" "k8s.$_NAME" > "$_CONF"
if [ $? -ne 0 ]; then
echo "No such context: $_NAME" >&2
return 1
fi
chmod 600 "$_CONF"
echo $KUBECONFIG | grep "$_CONF" -q
if [ $? -eq 0 ]; then
export KUBECONFIG="$_CONF"
else
export KUBECONFIG="$_CONF:$KUBECONFIG"
fi
;;
del)
_NAME=$2
if [ -z "$_NAME" ]; then
echo "Please specify a context name" >&2
return 1
fi
kstore get "kcontext" "k8s.$_NAME" 2> /dev/null > /dev/null
if [ $? -eq 0 ]; then
read -p "Delete context \"$_NAME\"? (y/n): " _CONFIRM
if [ "$_CONFIRM" != "y" ]; then
return 0
fi
kstore query "DELETE FROM store WHERE key = 'kcontext' AND prop = 'k8s.$_NAME'"
return $?
else
echo "No such context: $_NAME" >&2
fi
;;
*)
__func_head "list"
__func_help "save"
__func_help "use NAME"
__func_help "del NAME"
;;
esac
return 1
}
function kcontext-s3-arch {
local _NAME _CONFIRM _URL _AUTH
case $1 in
list)
kstore query -list "SELECT SUBSTR( prop, 16 ) FROM store WHERE key = 'kcontext' AND prop LIKE 's3-arch.bucket.%'"
return $?
;;
save)
_NAME=$2
@ -54,7 +132,7 @@ function kcontext-s3-arch {
echo "No s3-arch settings can be found in env" >&2
fi
;;
load)
use)
_NAME=$2
if [ -z "$_NAME" ]; then
echo "Please specify a context name" >&2
@ -97,7 +175,7 @@ function kcontext-s3-arch {
*)
__func_head "list"
__func_help "save NAME"
__func_help "load NAME"
__func_help "use NAME"
__func_help "del NAME"
;;
esac