rrichards
5/28/2019 - 3:44 PM

kube-ssh.sh

#!/usr/bin/env bash

while getopts ":u:" opt; do
  case "$opt" in
    u)
      username="${OPTARG}"
      ;;
    \?)
      echo "Invalid option: -${OPTARG}" >&2
      return 1
      ;;
    :)
      echo "Option -${OPTARG} requires an argument." >&2
      return 1
      ;;
  esac
done

shift "$((OPTIND-1))"

if [[ -z $1 ]]; then
  echo "Node name required." >&2
  return 1
else
  ip="$(kubectl get nodes $1 -o go-template='{{range .status.addresses}}{{if eq .type "InternalIP"}}{{print .address}}{{end}}{{end}}')"

  ssh "${username}@$ip"
fi