dotfiles/home/.bin/gs-login

84 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
source .utils
DEBUG=""
print_usage() {
orange "gs-login - login to Giant Swarm managed clusters"
echo " "
underline "Usage:"
echo "gs-login [INSTALLATION] [WORKLOAD CLUSTER] [ORGANISATION]"
echo " "
underline "Examples:"
echo "> gs-login gauss"
echo "> gs-login gauss mywc1"
echo " "
underline "Options:"
echo "-h, --help show this help text"
}
POS_ARGS=()
while test $# -gt 0; do
case "$1" in
-t|--ttl)
shift
echo "-t / --ttl no longer handled"
shift
;;
-g|--certificate-group)
shift
echo "-g / --certificate-group no longer handled"
shift
;;
-h|--help)
print_usage
exit 0
;;
--debug)
DEBUG="--level=debug"
shift
;;
/)
# We want to ignore slash seperators between MC and WC
shift
;;
*)
POS_ARGS+=(`echo $1 | tr '/' ' '`)
shift
;;
esac
done
if [ ${#POS_ARGS[@]} -eq 0 ]; then
POS_ARGS+=(`opsctl list installations --short | tr ' ' '\n' | fzf`)
fi
case ${#POS_ARGS[@]} in
0)
print_usage
exit 1
;;
*)
kubectl config delete-context gs-${POS_ARGS[0]} &>/dev/null
TELEPORT_CLUSTER_NAME="$(echo "${POS_ARGS[@]}" | tr ' ' '-')"
TELEPORT_SUPPORTED=$(tsh kube ls -f json --query "name == \"${TELEPORT_CLUSTER_NAME}\"" 2>/dev/null | jq '. | length')
if [[ "${TELEPORT_SUPPORTED}" == "0" ]]; then
# Teleport not supported, old style login
echo "Cluster isn't know to Teleport, using old login method"
opsctl login ${DEBUG} ${POS_ARGS[@]}
else
echo "Logging in with Teleport. Cluster: '${TELEPORT_CLUSTER_NAME}'"
# Make sure that caching is disabled to avoid issues with cross-cluster cache pollution
TELEPORT_CACHE_DIR="${HOME}/.kube/cache/discovery/teleport.giantswarm.io_443"
if [[ "$(readlink -f ${TELEPORT_CACHE_DIR})" != "/dev/null" ]]; then
rm -rf ${TELEPORT_CACHE_DIR}
ln -s /dev/null ${TELEPORT_CACHE_DIR}
fi
tsh kube login ${TELEPORT_CLUSTER_NAME}
fi
;;
esac