Files
scwrypts/plugins/kube/driver/kubectl.driver.zsh
T

199 lines
5.1 KiB
Bash
Raw Normal View History

2023-08-30 17:26:13 -06:00
[[ $SCWRYPTS_KUBECTL_DRIVER_READY -eq 1 ]] && return 0
2026-05-05 15:31:50 -06:00
unalias k h f &>/dev/null
2023-08-30 17:26:13 -06:00
k() { _SCWRYPTS_KUBECTL_DRIVER kubectl $@; }
h() { _SCWRYPTS_KUBECTL_DRIVER helm $@; }
2023-11-22 15:54:16 -07:00
f() { _SCWRYPTS_KUBECTL_DRIVER flux $@; }
2023-08-30 17:26:13 -06:00
_SCWRYPTS_KUBECTL_DRIVER() {
[ ! $SCWRYPTS_ENV ] && {
2024-05-10 13:32:02 -06:00
echo.error "must set SCWRYPTS_ENV in order to use '$(echo $CLI | head -c1)'"
2023-08-30 17:26:13 -06:00
return 1
}
2026-05-05 15:31:50 -06:00
which kube.redis &>/dev/null \
2024-05-10 13:32:02 -06:00
|| eval "$(scwrypts -n --name meta/get-static-redis-definition --type zsh --group kube)"
2023-08-30 17:26:13 -06:00
local CLI="$1"; shift 1
local SCWRYPTS_GROUP CUSTOM_COMMANDS=(meta)
for SCWRYPTS_GROUP in ${SCWRYPTS_GROUPS[@]}
do
CUSTOM_COMMANDS+=($(eval echo '$SCWRYPTS_KUBECTL_CUSTOM_COMMANDS__'$SCWRYPTS_GROUP))
done
##########################################
local USAGE="
usage: - [...args...] [...options...] -- [...$CLI options...]
args: -
options: -
--subsession [0-9] use indicated subsession (use for script clarity instead of positional arg)
-h, --help display this help dialogue
-v, --verbose output debugging information
description: -
"
2024-05-10 13:32:02 -06:00
local USAGE__usage=$(echo $CLI | head -c1)
2023-08-30 17:26:13 -06:00
local USAGE__args="$(
{
2024-05-10 13:32:02 -06:00
echo "$(utils.colors.print green '[0-9]')^if the first argument is a number 0-9, uses or creates a subsession (default 0)"
2023-08-30 17:26:13 -06:00
echo " ^ "
for C in ${CUSTOM_COMMANDS[@]}
do
2024-05-10 13:32:02 -06:00
echo "$(utils.colors.print green ${C})^$(SCWRYPTS_KUBECTL_CUSTOM_COMMAND_DESCRIPTION__$C 2>/dev/null)"
done
2023-08-30 17:26:13 -06:00
} | column -ts '^'
)"
local USAGE__options="
-n, --namespace set the namespace
-k, --context set the context
"
local USAGE__description="
2023-11-22 15:54:16 -07:00
Provides 'k' (kubectl), 'h' (helm), and 'f' (flux) shorthands to the respective
2023-08-30 17:26:13 -06:00
utility. These functions leverage redis and scwrypts environments to
allow quick selection of contexts and namespaces usable across all
active shell instances.
2024-05-10 13:32:02 -06:00
2023-08-30 17:26:13 -06:00
The scwrypts group 'kubectl' has simple selection executables for
kubecontext and namespace, but also provides the library to enable
enriched, use-case-sensitive setup of kubernetes context.
All actions are scoped to the current SCWRYPTS_ENV
2024-05-10 13:32:02 -06:00
currently : $(utils.colors.print yellow ${SCWRYPTS_ENV})
2023-08-30 17:26:13 -06:00
"
##########################################
2024-05-10 13:32:02 -06:00
2023-08-30 17:26:13 -06:00
local USER_ARGS=()
local CUSTOM_COMMAND=0
local VERBOSE=0
local HELP=0
local ERRORS=0
2023-10-05 14:36:40 -06:00
local COMMAND_SWITCH_CASE="@($(echo $CUSTOM_COMMANDS | sed 's/ /|/g'))"
2023-08-30 17:26:13 -06:00
[ ! $SUBSESSION ] && local SUBSESSION=0
[[ $1 =~ ^[0-9]$ ]] && SUBSESSION=$1 && shift 1
while [[ $# -gt 0 ]]
do
case $1 in
meta )
CUSTOM_COMMAND=$1
SCWRYPTS_KUBECTL_CUSTOM_COMMAND_PARSE__$1 ${@:2}
break
;;
-v | --verbose ) VERBOSE=1 ;;
-h | --help ) HELP=1 ;;
--subsession ) SUBSESSION=$2; shift 1 ;;
-n | --namespace )
2023-11-22 15:54:16 -07:00
_SCWRYPTS_KUBECTL_DRIVER kubectl meta set namespace $2
shift 1
2023-08-30 17:26:13 -06:00
;;
-k | --context | --kube-context )
2023-11-22 15:54:16 -07:00
_SCWRYPTS_KUBECTL_DRIVER kubectl meta set context $2
2023-08-30 17:26:13 -06:00
shift 1
;;
2023-12-11 17:07:09 -07:00
-- )
echo $USER_ARGS | grep -q 'exec' && USER_ARGS+=(--)
shift 1
break
;;
2023-08-30 17:26:13 -06:00
2023-10-05 14:36:40 -06:00
* )
[ ! $CUSTOM_COMMAND ] && {
for C in ${CUSTOM_COMMANDS[@]}
do
[[ $1 =~ ^$C$ ]] && {
SCWRYPTS_KUBECTL_CUSTOM_COMMAND_PARSE__$1 ${@:2}
break
}
done
}
USER_ARGS+=($1)
;;
2023-08-30 17:26:13 -06:00
esac
shift 1
done
while [[ $# -gt 0 ]]; do USER_ARGS+=($1); shift 1; done
2024-05-10 13:32:02 -06:00
utils.check-errors || return 1
2023-08-30 17:26:13 -06:00
2024-05-10 13:32:02 -06:00
[[ $HELP -eq 1 ]] && { utils.io.usage; return 0; }
2023-08-30 17:26:13 -06:00
#####################################################################
local STRICT=$(_SCWRYPTS_KUBECTL_SETTINGS get strict || echo 1)
case $CUSTOM_COMMAND in
0 )
local CLI_ARGS=()
local CONTEXT=$(k meta get context)
local NAMESPACE=$(k meta get namespace)
[ $CONTEXT ] && [[ $CLI =~ ^helm$ ]] && CLI_ARGS+=(--kube-context $CONTEXT)
[ $CONTEXT ] && [[ $CLI =~ ^kubectl$ ]] && CLI_ARGS+=(--context $CONTEXT)
2023-11-22 15:54:16 -07:00
[ $CONTEXT ] && [[ $CLI =~ ^flux$ ]] && CLI_ARGS+=(--context $CONTEXT)
2023-08-30 17:26:13 -06:00
[[ $STRICT -eq 1 ]] && {
2024-05-10 13:32:02 -06:00
[ $CONTEXT ] || echo.error "missing kubectl 'context'"
[ $NAMESPACE ] || echo.error "missing kubectl 'namespace'"
2023-08-30 17:26:13 -06:00
2024-05-10 13:32:02 -06:00
utils.check-errors --no-fail --no-usage || {
echo.error "with 'strict' settings enabled, context and namespace must be set!"
echo.reminder "
2023-08-30 17:26:13 -06:00
these values can be set directly with
$(echo $CLI | head -c1) meta set (namespace|context)
"
return 2
}
}
[ $NAMESPACE ] && CLI_ARGS+=(--namespace $NAMESPACE)
[[ $VERBOSE -eq 1 ]] && {
2024-05-10 13:32:02 -06:00
echo.reminder "
2023-08-30 17:26:13 -06:00
context '$CONTEXT'
namespace '$NAMESPACE'
environment '$SCWRYPTS_ENV'
subsession '$SUBSESSION'
"
2024-05-10 13:32:02 -06:00
echo.status "running $CLI ${CLI_ARGS[@]} ${USER_ARGS[@]}"
2023-08-30 17:26:13 -06:00
} || {
[[ $(_SCWRYPTS_KUBECTL_SETTINGS get context) =~ ^show$ ]] && {
2024-05-10 13:32:02 -06:00
echo.reminder "$SCWRYPTS_ENV.$SUBSESSION : $CLI ${CLI_ARGS[@]} ${USER_ARGS[@]}"
2023-08-30 17:26:13 -06:00
}
}
$CLI ${CLI_ARGS[@]} ${USER_ARGS[@]}
;;
* ) SCWRYPTS_KUBECTL_CUSTOM_COMMAND__$CUSTOM_COMMAND ${USER_ARGS[@]} ;;
esac
}
_SCWRYPTS_KUBECTL_SETTINGS() {
# (get setting-name) or (set setting-name setting-value)
2024-05-10 13:32:02 -06:00
kube.redis h$1 ${SCWRYPTS_ENV}:kubectl:settings ${@:2} | grep .
2023-08-30 17:26:13 -06:00
}
#####################################################################
source ${0:a:h}/kubectl.completion.zsh
source ${0:a:h}/meta.zsh