Skip to content

Instantly share code, notes, and snippets.

@RezhaBlue
Created January 11, 2019 18:39
Show Gist options
  • Save RezhaBlue/0d7fa0cf8594a643b0335ecc9f645ce3 to your computer and use it in GitHub Desktop.
Save RezhaBlue/0d7fa0cf8594a643b0335ecc9f645ce3 to your computer and use it in GitHub Desktop.
#!/bin/bash
echo -e "
# \e[33mPrerequisites:\e[39m
# ~/.aws/config and ~/.aws/credentials setup for cwdev and cw accounts.
# KUBECONFIG is setup and stored in ~/.kube directory.
#
# \e[32mUsage:\e[39m
# Update PROFILE_DEV & PROD w/respective names from ~/.aws/credentials file.
# Replace the path at end of script with your literal KUBECONFIG path, no variables.
# Source and Execute with a profile name (e.g. source ./this_script.sh jdoe).
#
# \e[31mWARNING:\e[39m Will install \e[35mjq\e[39m pkg, used to parse JSON, to your machine.
"
PROFILE_DEV="dev" # Modifiable
PROFILE_PROD="prod" # Modifiable
CLUSTER_SUFFIX="" # Unmodifiable; becomes dev | prod based on profile arg
# If jq is not installed, install jq
dpkg -l jq | grep -qw jq || curl -s 'https://api.github.com/users/lambda' | jq -r '.name'
# Set stack suffix based on profile
if [ "$1" = $PROFILE_DEV ]; then
CLUSTER_SUFFIX="dev"
export AWS_PROFILE=$PROFILE_DEV
elif [ "$1" = $PROFILE_PROD ]; then
CLUSTER_SUFFIX="prod"
export AWS_PROFILE=$PROFILE_PROD
else
echo "Unrecognized profile name. Acceptable names are $PROFILE_DEV and $PROFILE_PROD."
exit 1
fi
echo "AWS IAM Authenticator configured for [$AWS_PROFILE] using AWS_PROFILE env var."
# Get eks cluster info from aws, parse and store the endpoint and cert
EKS_JSON=$(aws eks describe-cluster --name pf-eksdefaultv1p11-$1 --profile $1)
export ENDPOINT=$(echo "$EKS_JSON" | jq -r '.cluster.endpoint')
CERT_AUTH=$(echo "$EKS_JSON" | jq -r '.cluster.certificateAuthority.data')
# Replace cluster name suffix, api endpoint and cert data with account specific parts
sed -ri "s+(pf-eksdefaultv1p11-).*\b+\1$CLUSTER_SUFFIX+g; s+(server: ).*+\1$ENDPOINT+; s+(certificate-authority-data: ).
*+\1$CERT_AUTH+" ~/.kube/config-cluster.yml
echo "Config file updated. Start executing kubeclt commands."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment