v3.8.0
===================================================================== --- Changes ------------------------------ - kubectl driver updates; getting better, but still need to fix autocomplete in certain circumstances - added -y|--yes flags to scwrypts to auto-accept user-prompts (use with caution) - figured out the whole mikefarah/yq vs kislyuk/yq thing; use YQ for compatiblity --- Bug fixes ---------------------------- - helm template generation now loads values in a more appropriate order which prevents overwrite by the wrong values file
This commit is contained in:
@ -3,6 +3,7 @@
|
||||
unalias k h >/dev/null 2>&1
|
||||
k() { _SCWRYPTS_KUBECTL_DRIVER kubectl $@; }
|
||||
h() { _SCWRYPTS_KUBECTL_DRIVER helm $@; }
|
||||
f() { _SCWRYPTS_KUBECTL_DRIVER flux $@; }
|
||||
|
||||
|
||||
_SCWRYPTS_KUBECTL_DRIVER() {
|
||||
@ -57,7 +58,7 @@ _SCWRYPTS_KUBECTL_DRIVER() {
|
||||
"
|
||||
|
||||
local USAGE__description="
|
||||
Provides 'k' (kubectl) and 'h' (helm) shorthands to the respective
|
||||
Provides 'k' (kubectl), 'h' (helm), and 'f' (flux) shorthands to the respective
|
||||
utility. These functions leverage redis and scwrypts environments to
|
||||
allow quick selection of contexts and namespaces usable across all
|
||||
active shell instances.
|
||||
@ -100,14 +101,12 @@ _SCWRYPTS_KUBECTL_DRIVER() {
|
||||
--subsession ) SUBSESSION=$2; shift 1 ;;
|
||||
|
||||
-n | --namespace )
|
||||
echo "TODO: set namespace ('$2')" >&2
|
||||
USER_ARGS+=(--namespace $2); shift 1
|
||||
_SCWRYPTS_KUBECTL_DRIVER kubectl meta set namespace $2
|
||||
shift 1
|
||||
;;
|
||||
|
||||
-k | --context | --kube-context )
|
||||
echo "TODO: set context ('$2')" >&2
|
||||
[[ $CLI =~ ^helm$ ]] && USER_ARGS+=(--kube-context $2)
|
||||
[[ $CLI =~ ^kubectl$ ]] && USER_ARGS+=(--context $2)
|
||||
_SCWRYPTS_KUBECTL_DRIVER kubectl meta set context $2
|
||||
shift 1
|
||||
;;
|
||||
|
||||
@ -149,6 +148,7 @@ _SCWRYPTS_KUBECTL_DRIVER() {
|
||||
|
||||
[ $CONTEXT ] && [[ $CLI =~ ^helm$ ]] && CLI_ARGS+=(--kube-context $CONTEXT)
|
||||
[ $CONTEXT ] && [[ $CLI =~ ^kubectl$ ]] && CLI_ARGS+=(--context $CONTEXT)
|
||||
[ $CONTEXT ] && [[ $CLI =~ ^flux$ ]] && CLI_ARGS+=(--context $CONTEXT)
|
||||
|
||||
[[ $STRICT -eq 1 ]] && {
|
||||
[ $CONTEXT ] || ERROR "missing kubectl 'context'"
|
||||
|
@ -30,8 +30,15 @@ SCWRYPTS_KUBECTL_CUSTOM_COMMAND_PARSE__meta() {
|
||||
USAGE__args="set (namespace|context)"
|
||||
USAGE__description="interactively set a namespace or context for '$SCWRYPTS_ENV'"
|
||||
case $2 in
|
||||
namespace | context ) USER_ARGS+=($1 $2) ;;
|
||||
namespace | context ) USER_ARGS+=($1 $2 $3); [ $3 ] && shift 1 ;;
|
||||
-h | --help ) HELP=1 ;;
|
||||
'' )
|
||||
: \
|
||||
&& SCWRYPTS_KUBECTL_CUSTOM_COMMAND__meta set context \
|
||||
&& SCWRYPTS_KUBECTL_CUSTOM_COMMAND__meta set namespace \
|
||||
;
|
||||
return $?
|
||||
;;
|
||||
|
||||
* ) ERROR "cannot set '$2'" ;;
|
||||
esac
|
||||
@ -94,7 +101,7 @@ SCWRYPTS_KUBECTL_CUSTOM_COMMAND__meta() {
|
||||
;;
|
||||
|
||||
set )
|
||||
scwrypts -n --name set-$2 --type zsh --group kubectl -- --subsession $SUBSESSION >/dev/null \
|
||||
scwrypts -n --name set-$2 --type zsh --group kubectl -- $3 --subsession $SUBSESSION >/dev/null \
|
||||
&& SUCCESS "$2 set"
|
||||
;;
|
||||
|
||||
|
@ -21,7 +21,6 @@ KUBECTL() {
|
||||
kubectl ${KUBECTL_ARGS[@]} $@
|
||||
}
|
||||
|
||||
|
||||
#####################################################################
|
||||
|
||||
KUBECTL__GET_CONTEXT() { REDIS get --prefix "current:context"; }
|
||||
@ -87,3 +86,73 @@ KUBECTL__LIST_NAMESPACES() {
|
||||
echo default
|
||||
KUBECTL get namespaces -o name | sed 's/^namespace\///' | sort
|
||||
}
|
||||
|
||||
#####################################################################
|
||||
|
||||
KUBECTL__SERVE() {
|
||||
[ $CONTEXT ] || local CONTEXT=$(KUBECTL__GET_CONTEXT)
|
||||
[ $CONTEXT ] || ERROR 'must configure a context in which to serve'
|
||||
|
||||
[ $NAMESPACE ] || local NAMESPACE=$(KUBECTL__GET_NAMESPACE)
|
||||
[ $NAMESPACE ] || ERROR 'must configure a namespace in which to serve'
|
||||
|
||||
CHECK_ERRORS --no-fail --no-usage || return 1
|
||||
|
||||
[ $SERVICE ] && SERVICE=$(KUBECTL__LIST_SERVICES | jq -c "select (.service == \"$SERVICE\")" || echo $SERVICE)
|
||||
[ $SERVICE ] || local SERVICE=$(KUBECTL__SELECT_SERVICE)
|
||||
[ $SERVICE ] || ERROR 'must provide or select a service'
|
||||
|
||||
KUBECTL__LIST_SERVICES | grep -q "^$SERVICE$"\
|
||||
|| ERROR "no service '$SERVICE' in '$CONFIG/$NAMESPACE'"
|
||||
|
||||
CHECK_ERRORS --no-fail --no-usage || return 1
|
||||
|
||||
##########################################
|
||||
|
||||
SERVICE_PASSWORD="$(KUBECTL__GET_SERVICE_PASSWORD)"
|
||||
KUBECTL__SERVICE_PARSE
|
||||
|
||||
INFO "attempting to serve ${NAMESPACE}/${SERVICE_NAME}:${SERVICE_PORT}"
|
||||
[ $SERVICE_PASSWORD ] && INFO "password : $SERVICE_PASSWORD"
|
||||
|
||||
KUBECTL port-forward service/$SERVICE_NAME $SERVICE_PORT
|
||||
}
|
||||
|
||||
KUBECTL__SELECT_SERVICE() {
|
||||
[ $NAMESPACE ] || local NAMESPACE=$(KUBECTL__GET_NAMESPACE)
|
||||
[ $NAMESPACE ] || return 1
|
||||
|
||||
local SERVICES=$(KUBECTL__LIST_SERVICES)
|
||||
local SELECTED=$({
|
||||
echo "namespace service port"
|
||||
echo $SERVICES \
|
||||
| jq -r '.service + " " + .port' \
|
||||
| sed "s/^/$NAMESPACE /" \
|
||||
;
|
||||
} \
|
||||
| column -t \
|
||||
| FZF 'select a service' --header-lines=1 \
|
||||
| awk '{print $2;}' \
|
||||
)
|
||||
|
||||
echo $SERVICES | jq -c "select (.service == \"$SELECTED\")"
|
||||
}
|
||||
|
||||
KUBECTL__LIST_SERVICES() {
|
||||
KUBECTL get service --no-headers\
|
||||
| awk '{print "{\"service\":\""$1"\",\"ip\":\""$3"\",\"port\":\""$5"\"}"}' \
|
||||
| jq -c 'select (.ip != "None")' \
|
||||
;
|
||||
}
|
||||
|
||||
KUBECTL__GET_SERVICE_PASSWORD() {
|
||||
[ $PASSWORD_SECRET ] && [ $PASSWORD_KEY ] || return 0
|
||||
|
||||
KUBECTL get secret $PASSWORD_SECRET -o jsonpath="{.data.$PASSWORD_KEY}" \
|
||||
| base64 --decode
|
||||
}
|
||||
|
||||
KUBECTL__SERVICE_PARSE() {
|
||||
SERVICE_NAME=$(echo $SERVICE | jq -r .service)
|
||||
SERVICE_PORT=$(echo $SERVICE | jq -r .port | sed 's|/.*$||')
|
||||
}
|
||||
|
58
plugins/kubectl/serve
Executable file
58
plugins/kubectl/serve
Executable file
@ -0,0 +1,58 @@
|
||||
#!/bin/zsh
|
||||
#####################################################################
|
||||
DEPENDENCIES+=()
|
||||
REQUIRED_ENV+=()
|
||||
|
||||
use kubectl --group kubectl
|
||||
|
||||
CHECK_ENVIRONMENT
|
||||
#####################################################################
|
||||
|
||||
MAIN() {
|
||||
local USAGE="
|
||||
usage: [service] [...options...]
|
||||
|
||||
args:
|
||||
service (optional) name of the service to forward locally
|
||||
|
||||
options:
|
||||
--context override context
|
||||
--namespace override namespace
|
||||
--subsession REDIS subsession (default 0)
|
||||
|
||||
to show a required password on screen, use both:
|
||||
--password-secret Secret resource
|
||||
--password-key key within Secret's 'data'
|
||||
|
||||
-h, --help show this dialogue and exit
|
||||
"
|
||||
local CONTEXT NAMESPACE SERVICE
|
||||
local SUBSESSION=0
|
||||
|
||||
while [[ $# -gt 0 ]]
|
||||
do
|
||||
case $1 in
|
||||
--context ) CONTEXT=$2; shift 1 ;;
|
||||
--namespace ) NAMESPACE=$2; shift 1 ;;
|
||||
--subsession ) SUBSESSION=$2; shift 1 ;;
|
||||
|
||||
--password-secret ) PASSWORD_SECRET=$2; shift 1 ;;
|
||||
--password-key ) PASSWORD_KEY=$2; shift 1 ;;
|
||||
|
||||
-h | --help ) USAGE; return 0 ;;
|
||||
|
||||
* )
|
||||
[ $SERVICE ] && ERROR "unexpected argument '$2'"
|
||||
SERVICE=$1
|
||||
;;
|
||||
esac
|
||||
shift 1
|
||||
done
|
||||
|
||||
CHECK_ERRORS
|
||||
|
||||
KUBECTL__SERVE
|
||||
}
|
||||
|
||||
#####################################################################
|
||||
MAIN $@
|
@ -17,6 +17,8 @@ MAIN() {
|
||||
|
||||
options:
|
||||
--subsession REDIS subsession (default 0)
|
||||
|
||||
-h, --help show this dialogue and exit
|
||||
"
|
||||
local CONTEXT
|
||||
local SUBSESSION=0
|
||||
@ -26,6 +28,8 @@ MAIN() {
|
||||
case $1 in
|
||||
--subsession ) SUBSESSION=$2; shift 1 ;;
|
||||
|
||||
-h | --help ) USAGE; return 0 ;;
|
||||
|
||||
* )
|
||||
[ $CONTEXT ] && ERROR "unexpected argument '$2'"
|
||||
CONTEXT=$1
|
||||
|
@ -17,6 +17,8 @@ MAIN() {
|
||||
|
||||
options:
|
||||
--subsession REDIS subsession (default 0)
|
||||
|
||||
-h, --help show this dialogue and exit
|
||||
"
|
||||
local NAMESPACE
|
||||
local SUBSESSION=0
|
||||
@ -26,6 +28,8 @@ MAIN() {
|
||||
case $1 in
|
||||
--subsession ) SUBSESSION=$2; shift 1 ;;
|
||||
|
||||
-h | --help ) USAGE; return 0 ;;
|
||||
|
||||
* )
|
||||
[ $NAMESPACE ] && ERROR "unexpected argument '$2'"
|
||||
NAMESPACE=$1
|
||||
|
Reference in New Issue
Block a user