Skip to content

Commit

Permalink
v3.8.0
Browse files Browse the repository at this point in the history
=====================================================================

--- 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
  • Loading branch information
wrynegade committed Nov 22, 2023
1 parent a03885e commit 72e831d
Show file tree
Hide file tree
Showing 14 changed files with 354 additions and 37 deletions.
12 changes: 6 additions & 6 deletions plugins/kubectl/driver/kubectl.driver.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
;;

Expand Down Expand Up @@ -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'"
Expand Down
11 changes: 9 additions & 2 deletions plugins/kubectl/driver/meta.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
;;

Expand Down
71 changes: 70 additions & 1 deletion plugins/kubectl/lib/kubectl.module.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ KUBECTL() {
kubectl ${KUBECTL_ARGS[@]} $@
}


#####################################################################

KUBECTL__GET_CONTEXT() { REDIS get --prefix "current:context"; }
Expand Down Expand Up @@ -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 changes: 58 additions & 0 deletions plugins/kubectl/serve
Original file line number Diff line number Diff line change
@@ -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 $@
4 changes: 4 additions & 0 deletions plugins/kubectl/set-context
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ MAIN() {
options:
--subsession REDIS subsession (default 0)
-h, --help show this dialogue and exit
"
local CONTEXT
local SUBSESSION=0
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions plugins/kubectl/set-namespace
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ MAIN() {
options:
--subsession REDIS subsession (default 0)
-h, --help show this dialogue and exit
"
local NAMESPACE
local SUBSESSION=0
Expand All @@ -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
Expand Down
13 changes: 11 additions & 2 deletions run
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ __RUN() {
OPTIONS
-g, --group <group-name> only use scripts from the indicated group
-t, --type <type-name> only use scripts of the indicated type
-m, --name <scwrypt-name> only run the script if there is an exact match
-m, --name <scwrypt-name> only run the script if there is an exact match
(requires type and group)
-y, --yes auto-accept all [yn] prompts through current scwrypt
-e, --env <env-name> set environment; overwrites SCWRYPTS_ENV
-n, --no-log skip logging and run in quiet mode
Expand Down Expand Up @@ -56,14 +57,22 @@ __RUN() {
VARSPLIT=$(echo "$1 " | sed 's/^\(-.\)\(.*\) /\1 -\2/')
set -- $(echo " $VARSPLIT ") ${@:2}
;;

-h | --help )
USAGE
return 0
;;

-n | --no-log )
[ ! $SUBSCWRYPT ] && SUBSCWRYPT=0
shift 1
;;

-y | --yes )
export __SCWRYPTS_YES=1
shift 1
;;

-e | --env )
[ ! $2 ] && ERROR "missing value for argument $1" && break
[ ! $SUBSCWRYPTS ] \
Expand Down Expand Up @@ -135,7 +144,7 @@ __RUN() {

local SCWRYPTS_AVAILABLE
local POTENTIAL_ERROR="no such scwrypt exists:"

SCWRYPTS_AVAILABLE=$(SCWRYPTS__GET_AVAILABLE_SCWRYPTS)

[ $SEARCH_NAME ] && {
Expand Down
2 changes: 1 addition & 1 deletion zsh/cloud/aws/eks/login
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ use cloud/aws/eks
CHECK_ENVIRONMENT
#####################################################################

EKS_CLUSTER_LOGIN $@
EKS__CLUSTER_LOGIN $@
44 changes: 35 additions & 9 deletions zsh/lib/cloud/aws/eks.module.zsh
Original file line number Diff line number Diff line change
@@ -1,19 +1,44 @@
#####################################################################

DEPENDENCIES+=(
kubectl
)

REQUIRED_ENV+=(
AWS_ACCOUNT
AWS_REGION
)
DEPENDENCIES+=(kubectl yq)
REQUIRED_ENV+=()

use cloud/aws/cli

#####################################################################

EKS_CLUSTER_LOGIN() {
EKS__KUBECTL() { EKS kubectl $@; }
EKS__FLUX() { EKS flux $@; }

#####################################################################

EKS() {
local USAGE="
usage: cli [...kubectl args...]
args:
cli a kubectl-style CLI (e.g. kubectl, helm, flux, etc)
Allows access to kubernetes CLI commands by configuring environment
to point to a specific cluster.
"

REQUIRED_ENV=(AWS_REGION AWS_ACCOUNT CLUSTER_NAME) DEPENDENCIES=(kubectl $1) CHECK_ENVIRONMENT || return 1

local CONTEXT="arn:aws:eks:${AWS_REGION}:${AWS_ACCOUNT}:cluster/${CLUSTER_NAME}"

local CONTEXT_ARGS=()
case $1 in
helm ) CONTEXT_ARGS+=(--kube-context $CONTEXT) ;;
* ) CONTEXT_ARGS+=(--context $CONTEXT) ;;
esac

$1 ${CONTEXT_ARGS[@]} ${@:2}
}

#####################################################################

EKS__CLUSTER_LOGIN() {
local USAGE="
usage: [...options...]
Expand All @@ -25,6 +50,7 @@ EKS_CLUSTER_LOGIN() {
cluster in EKS. Also creates the kubeconfig entry if it does not
already exist.
"
REQUIRED_ENV=(AWS_ACCOUNT AWS_REGION) CHECK_ENVIRONMENT || return 1

local CLUSTER_NAME

Expand Down
Loading

0 comments on commit 72e831d

Please sign in to comment.