|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +usage() { |
| 5 | + echo "Usage: $0 --tf-dir <terragrunt directory> --context <k8s context alias>" |
| 6 | + exit 1 |
| 7 | +} |
| 8 | + |
| 9 | +parse_args() { |
| 10 | + while [[ $# -gt 0 ]]; do |
| 11 | + case "$1" in |
| 12 | + --tf-dir) |
| 13 | + TF_DIR="$2" |
| 14 | + shift 2 |
| 15 | + ;; |
| 16 | + --context) |
| 17 | + FROM_CTX="$2" |
| 18 | + shift 2 |
| 19 | + ;; |
| 20 | + *) |
| 21 | + echo "Unknown arg: $1" |
| 22 | + usage |
| 23 | + ;; |
| 24 | + esac |
| 25 | + done |
| 26 | + |
| 27 | + [[ -z "${TF_DIR:-}" || -z "${FROM_CTX:-}" ]] && usage |
| 28 | + |
| 29 | + TENANT=$(basename "$TF_DIR") |
| 30 | + [[ -z "${TENANT:-}" ]] && { |
| 31 | + echo "Error: Could not determine tenant from directory '$TF_DIR'" |
| 32 | + exit 1 |
| 33 | + } |
| 34 | + CONFIG_FILE="${TF_DIR}/config.yml" |
| 35 | +} |
| 36 | + |
| 37 | +detect_clusters() { |
| 38 | + CURRENT_CLUSTER=$(yq e '.arc_cluster_name' "$CONFIG_FILE") |
| 39 | + |
| 40 | + if [[ "$CURRENT_CLUSTER" == *"-green" ]]; then |
| 41 | + FROM="$CURRENT_CLUSTER" |
| 42 | + TO="${CURRENT_CLUSTER%-green}-blue" |
| 43 | + elif [[ "$CURRENT_CLUSTER" == *"-blue" ]]; then |
| 44 | + FROM="$CURRENT_CLUSTER" |
| 45 | + TO="${CURRENT_CLUSTER%-blue}-green" |
| 46 | + else |
| 47 | + echo "Cannot detect blue/green suffix in arc_cluster_name: $CURRENT_CLUSTER" |
| 48 | + exit 1 |
| 49 | + fi |
| 50 | +} |
| 51 | + |
| 52 | +scale_down_runners() { |
| 53 | + echo "🧯 Scaling down runners in namespace: $TENANT" |
| 54 | + for key in $(yq -r '.arc_runner_specs | keys | .[]' "$CONFIG_FILE"); do |
| 55 | + echo "Checking runner set: $key" |
| 56 | + if kubectl --context "$FROM_CTX" get autoscalingrunnersets.actions.github.com -n "$TENANT" "$key" &>/dev/null; then |
| 57 | + echo "Scaling down runner set: $key" |
| 58 | + kubectl --context "$FROM_CTX" patch autoscalingrunnersets.actions.github.com -n "$TENANT" "$key" --type merge -p '{"spec":{"minRunners":0,"maxRunners":0}}' |
| 59 | + else |
| 60 | + echo "Runner set $key not found, skipping." |
| 61 | + fi |
| 62 | + done |
| 63 | + |
| 64 | + echo "⏳ Waiting for runner pods to terminate..." |
| 65 | + while kubectl --context "$FROM_CTX" get pods -n "$TENANT" | grep -q runner; do |
| 66 | + sleep 5 |
| 67 | + done |
| 68 | +} |
| 69 | + |
| 70 | +terragrunt_apply() { |
| 71 | + local target="$1" |
| 72 | + echo "🔧 Applying Terragrunt target: $target" |
| 73 | + terragrunt apply --target "$target" -working-dir "$TF_DIR" -non-interactive -auto-approve |
| 74 | +} |
| 75 | + |
| 76 | +update_config() { |
| 77 | + local migrate_flag="$1" |
| 78 | + local cluster_name="$2" |
| 79 | + |
| 80 | + yq e -i ".migrate_arc_cluster = $migrate_flag" "$CONFIG_FILE" |
| 81 | + yq e -i ".arc_cluster_name = \"$cluster_name\"" "$CONFIG_FILE" |
| 82 | +} |
| 83 | + |
| 84 | +main() { |
| 85 | + parse_args "$@" |
| 86 | + detect_clusters |
| 87 | + |
| 88 | + echo "🔄 Migrating tenant '$TENANT' from '$FROM' to '$TO'..." |
| 89 | + echo "📄 Editing config: $CONFIG_FILE" |
| 90 | + echo "🔍 Using Kubernetes context: $FROM_CTX" |
| 91 | + |
| 92 | + # Step 1 |
| 93 | + scale_down_runners |
| 94 | + |
| 95 | + # Step 2 |
| 96 | + echo "🛑 Disabling ARC for tenant on old cluster '$FROM'" |
| 97 | + update_config true "$FROM" |
| 98 | + terragrunt_apply 'module.arc_runners' |
| 99 | + |
| 100 | + # Step 3 |
| 101 | + echo "🚀 Enabling ARC for tenant on new cluster '$TO'" |
| 102 | + update_config false "$TO" |
| 103 | + terragrunt_apply 'module.arc_runners' |
| 104 | + |
| 105 | + echo "✅ Migration complete. Tenant '$TENANT' is now on '$TO'" |
| 106 | +} |
| 107 | + |
| 108 | +main "$@" |
0 commit comments