Skip to content

Instantly share code, notes, and snippets.

@tyzbit
Created March 20, 2020 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyzbit/b937797fa39799727de66018e3b6fc70 to your computer and use it in GitHub Desktop.
Save tyzbit/b937797fa39799727de66018e3b6fc70 to your computer and use it in GitHub Desktop.
Onboard to k8s
#!/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