dotfiles/home/.bin/gs-login
Marcus Noble 1cfabac2f5
Fix login fallback
Signed-off-by: Marcus Noble <github@marcusnoble.co.uk>
2021-12-14 15:53:44 +00:00

79 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
TTL="8h"
CERTIFICATE_GROUP="system:masters"
DEBUG=""
print_usage() {
echo "gs-login - login to Giant Swarm managed clusters"
echo " "
echo "gs-login [INSTALLATION] [WORKLOAD CLUSTER] [ORGANISATION]"
echo " "
echo "Examples:"
echo "> gs-login gauss"
echo "> gs-login gauss mywc1"
echo " "
echo "Options:"
echo "-h, --help show this help text"
echo "-t, --ttl the certificate ttl for the workload cluster login (default: 8h)"
echo "-g, --certificate-group the certificate group to login as on the workload cluster (default: system:masters)"
}
POS_ARGS=()
while test $# -gt 0; do
case "$1" in
-t|--ttl)
shift
TTL=$1
shift
;;
-g|--certificate-group)
shift
CERTIFICATE_GROUP=$1
shift
;;
-h|--help)
print_usage
exit 0
;;
--debug)
DEBUG="--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
MC_EXIST=$(kubectl config get-contexts --no-headers -o name gs-${POS_ARGS[0]} 2>/dev/null | wc -l | xargs)
case ${#POS_ARGS[@]} in
1)
kubectl config delete-context gs-${POS_ARGS[0]} &>/dev/null
kubectl gs login ${POS_ARGS[0]} ${DEBUG} 2>/dev/null || opsctl kgs login -i ${POS_ARGS[0]} || opsctl create kubeconfig -i ${POS_ARGS[0]}
;;
2)
if [ ${MC_EXIST} -eq 0 ]; then gs-login ${POS_ARGS[0]}; fi
kubectl gs login ${POS_ARGS[0]} --workload-cluster ${POS_ARGS[1]} --certificate-group ${CERTIFICATE_GROUP} --certificate-ttl ${TTL} ${DEBUG}
;;
3)
if [ ${MC_EXIST} -eq 0 ]; then gs-login ${POS_ARGS[0]}; fi
kubectl gs login ${POS_ARGS[0]} --workload-cluster ${POS_ARGS[1]} --certificate-group ${CERTIFICATE_GROUP} --certificate-ttl ${TTL} --organization ${POS_ARGS[2]} ${DEBUG}
;;
*)
print_usage
exit 1
;;
esac