feature/tofu (#15)
@thomas.solbjor her er "import" av tofu fra ditt repo med justeringer for å tilpasse patterns her. Også minimalisert til å kun opprette cluster, ingen managed services som postgres etc. Ta en titt. Co-authored-by: Danijel Simeunovic <danijel.simeunovic@fortedigital.com> Reviewed-on: #15 Reviewed-by: Danijel Simeunovic <danijel.simeunovic@fortedigital.com> Co-authored-by: Ghost <> Co-committed-by: Ghost <>
This commit was merged in pull request #15.
This commit is contained in:
66
.tofu/scripts/get-kubeconfig.sh
Normal file
66
.tofu/scripts/get-kubeconfig.sh
Normal file
@@ -0,0 +1,66 @@
|
||||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
TOFU_ROOT="$(dirname "$SCRIPT_DIR")"
|
||||
PROJECT_ROOT="$(dirname "$TOFU_ROOT")"
|
||||
|
||||
CLUSTER="${1:?Usage: $0 <cluster> (e.g., aks-dev, eks-prod)}"
|
||||
PLATFORM="${CLUSTER%%-*}"
|
||||
ENV="${CLUSTER#*-}"
|
||||
|
||||
KUBECONFIG_FILE="$PROJECT_ROOT/private/$CLUSTER/kubeconfig"
|
||||
|
||||
if [[ -f "$KUBECONFIG_FILE" ]]; then
|
||||
echo "Kubeconfig already exists: $KUBECONFIG_FILE"
|
||||
echo ""
|
||||
echo " export KUBECONFIG=$KUBECONFIG_FILE"
|
||||
else
|
||||
echo "No cached kubeconfig. Fetching from platform..."
|
||||
|
||||
# Load platform credentials
|
||||
ENV_FILE="$TOFU_ROOT/configs/$PLATFORM.env"
|
||||
if [[ -f "$ENV_FILE" ]]; then
|
||||
set -a; source "$ENV_FILE"; set +a
|
||||
fi
|
||||
|
||||
TOFU_DIR="$TOFU_ROOT/platforms/$PLATFORM/$ENV"
|
||||
mkdir -p "$(dirname "$KUBECONFIG_FILE")"
|
||||
|
||||
case "$PLATFORM" in
|
||||
aks)
|
||||
cd "$TOFU_DIR"
|
||||
RG=$(tofu output -raw resource_group_name 2>/dev/null || echo "$CLUSTER-rg")
|
||||
NAME=$(tofu output -raw cluster_name 2>/dev/null || echo "$CLUSTER")
|
||||
az aks get-credentials --resource-group "$RG" --name "$NAME" --file "$KUBECONFIG_FILE" --overwrite-existing
|
||||
;;
|
||||
eks)
|
||||
cd "$TOFU_DIR"
|
||||
NAME=$(tofu output -raw cluster_name 2>/dev/null || echo "$CLUSTER")
|
||||
REGION=$(tofu output -raw aws_region 2>/dev/null || echo "${AWS_REGION:-eu-west-1}")
|
||||
aws eks update-kubeconfig --name "$NAME" --region "$REGION" --kubeconfig "$KUBECONFIG_FILE"
|
||||
;;
|
||||
gke)
|
||||
cd "$TOFU_DIR"
|
||||
NAME=$(tofu output -raw cluster_name 2>/dev/null || echo "$CLUSTER")
|
||||
REGION=$(tofu output -raw region 2>/dev/null || echo "${GCP_REGION:-europe-west4}")
|
||||
PROJECT=$(tofu output -raw project_id 2>/dev/null || echo "${GCP_PROJECT_ID:-}")
|
||||
gcloud container clusters get-credentials "$NAME" --region "$REGION" --project "$PROJECT"
|
||||
cp ~/.kube/config "$KUBECONFIG_FILE"
|
||||
;;
|
||||
upc)
|
||||
cd "$TOFU_DIR"
|
||||
CLUSTER_ID=$(tofu output -raw cluster_id 2>/dev/null || echo "${UPCLOUD_CLUSTER_ID:-}")
|
||||
upctl kubernetes config "$CLUSTER_ID" > "$KUBECONFIG_FILE"
|
||||
;;
|
||||
*)
|
||||
echo "Error: unknown platform '$PLATFORM'"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
||||
chmod 600 "$KUBECONFIG_FILE"
|
||||
echo "Kubeconfig saved: $KUBECONFIG_FILE"
|
||||
echo ""
|
||||
echo " export KUBECONFIG=$KUBECONFIG_FILE"
|
||||
fi
|
||||
Reference in New Issue
Block a user