From 2d78ed73c96bd38e6654bc7317995f750c55d607 Mon Sep 17 00:00:00 2001 From: Marcus Noble Date: Fri, 30 Aug 2024 13:42:25 +0100 Subject: [PATCH] Added gs-ssh Signed-off-by: Marcus Noble --- home/.bin/gs-ssh | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100755 home/.bin/gs-ssh diff --git a/home/.bin/gs-ssh b/home/.bin/gs-ssh new file mode 100755 index 0000000..936c568 --- /dev/null +++ b/home/.bin/gs-ssh @@ -0,0 +1,75 @@ +#!/usr/bin/env bash + +source .utils + +DEBUG="" + +print_usage() { + orange "gs-ssh - ssh to Giant Swarm managed cluster node" + echo " " + underline "Usage:" + echo "gs-ssh [CLUSTER NAME] [NODE NAME]" + echo " " + underline "Examples:" + echo "> gs-ssh gauss" + echo "> gs-ssh gauss ip-1-2-3-4.ey-west-1.compute.internal" + echo " " + underline "Options:" + echo "-h, --help show this help text" +} + +POS_ARGS=() + +while test $# -gt 0; do + case "$1" in + -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 + +TELEPORT_CLUSTER_NAME="$(echo "${POS_ARGS[0]}" | tr ' ' '-')" +TELEPORT_SUPPORTED=$(tsh kube ls -f json --query "name == \"${TELEPORT_CLUSTER_NAME}\"" 2>/dev/null | jq '. | length') + +if [ ${#POS_ARGS[@]} -eq 1 ]; then + if [[ "${TELEPORT_SUPPORTED}" == "0" ]]; then + orange "Node name must be specified if cluster doesn't support Teleport" + exit 1 + else + POS_ARGS+=(`tsh ls -f names cluster=${POS_ARGS[0]} | fzf`) + fi +fi + +kubectl config delete-context gs-${POS_ARGS[0]} &>/dev/null + +if [[ "${TELEPORT_SUPPORTED}" == "0" ]]; then + # Teleport not supported, old style login + echo "Cluster isn't know to Teleport, using old ssh method" + opsctl ssh ${DEBUG} ${POS_ARGS[@]} +else + echo "SSHing with Teleport. Cluster: '${TELEPORT_CLUSTER_NAME}' Node: ${POS_ARGS[1]}" + # 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 ssh root@cluster=${POS_ARGS[0]},node=${POS_ARGS[1]} +fi