From d4b7d54dd66692129518a49cc449187ebd519a73 Mon Sep 17 00:00:00 2001 From: dvirlabs Date: Sun, 22 Jun 2025 02:50:22 +0300 Subject: [PATCH] Test the script --- automation/cloudflared-sync.sh | 47 +++++++++++++++++++--------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/automation/cloudflared-sync.sh b/automation/cloudflared-sync.sh index b833c14..b5f4ce5 100644 --- a/automation/cloudflared-sync.sh +++ b/automation/cloudflared-sync.sh @@ -1,7 +1,7 @@ #!/bin/bash set -e -# 📦 Install required tools +# 📦 Install required tools (run via Woodpecker .woodpecker.yml) apk add --no-cache git bash curl yq jq echo "🔍 Scanning for apps with cname.yaml..." @@ -20,13 +20,15 @@ ORIGINAL_FILE="$INFRA_CLONE/manifests/cloudflared/values.yaml" MERGED_FILE="$INFRA_CLONE/manifests/cloudflared/values.yaml" # === STEP 1: Clone Repos === -echo "📦 Cloning sandbox..." +echo "📦 Cloning sandbox-apps..." git clone --depth=1 "$SANDBOX_REPO_URL" "$SANDBOX_CLONE" echo "📦 Cloning infra..." git clone --depth=1 "$INFRA_REPO_URL" "$INFRA_CLONE" -# === STEP 2: Generate new ingress entries === +ls -l .tmp-repos/ + +# === STEP 2: Extract Generated CNAMEs from sandbox === echo "⚙️ Generating sandbox ingress list..." cat < "$GENERATED_FILE" ingress: [] @@ -38,12 +40,11 @@ find "$SANDBOX_CLONE/manifests" -name cname.yaml | while read -r cname_file; do if [[ "$enabled" == "true" ]]; then hostname=$(yq '.hostname' "$cname_file") - port=$(yq '.port // 80' "$cname_file") # default to 80 if not set - service="http://${app_name}.sandbox.svc.cluster.local:$port" + service="http://${app_name}.sandbox.svc.cluster.local:80" echo "✅ Found $hostname → $service" - # Append to ingress list + # Append new entry to generated ingress list yq eval ".ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$GENERATED_FILE" fi done @@ -51,12 +52,15 @@ done echo "📄 Generated Ingress:" cat "$GENERATED_FILE" -# === STEP 3: Merge ingress only === -echo "🔁 Merging ingress into: $ORIGINAL_FILE" +# === STEP 3: Merge with existing cloudflared values === +echo "🔁 Merging new entries into: $ORIGINAL_FILE" TEMP_FILE=$(mktemp) + +# Extract original ingress yq eval '.cloudflare.ingress' "$ORIGINAL_FILE" > "$TEMP_FILE" +# Append new unique entries yq eval '.ingress' "$GENERATED_FILE" | yq eval -o=json '.' - | jq -c '.[]' | while read -r new_entry; do hostname=$(echo "$new_entry" | jq -r '.hostname') service=$(echo "$new_entry" | jq -r '.service') @@ -71,23 +75,26 @@ yq eval '.ingress' "$GENERATED_FILE" | yq eval -o=json '.' - | jq -c '.[]' | whi fi done -echo "📝 Writing merged file to $MERGED_FILE" +# === STEP 4: Load preserved values === +TUNNEL_NAME=$(yq e '.cloudflare.tunnelName' "$ORIGINAL_FILE") +ENABLE_WARP=$(yq e '.cloudflare.enableWarp' "$ORIGINAL_FILE") +SECRET_NAME=$(yq e '.cloudflare.secretName' "$ORIGINAL_FILE") +CLOUDFLARED_BLOCK=$(yq e '.cloudflared' "$ORIGINAL_FILE") -# Merge ingress back into full file, preserving all existing fields -yq eval-all ' - select(fileIndex == 0) as $orig | - select(fileIndex == 1) as $newIngress | - $orig * { - cloudflare: $orig.cloudflare * { - ingress: $newIngress - } - } -' "$ORIGINAL_FILE" "$TEMP_FILE" > "$MERGED_FILE" +# === STEP 5: Write final merged values === +echo "📝 Writing merged file to $MERGED_FILE" +yq eval " +.cloudflare.ingress = load(\"$TEMP_FILE\") | +.cloudflare.tunnelName = \"$TUNNEL_NAME\" | +.cloudflare.enableWarp = \"$ENABLE_WARP\" | +.cloudflare.secretName = \"$SECRET_NAME\" | +.cloudflared = $CLOUDFLARED_BLOCK +" "$ORIGINAL_FILE" > "$MERGED_FILE" echo "✅ Final merged values.yaml:" cat "$MERGED_FILE" -# === STEP 4: Git Push === +# === STEP 6: Optional Git push === echo "📤 Pushing updated values.yaml to infra repo..." cd "$INFRA_CLONE"