apps-gitops/automation/cloudflared-sync.sh
2025-06-19 22:26:06 +03:00

43 lines
1.4 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# CONFIG
ORIGINAL_FILE="../infra/cloudflared/values.yaml"
MERGED_FILE="../infra/cloudflared/values.yaml"
ls -l ../../
GENERATED_FILE="generated-values/cloudflared-values.yaml"
echo "🔁 Merging new entries into: $ORIGINAL_FILE"
# Extract original ingress list
ORIGINAL_INGRESS=$(yq eval '.cloudflare.ingress' "$ORIGINAL_FILE")
TEMP_FILE=$(mktemp)
# Start a fresh ingress list
echo "$ORIGINAL_INGRESS" | yq eval '.' - > "$TEMP_FILE"
# Loop over new 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')
# Check if hostname already exists
exists=$(yq e ".cloudflare.ingress[] | select(.hostname == \"$hostname\")" "$ORIGINAL_FILE")
if [ -z "$exists" ]; then
echo " Adding $hostname$service"
# Append new entry
yq eval ". += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$TEMP_FILE"
else
echo "⚠️ $hostname already exists, skipping"
fi
done
# Rebuild final values.yaml with updated ingress
yq eval '
.cloudflare.ingress = load("'"$TEMP_FILE"'") |
.cloudflare.tunnelName = strenv(TUNNEL_NAME) |
.cloudflare.enableWarp = false |
.cloudflare.secretName = "cloudflared-creds" |
.cloudflared = load("'"$ORIGINAL_FILE"'") | .cloudflared
' "$ORIGINAL_FILE" > "$MERGED_FILE"
echo "✅ Patched values.yaml saved to: $MERGED_FILE"