Created
March 20, 2020 14:22
-
-
Save tyzbit/b937797fa39799727de66018e3b6fc70 to your computer and use it in GitHub Desktop.
Onboard to k8s
This file contains 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 | |
## Use this script to configure kubectl for an EKS cluster using a role ARN. | |
function usage() { | |
echo "Usage: $0 [Amazon Role ARN]" | |
} | |
if [[ -z $1 ]]; then | |
usage | |
exit 1 | |
fi | |
rolearn=$1 | |
contextname=${rolearn##*/} | |
if [[ "${rolearn}" =~ "clusteradmin" ]]; then | |
clustername=${contextname%-*} | |
else | |
cluster_namespace=${contextname%-*} | |
clustername=${cluster_namespace%-*} | |
namespace_role=${contextname##${clustername}-} | |
namespace=${namespace_role%-*} | |
fi | |
# Update kubeconfig using aws-cli | |
aws eks --region us-east-2 update-kubeconfig \ | |
--name "${clustername}" \ | |
--role-arn "${rolearn}" \ | |
--alias "${contextname}" | |
# If the user is not a cluster admin, set their namespace for them | |
if [[ ! "${rolearn}" =~ "clusteradmin" ]]; then | |
# Set the namespace to the namespace specified in the role name | |
kubectl config set-context --current --namespace="${namespace}" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment