#!/bin/bash # Script to get kubeconfig from Terraform or Linode CLI # Usage: ./get-kubeconfig.sh [environment] set -e ENVIRONMENT="${1:-staging}" OUTPUT_FILE="${2:-/home/bobby/.kube/config}" echo "Getting kubeconfig for $ENVIRONMENT environment..." # Check if Terraform is available and state exists if command -v terraform &> /dev/null && [ -f "infra/terraform.tfstate" ]; then echo "đŸ“Ļ Getting kubeconfig from Terraform state..." # Get kubeconfig from postgres module output KUBECONFIG=$(terraform output -json | jq -r '.postgres.value.kubeconfig // empty') if [ -n "$KUBECONFIG" ]; then echo "$KUBECONFIG" > "$OUTPUT_FILE" echo "✅ Kubeconfig saved to $OUTPUT_FILE" else echo "âš ī¸ No kubeconfig found in Terraform state" echo "You may need to run 'terraform apply' first to get the cluster kubeconfig" fi elif command -v linode-cli &> /dev/null; then echo "đŸ“Ļ Getting kubeconfig from Linode CLI..." # List clusters to find the right one CLUSTERS=$(linode lke clusters-list --json) CLUSTER_ID=$(echo "$CLUSTERS" | jq -r ".[] | select(.label | contains(\"$ENVIRONMENT\")) | .id" 2>/dev/null | head -1) if [ -n "$CLUSTER_ID" ]; then echo "Found cluster $CLUSTER_ID" linode lke cluster-kubeconfig "$CLUSTER_ID" --raw > "$OUTPUT_FILE" echo "✅ Kubeconfig saved to $OUTPUT_FILE" else echo "âš ī¸ No Linode cluster found for $ENVIRONMENT" echo "Available clusters:" linode lke clusters fi else echo "â„šī¸ No Terraform or Linode CLI found" echo "Please manually get your kubeconfig from the Linode Dashboard:" echo "1. Go to Kubernetes in Linode Dashboard" echo "2. Select your cluster (hapi-$ENVIRONMENT)" echo "3. Click 'Kubeconfig' tab and copy the config" echo "4. Save to $OUTPUT_FILE" fi echo "" echo "Verify your setup with:" echo " kubectl cluster-info"