Last active
February 4, 2025 14:19
-
-
Save kyontan/5f1774b1e3426edaabd148d69061ebef to your computer and use it in GitHub Desktop.
with-app-env
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if [ -w /dev/stderr ] | |
then | |
STDERR=/dev/stderr | |
else | |
STDERR=/dev/tty # The only reason is that stderr is a link to console | |
fi | |
function echo_info() { | |
# echo "[INFO] $1" > /dev/stderr | |
# colored output | |
echo -e "\033[1;32m[INFO]\033[0m $1" > $STDERR | |
} | |
function echo_error() { | |
# echo "[ERROR] $1" > /dev/stderr | |
# colored output | |
echo -e "\033[1;31m[ERROR]\033[0m $1" > $STDERR | |
} | |
function underline() { | |
echo -e "\033[4m$1\033[0m" | |
} | |
function print_usage() { | |
echo "Usage: with-app-env [-n namespace] <deployment or rollout> -- <command ...>" > $STDERR | |
echo | |
echo "Environment variables:" | |
echo " KUBE_NAMESPACE (optional): Kubernetes namespace" | |
} | |
if [ $# -lt 3 ]; then | |
print_usage | |
exit 1 | |
fi | |
if [ $1 == "-n" ]; then | |
KUBE_NAMESPACE=$2 | |
shift 2 | |
fi | |
if [ $2 != "--" ]; then | |
echo_error "Invalid command format" | |
print_usage | |
exit 1 | |
fi | |
DEPLOYMENT=$1 | |
shift 2 | |
command=() | |
deployment_json=$(mktemp) | |
trap 'rm -f $deployment_json' EXIT | |
if [ -n "$KUBE_NAMESPACE" ]; then | |
kubectl="kubectl -n $KUBE_NAMESPACE" | |
export KUBE_NAMESPACE | |
else | |
kubectl="kubectl" | |
fi | |
$kubectl get deploy $DEPLOYMENT -o json > $deployment_json 2>/dev/null | |
if [ $? -eq 0 ]; then | |
echo_info "Found Deployment: $DEPLOYMENT" | |
else | |
$kubectl get rollout $DEPLOYMENT -o json > $deployment_json 2>/dev/null | |
if [ $? -ne 0 ]; then | |
echo_error "Deployment or Rollout not found: $DEPLOYMENT" | |
exit 1 | |
fi | |
echo_info "Found Rollout: $DEPLOYMENT" | |
fi | |
container_env=$(mktemp) | |
trap 'rm -f $container_env' EXIT | |
cat $deployment_json | | |
jq -r '.spec.template.spec.containers[0].env[] | select(.value != null) | "\(.name)=\"\(.value)\""' | | |
while read -r env; do | |
echo "export $env" >> $container_env | |
done | |
source $container_env | |
CONFIGMAP_NAME=$(cat $deployment_json | jq -r '.spec.template.spec.containers[0].envFrom[].configMapRef.name | select(. != null)' | head -1) | |
if [ -n "$CONFIGMAP_NAME" ]; then | |
echo_info "Kubernetes ConfigMap: $CONFIGMAP_NAME" | |
export WITH_CONFIG=$CONFIGMAP_NAME | |
command+=(with-config) | |
fi | |
AWSSECRET_NAME=$(cat $deployment_json | jq -r '.spec.template.spec.containers[0].envFrom[].secretRef.name | select(. != null)' | head -1) | |
if [ -n "$AWSSECRET_NAME" ]; then | |
# echo "Searching AWSSecret: $AWSSECRET_NAME" | |
awssecret_json=$(mktemp) | |
trap 'rm -f $awssecret_json' EXIT | |
$kubectl get awssecret $AWSSECRET_NAME -o json > $awssecret_json | |
AWSSECRET_ID=$(cat $awssecret_json | jq -r ".spec.stringDataFrom.secretsManagerSecretRef.secretId") | |
AWSSECRET_VERSION_ID=$(cat $awssecret_json | jq -r ".spec.stringDataFrom.secretsManagerSecretRef.versionId") | |
echo_info "Found secret in AWS Secret Manager: $AWSSECRET_ID (versionId: $AWSSECRET_VERSION_ID)" | |
export WITH_SECRET_ID=$AWSSECRET_ID | |
export WITH_SECRET_VERSION_ID=$AWSSECRET_VERSION_ID | |
command+=(with-secret) | |
fi | |
command=(${command[@]} "$@") | |
exec "${command[@]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment