From 61c334293f9f92e34de88393cf2f540e9f3af871 Mon Sep 17 00:00:00 2001 From: Matt Pryor Date: Thu, 25 Jul 2024 13:23:25 +0100 Subject: [PATCH] Add support for a new kube-connect script, independent of tilt-up (#168) --- Tiltfile | 2 +- bin/kube-connect | 119 +++++++++++++++++++++++++++++++++++++++++++++++ bin/tilt-up | 58 ++++++----------------- 3 files changed, 135 insertions(+), 44 deletions(-) create mode 100755 bin/kube-connect diff --git a/Tiltfile b/Tiltfile index eb791bc8..076fb012 100644 --- a/Tiltfile +++ b/Tiltfile @@ -6,7 +6,7 @@ TILT_IMAGES_UNAPPLY = os.path.abspath("./bin/tilt-images-unapply") # Allow the use of the azimuth-dev context -allow_k8s_contexts("azimuth-dev") +allow_k8s_contexts("azimuth") def deep_merge(dict1, dict2): diff --git a/bin/kube-connect b/bin/kube-connect new file mode 100755 index 00000000..a42d8c06 --- /dev/null +++ b/bin/kube-connect @@ -0,0 +1,119 @@ +#!/usr/bin/env bash + +##### +## This script uses Tilt (tilt.dev) to allow easier code development on the +## currently activated environment +##### + +set -eo pipefail + + +if [ -z "$AZIMUTH_CONFIG_ROOT" ] || [ -z "$AZIMUTH_CONFIG_ENVIRONMENT_ROOT" ]; then + echo "Please activate an environment" >&2 + exit 1 +fi + + +ansible_variable() { + ANSIBLE_LOAD_CALLBACK_PLUGINS=true \ + ANSIBLE_STDOUT_CALLBACK=json \ + ANSIBLE_JSON_INDENT=0 \ + ansible -m debug -a "var=$1" all | \ + jq -r -R "fromjson? | .plays[0].tasks[0].hosts.localhost.$1" +} + + +# Process the inputs +terminate_only="no" +socks_port="1080" +while [[ $# -gt 0 ]]; do + case $1 in + --terminate) + terminate_only="yes" + shift + ;; + -p|--socks-port) + socks_port="$2" + shift + shift + ;; + *) + shift + ;; + esac +done + + +# Find the working directory +# We will write a PID file and the kubeconfig file here +echo "Creating working directory..." >&2 +work_dir="$(ansible_variable work_directory)/kube-connect" +socks_pid_file="$work_dir/pid" +kubeconfig="$work_dir/config" + + +# If there is an existing PID file, kill the process and delete the old files +if [ -f "$socks_pid_file" ]; then + socks_pid="$(cat "$socks_pid_file")" + if kill -0 "$socks_pid" >/dev/null 2>&1; then + echo "Terminating existing SOCKS proxy..." >&2 + kill "$socks_pid" + rm -rf "$work_dir" + fi +fi + + +# If we are only doing a terminate, we are done +if [ "$terminate_only" = "yes" ]; then + exit +fi + + +# Make sure that the work directory exists +mkdir -p "$work_dir" + + +# Get the raw kubeconfig for the Azimuth cluster +echo "Fetching kubeconfig for Azimuth Kubernetes cluster..." >&2 +install_mode="$(ansible_variable install_mode)" +if [ "$install_mode" = "ha" ]; then + cluster_name="$(ansible_variable capi_cluster_release_name)" + kubeconfig_arg="KUBECONFIG=./kubeconfig-${cluster_name}.yaml" +fi +"$AZIMUTH_CONFIG_ROOT/bin/seed-ssh" \ + $kubeconfig_arg \ + kubectl config view --raw > "$kubeconfig" + + +# Add the SOCKS proxy config to the cluster +echo "Updating kubeconfig with SOCKS proxy..." >&2 +ctx="$(kubectl config current-context --kubeconfig $kubeconfig)" +cluster="$( \ + kubectl config view \ + --output jsonpath="{.contexts[?(@.name == \"$ctx\")].context.cluster}" \ + --kubeconfig $kubeconfig +)" +kubectl config set-cluster $cluster \ + --proxy-url "socks5://localhost:$socks_port" \ + --kubeconfig $kubeconfig \ + >/dev/null + +echo "Renaming context to azimuth..." >&2 +kubectl config rename-context $ctx azimuth --kubeconfig $kubeconfig >/dev/null + + +# Launch the SOCKS proxy and store the PID +echo "Starting SOCKS proxy..." >&2 +"$AZIMUTH_CONFIG_ROOT/bin/seed-ssh" -D $socks_port -N & +socks_pid="$!" +# Wait a few seconds and check that the process is running +sleep 5 +if ! kill -0 "$socks_pid" >/dev/null 2>&1; then + echo "Failed to connect to Azimuth cluster" >&2 + exit 1 +fi +echo "$socks_pid" > "$socks_pid_file" + + +# Echo the KUBECONFIG variable that needs to be set +echo "export KUBECONFIG=$kubeconfig" diff --git a/bin/tilt-up b/bin/tilt-up index 49aa8653..c67506de 100755 --- a/bin/tilt-up +++ b/bin/tilt-up @@ -30,71 +30,43 @@ ansible_variable() { } -# Variables to hold the PIDs of the SSH connection and tilt -ssh_pid= tilt_pid= work_dir= + # Function to terminate background processes when the script exits -terminate_bg_pids() { +terminate_bg_processes() { set +e # Make sure tilt up is dead test -n "$tilt_pid" && kill -0 "$tilt_pid" >/dev/null 2>&1 && kill "$tilt_pid" # Run tilt down tilt down - # Kill the SSH tunnel - test -n "$ssh_pid" && kill -0 "$ssh_pid" >/dev/null 2>&1 && kill "$ssh_pid" + # Disconnect from Kubernetes + "$AZIMUTH_CONFIG_ROOT/bin/kube-connect" --terminate # Clean up the working directory test -n "$work_dir" && rm -rf "$work_dir" } -trap 'terminate_bg_pids' EXIT +trap 'terminate_bg_processes' EXIT + # The SOCKS port is the only input socks_port="${1:-1080}" + # Make a working directory for tilt related stuff echo "Creating working directory..." -work_dir="$(ansible_variable work_directory)/tilt" -mkdir -p "$work_dir" -KUBECONFIG="$work_dir/kubeconfig" - - -echo "Fetching kubeconfig for Azimuth Kubernetes cluster..." -install_mode="$(ansible_variable install_mode)" -if [ "$install_mode" = "ha" ]; then - cluster_name="$(ansible_variable capi_cluster_release_name)" - kubeconfig_arg="KUBECONFIG=./kubeconfig-${cluster_name}.yaml" -fi -"$AZIMUTH_CONFIG_ROOT/bin/seed-ssh" \ - $kubeconfig_arg \ - kubectl config view --raw > "$KUBECONFIG" +export AZIMUTH_TILT_WORK_DIR="$(ansible_variable work_directory)/tilt" +mkdir -p "$AZIMUTH_TILT_WORK_DIR" -echo "Updating kubeconfig with SOCKS proxy..." -export KUBECONFIG -ctx="$(kubectl config current-context)" -cluster="$(kubectl config view -o jsonpath="{.contexts[?(@.name == \"$ctx\")].context.cluster}")" -kubectl config set-cluster $cluster --proxy-url="socks5://localhost:$socks_port" - -echo "Renaming context to azimuth-dev..." -kubectl config rename-context $ctx azimuth-dev - - -echo "Starting SOCKS proxy..." -"$AZIMUTH_CONFIG_ROOT/bin/seed-ssh" -D $socks_port -N & -ssh_pid="$!" -# Wait a few seconds and check that the process is running -sleep 5 -if ! kill -0 "$ssh_pid" >/dev/null 2>&1; then - echo "[ERROR] Failed to connect to Azimuth cluster" 1>&2 - exit 1 -fi +# Connect to the Azimuth Kubernetes cluster +echo "Connecting to Azimuth Kubernetes cluster..." +eval "$("$AZIMUTH_CONFIG_ROOT/bin/kube-connect" -p "$socks_port")" -# Use the working directory as TMP for tilt -export AZIMUTH_TILT_WORK_DIR="$work_dir" +# Run tilt echo "Running 'tilt up'..." tilt up & tilt_pid="$!" -# Spin until one of the processes exits -wait $ssh_pid $tilt_pid +# Spin until tilt exits +wait $tilt_pid