Files
homelab/terraform/scripts/phase-1-storageclass-import.sh
T
Story Crater BotandClaude Haiku 4.5 23ec31bd6d feat(terraform): import Longhorn StorageClasses and app PVCs to Terraform state
- Phase 1: longhorn, longhorn-kafka StorageClasses (cluster-wide defaults)
- Phase 2 pilot: grafana, loki, portainer, forgejo PVCs
- All imports protected by lifecycle.prevent_destroy
- Removes Helm annotations (meta.helm.sh/*) to prevent dual-ownership conflicts
- Remote state backend (MinIO S3) syncs automatically on plan/apply
- Import-only approach: zero data loss, existing volumes untouched
- See terraform/LONGHORN_PVC_IMPORT.md for execution record

Co-Authored-By: Claude Haiku 4.5 <[email protected]>
2026-07-15 12:22:35 -07:00

88 lines
3.1 KiB
Bash
Executable File

#!/bin/bash
# Phase 1 — StorageClass import (longhorn + longhorn-kafka)
# Captures live spec, verifies against resource blocks, imports to state
set -e
cd "$(dirname "$0")/.."
echo "=== Phase 1: StorageClass Import ==="
echo ""
# Verify cluster connection
echo "Checking cluster connection..."
kubectl cluster-info || { echo "ERROR: No cluster access"; exit 1; }
echo ""
# Capture live longhorn SC spec
echo "--- Capturing live 'longhorn' StorageClass spec ---"
kubectl get sc longhorn -o yaml > /tmp/longhorn-live.yaml
echo "Saved to /tmp/longhorn-live.yaml"
echo "Contents:"
cat /tmp/longhorn-live.yaml
echo ""
# Capture live longhorn-kafka SC spec
echo "--- Capturing live 'longhorn-kafka' StorageClass spec ---"
kubectl get sc longhorn-kafka -o yaml > /tmp/longhorn-kafka-live.yaml
echo "Saved to /tmp/longhorn-kafka-live.yaml"
echo "Contents:"
cat /tmp/longhorn-kafka-live.yaml
echo ""
# Verify terraform resource blocks match live spec
echo "--- Verifying terraform/longhorn.tf resource blocks against live specs ---"
echo "MANUAL STEP: Compare the captured specs above against terraform/longhorn.tf"
echo " 1. Check 'provisioner', 'reclaimPolicy', 'volumeBindingMode', 'allowVolumeExpansion'"
echo " 2. Check 'parameters' (numberOfReplicas, staleReplicaTimeout, fsType, dataLocality)"
echo " 3. Fix terraform/longhorn.tf if any diffs found, then re-run terraform plan"
echo ""
read -p "Press Enter once you've verified the Terraform blocks match live specs: " _ || true
echo ""
# Pre-import terraform plan
echo "--- Pre-import terraform plan (should be clean) ---"
terraform plan -out=/tmp/pre-import.plan || { echo "ERROR: terraform plan failed"; exit 1; }
echo "Plan saved to /tmp/pre-import.plan"
echo ""
# Import longhorn SC
echo "--- Importing 'longhorn' StorageClass ---"
terraform import kubernetes_storage_class.longhorn longhorn || {
echo "ERROR: terraform import longhorn failed"
exit 1
}
echo "Import successful"
echo ""
# Plan after first import
echo "--- Terraform plan after importing 'longhorn' (must show 0 to add/change/destroy) ---"
terraform plan || { echo "ERROR: terraform plan failed"; exit 1; }
echo ""
read -p "Press Enter if plan shows zero changes; otherwise abort and fix: " _ || true
echo ""
# Import longhorn-kafka SC
echo "--- Importing 'longhorn-kafka' StorageClass ---"
terraform import kubernetes_storage_class.longhorn_kafka longhorn-kafka || {
echo "ERROR: terraform import longhorn-kafka failed"
exit 1
}
echo "Import successful"
echo ""
# Plan after second import
echo "--- Terraform plan after importing 'longhorn-kafka' (must show 0 to add/change/destroy) ---"
terraform plan || { echo "ERROR: terraform plan failed"; exit 1; }
echo ""
read -p "Press Enter if plan shows zero changes; otherwise abort and fix: " _ || true
echo ""
# Annotate longhorn-kafka with helm.sh/resource-policy=keep
echo "--- Annotating 'longhorn-kafka' with helm.sh/resource-policy=keep ---"
kubectl annotate storageclass longhorn-kafka helm.sh/resource-policy=keep --overwrite
echo "Annotation added"
echo ""
echo "=== Phase 1 Complete ==="
echo "StorageClasses imported successfully. Ready to proceed to Phase 2 (PVC imports)."