diff --git a/automation/cloudflared-sync.sh b/automation/cloudflared-sync.sh index c25aab2..b833c14 100644 --- a/automation/cloudflared-sync.sh +++ b/automation/cloudflared-sync.sh @@ -20,15 +20,13 @@ ORIGINAL_FILE="$INFRA_CLONE/manifests/cloudflared/values.yaml" MERGED_FILE="$INFRA_CLONE/manifests/cloudflared/values.yaml" # === STEP 1: Clone Repos === -echo "📦 Cloning sandbox-apps..." +echo "📦 Cloning sandbox..." git clone --depth=1 "$SANDBOX_REPO_URL" "$SANDBOX_CLONE" echo "📦 Cloning infra..." git clone --depth=1 "$INFRA_REPO_URL" "$INFRA_CLONE" -ls -l .tmp-repos/ - -# === STEP 2: Extract Generated CNAMEs from sandbox === +# === STEP 2: Generate new ingress entries === echo "⚙️ Generating sandbox ingress list..." cat < "$GENERATED_FILE" ingress: [] @@ -40,10 +38,12 @@ find "$SANDBOX_CLONE/manifests" -name cname.yaml | while read -r cname_file; do if [[ "$enabled" == "true" ]]; then hostname=$(yq '.hostname' "$cname_file") - service="http://${app_name}.sandbox.svc.cluster.local:80" + port=$(yq '.port // 80' "$cname_file") # default to 80 if not set + service="http://${app_name}.sandbox.svc.cluster.local:$port" echo "✅ Found $hostname → $service" + # Append to ingress list yq eval ".ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$GENERATED_FILE" fi done @@ -51,8 +51,8 @@ done echo "📄 Generated Ingress:" cat "$GENERATED_FILE" -# === STEP 3: Merge into original ingress === -echo "🔁 Merging new entries into: $ORIGINAL_FILE" +# === STEP 3: Merge ingress only === +echo "🔁 Merging ingress into: $ORIGINAL_FILE" TEMP_FILE=$(mktemp) yq eval '.cloudflare.ingress' "$ORIGINAL_FILE" > "$TEMP_FILE" @@ -62,6 +62,7 @@ yq eval '.ingress' "$GENERATED_FILE" | yq eval -o=json '.' - | jq -c '.[]' | whi service=$(echo "$new_entry" | jq -r '.service') exists=$(yq e ".cloudflare.ingress[] | select(.hostname == \"$hostname\")" "$ORIGINAL_FILE") + if [ -z "$exists" ]; then echo "➕ Adding $hostname → $service" yq eval ". += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$TEMP_FILE" @@ -70,19 +71,23 @@ yq eval '.ingress' "$GENERATED_FILE" | yq eval -o=json '.' - | jq -c '.[]' | whi fi done -# === STEP 4: Write final merged values file (safe merge) === echo "📝 Writing merged file to $MERGED_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: { + cloudflare: $orig.cloudflare * { ingress: $newIngress } } ' "$ORIGINAL_FILE" "$TEMP_FILE" > "$MERGED_FILE" -# === STEP 5: Optional push to Git === +echo "✅ Final merged values.yaml:" +cat "$MERGED_FILE" + +# === STEP 4: Git Push === echo "📤 Pushing updated values.yaml to infra repo..." cd "$INFRA_CLONE"