2023-08-29 00:42:38 +00:00
|
|
|
#####################################################################
|
|
|
|
|
|
|
|
DEPENDENCIES+=(
|
|
|
|
kubectl
|
|
|
|
)
|
|
|
|
|
|
|
|
REQUIRED_ENV+=()
|
|
|
|
|
|
|
|
use redis --group kubectl
|
|
|
|
|
|
|
|
#####################################################################
|
|
|
|
|
|
|
|
KUBECTL() {
|
|
|
|
local NAMESPACE=$(REDIS get --prefix "current:namespace")
|
|
|
|
local CONTEXT=$(KUBECTL__GET_CONTEXT)
|
|
|
|
|
|
|
|
local KUBECTL_ARGS=()
|
|
|
|
[ $NAMESPACE ] && KUBECTL_ARGS+=(--namespace $NAMESPACE)
|
|
|
|
[ $CONTEXT ] && KUBECTL_ARGS+=(--context $CONTEXT)
|
|
|
|
|
|
|
|
kubectl ${KUBECTL_ARGS[@]} $@
|
|
|
|
}
|
|
|
|
|
|
|
|
#####################################################################
|
|
|
|
|
|
|
|
KUBECTL__GET_CONTEXT() { REDIS get --prefix "current:context"; }
|
|
|
|
|
|
|
|
KUBECTL__SET_CONTEXT() {
|
|
|
|
local CONTEXT=$1
|
|
|
|
[ ! $CONTEXT ] && return 1
|
|
|
|
|
2023-08-30 23:26:13 +00:00
|
|
|
[[ $CONTEXT =~ reset ]] && {
|
2023-08-29 00:42:38 +00:00
|
|
|
: \
|
|
|
|
&& REDIS del --prefix "current:context" \
|
2023-08-30 23:26:13 +00:00
|
|
|
&& KUBECTL__SET_NAMESPACE reset \
|
2023-08-29 00:42:38 +00:00
|
|
|
;
|
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
|
|
|
: \
|
|
|
|
&& REDIS set --prefix "current:context" "$CONTEXT" \
|
2023-08-30 23:26:13 +00:00
|
|
|
&& KUBECTL__SET_NAMESPACE reset \
|
2023-08-29 00:42:38 +00:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
KUBECTL__SELECT_CONTEXT() {
|
|
|
|
KUBECTL__LIST_CONTEXTS | FZF 'select a context'
|
|
|
|
}
|
|
|
|
|
|
|
|
KUBECTL__LIST_CONTEXTS() {
|
2023-08-30 23:26:13 +00:00
|
|
|
echo reset
|
|
|
|
local ALL_CONTEXTS=$(KUBECTL config get-contexts -o name | sort)
|
|
|
|
|
|
|
|
echo $ALL_CONTEXTS | grep -v '^arn:aws:eks'
|
|
|
|
|
|
|
|
[[ $AWS_ACCOUNT ]] && {
|
|
|
|
echo $ALL_CONTEXTS | grep "^arn:aws:eks:.*:$AWS_ACCOUNT"
|
|
|
|
true
|
|
|
|
} || {
|
|
|
|
echo $ALL_CONTEXTS | grep '^arn:aws:eks'
|
|
|
|
}
|
2023-08-29 00:42:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#####################################################################
|
|
|
|
|
|
|
|
KUBECTL__GET_NAMESPACE() { REDIS get --prefix "current:namespace"; }
|
|
|
|
|
|
|
|
KUBECTL__SET_NAMESPACE() {
|
|
|
|
local NAMESPACE=$1
|
|
|
|
[ ! $NAMESPACE ] && return 1
|
|
|
|
|
2023-08-30 23:26:13 +00:00
|
|
|
[[ $NAMESPACE =~ reset ]] && {
|
2023-08-29 00:42:38 +00:00
|
|
|
REDIS del --prefix "current:namespace"
|
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
|
|
|
REDIS set --prefix "current:namespace" "$NAMESPACE"
|
|
|
|
}
|
|
|
|
|
|
|
|
KUBECTL__SELECT_NAMESPACE() {
|
|
|
|
KUBECTL__LIST_NAMESPACES | FZF 'select a namespace'
|
|
|
|
}
|
|
|
|
|
|
|
|
KUBECTL__LIST_NAMESPACES() {
|
2023-08-30 23:26:13 +00:00
|
|
|
echo reset
|
2023-08-29 00:42:38 +00:00
|
|
|
echo default
|
|
|
|
KUBECTL get namespaces -o name | sed 's/^namespace\///' | sort
|
|
|
|
}
|
2023-11-22 22:54:16 +00:00
|
|
|
|
|
|
|
#####################################################################
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2024-04-13 23:10:16 +00:00
|
|
|
REMINDER "attempting to serve ${NAMESPACE}/${SERVICE_NAME}:${SERVICE_PORT}"
|
|
|
|
[ $SERVICE_PASSWORD ] && REMINDER "password : $SERVICE_PASSWORD"
|
2023-11-22 22:54:16 +00:00
|
|
|
|
|
|
|
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|/.*$||')
|
|
|
|
}
|