Test order values by sections

This commit is contained in:
dvirlabs 2025-06-22 22:27:41 +03:00
parent d72e17978d
commit eafef6d665
2 changed files with 41 additions and 32 deletions

View File

@ -1,5 +1,7 @@
steps: steps:
sync-cloudflare: sync-cloudflare:
when:
branch: [master]
name: Scan Apps and Update Cloudflared name: Scan Apps and Update Cloudflared
image: alpine image: alpine
commands: commands:

View File

@ -1,9 +1,6 @@
#!/bin/bash #!/bin/bash
set -e set -e
# 📦 Ensure apk installs: yq, jq, git, bash, curl
# Required for pipeline: alpine image + apk add --no-cache git bash curl yq jq
apk add --no-cache git bash curl yq jq apk add --no-cache git bash curl yq jq
echo "🔍 Scanning for apps with cname.yaml..." echo "🔍 Scanning for apps with cname.yaml..."
@ -12,76 +9,92 @@ mkdir -p generated-values
rm -rf .tmp-repos rm -rf .tmp-repos
mkdir -p .tmp-repos mkdir -p .tmp-repos
# === REPO CONFIG ===
SANDBOX_REPO_URL="https://git.dvirlabs.com/dvirlabs/sandbox.git" SANDBOX_REPO_URL="https://git.dvirlabs.com/dvirlabs/sandbox.git"
INFRA_REPO_URL="https://${GIT_TOKEN}@git.dvirlabs.com/dvirlabs/infra.git" INFRA_REPO_URL="https://${GIT_TOKEN}@git.dvirlabs.com/dvirlabs/infra.git"
SANDBOX_CLONE=".tmp-repos/sandbox" SANDBOX_CLONE=".tmp-repos/sandbox"
INFRA_CLONE=".tmp-repos/infra" INFRA_CLONE=".tmp-repos/infra"
GENERATED_FILE="$(pwd)/generated-values/cloudflared-values.yaml" GENERATED_FILE="generated-values/cloudflared-values.yaml"
ORIGINAL_FILE="$INFRA_CLONE/manifests/cloudflared/values.yaml" ORIGINAL_FILE="$INFRA_CLONE/manifests/cloudflared/values.yaml"
MERGED_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-apps..."
git clone --depth=1 "$SANDBOX_REPO_URL" "$SANDBOX_CLONE" git clone --depth=1 "$SANDBOX_REPO_URL" "$SANDBOX_CLONE"
echo "📦 Cloning infra..." echo "📦 Cloning infra..."
git clone --depth=1 "$INFRA_REPO_URL" "$INFRA_CLONE" git clone --depth=1 "$INFRA_REPO_URL" "$INFRA_CLONE"
ls -l .tmp-repos/
# === STEP 2: Extract Generated CNAMEs from sandbox ===
echo "⚙️ Generating sandbox ingress list..." echo "⚙️ Generating sandbox ingress list..."
cat <<EOF > "$GENERATED_FILE" cat <<EOF > "$GENERATED_FILE"
ingress: [] ingress: []
EOF EOF
find "$SANDBOX_CLONE/manifests" -name cname.yaml | while read -r cname_file; do find "$SANDBOX_CLONE/manifests" -name cname.yaml | while read -r cname_file; do
app_name=$(basename "$(dirname "$cname_file")") app_dir=$(dirname "$cname_file")
enabled=$(yq '.enabled' "$cname_file") app_name=$(basename "$app_dir")
namespace=$(basename "$(dirname "$app_dir")")
enabled=$(yq '.enabled' "$cname_file")
if [[ "$enabled" == "true" ]]; then if [[ "$enabled" == "true" ]]; then
hostname=$(yq '.hostname' "$cname_file") hostname=$(yq '.hostname' "$cname_file")
service="http://${app_name}.sandbox.svc.cluster.local:80" service="http://${app_name}.${namespace}.svc.cluster.local:80"
echo "✅ Found $hostname$service" echo "✅ Found $hostname$service"
yq eval ".ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\", \"namespace\": \"$namespace\"}]" -i "$GENERATED_FILE"
yq eval ".ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$GENERATED_FILE"
fi fi
done done
echo "📄 Generated Ingress:" echo "📄 Generated Ingress:"
cat "$GENERATED_FILE" cat "$GENERATED_FILE"
# === STEP 3: Merge with existing cloudflared values ===
echo "🔁 Merging new entries into: $ORIGINAL_FILE" echo "🔁 Merging new entries into: $ORIGINAL_FILE"
TEMP_FILE=$(mktemp) TEMP_FILE=$(mktemp)
cp "$ORIGINAL_FILE" "$TEMP_FILE" cp "$ORIGINAL_FILE" "$TEMP_FILE"
# Loop through new entries and append only if hostname not exists # Strip namespace key from existing values if present
yq eval '.cloudflare.ingress' "$TEMP_FILE" | yq -o=json | jq -c '.[]' > /tmp/existing.json
yq eval '.ingress' "$GENERATED_FILE" | yq -o=json | jq -c '.[]' | while read -r new_entry; do yq eval '.ingress' "$GENERATED_FILE" | yq -o=json | jq -c '.[]' | while read -r new_entry; do
hostname=$(echo "$new_entry" | jq -r '.hostname') hostname=$(echo "$new_entry" | jq -r '.hostname')
service=$(echo "$new_entry" | jq -r '.service') service=$(echo "$new_entry" | jq -r '.service')
namespace=$(echo "$new_entry" | jq -r '.namespace')
exists=$(yq e ".cloudflare.ingress[] | select(.hostname == \"$hostname\")" "$TEMP_FILE") exists=$(jq --arg hostname "$hostname" '.[] | select(.hostname == $hostname)' /tmp/existing.json)
if [ -z "$exists" ]; then if [ -z "$exists" ]; then
echo " Adding $hostname$service" echo " Adding $hostname$service"
yq eval ".cloudflare.ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\"}]" -i "$TEMP_FILE" yq eval ".cloudflare.ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\", \"namespace\": \"$namespace\"}]" -i "$TEMP_FILE"
else else
echo "⚠️ $hostname already exists, skipping" echo "⚠️ $hostname already exists, skipping"
fi fi
done done
# === STEP 4: Overwrite only ingress list and preserve all other fields === # === 🧹 Group and sort with comments ===
echo "📝 Writing final merged values.yaml" echo "🧼 Grouping ingress entries by namespace..."
cp "$TEMP_FILE" "$MERGED_FILE" GROUPED=$(mktemp)
echo "cloudflare:" > "$GROUPED"
yq eval '.cloudflare | del(.ingress)' "$TEMP_FILE" | tail -n +2 >> "$GROUPED"
echo " ingress:" >> "$GROUPED"
# Extract ingress entries with namespace
yq eval '.cloudflare.ingress' "$TEMP_FILE" | yq -o=json | jq -s 'group_by(.namespace)[]' | while read -r group; do
namespace=$(echo "$group" | jq -r '.[0].namespace')
echo " # ############ $namespace ############" >> "$GROUPED"
echo "$group" | jq -c '.[]' | while read -r item; do
hostname=$(echo "$item" | jq -r '.hostname')
service=$(echo "$item" | jq -r '.service')
echo " - hostname: $hostname" >> "$GROUPED"
echo " service: $service" >> "$GROUPED"
done
done
# Copy to merged location
cp "$GROUPED" "$MERGED_FILE"
echo "✅ Final merged values.yaml:" echo "✅ Final merged values.yaml:"
cat "$MERGED_FILE" cat "$MERGED_FILE"
# === STEP 5: Optional push to Git === # === Commit if changed ===
cd "$INFRA_CLONE" cd "$INFRA_CLONE"
git config user.name "woodpecker-bot" git config user.name "woodpecker-bot"
git config user.email "ci@dvirlabs.com" git config user.email "ci@dvirlabs.com"
@ -89,30 +102,24 @@ git remote set-url origin "https://${GIT_TOKEN}@git.dvirlabs.com/dvirlabs/infra.
if ! git diff --quiet manifests/cloudflared/values.yaml; then if ! git diff --quiet manifests/cloudflared/values.yaml; then
git add manifests/cloudflared/values.yaml git add manifests/cloudflared/values.yaml
git commit -m "chore(cloudflared): auto-merge CNAME entries from sandbox" git commit -m "chore(cloudflared): grouped ingress by namespace"
git push origin HEAD git push origin HEAD
echo "✅ Changes pushed successfully." echo "✅ Changes pushed successfully."
else else
echo " No changes to commit." echo " No changes to commit."
fi fi
# === STEP 5: Create CNAME records in Cloudflare === # === Create Cloudflare CNAMEs ===
ls -l
pwd
ls -l "$GENERATED_FILE"
echo "🌐 Creating CNAME records in Cloudflare..." echo "🌐 Creating CNAME records in Cloudflare..."
CLOUDFLARE_API="https://api.cloudflare.com/client/v4" CLOUDFLARE_API="https://api.cloudflare.com/client/v4"
TARGET="b50bbf48-0a2f-47ce-b73e-336b6718318b.cfargotunnel.com" TARGET="b50bbf48-0a2f-47ce-b73e-336b6718318b.cfargotunnel.com"
# Load required secrets
: "${CLOUDFLARE_API_TOKEN:?CLOUDFLARE_API_TOKEN not set}" : "${CLOUDFLARE_API_TOKEN:?CLOUDFLARE_API_TOKEN not set}"
: "${CLOUDFLARE_ZONE_ID:?CLOUDFLARE_ZONE_ID not set}" : "${CLOUDFLARE_ZONE_ID:?CLOUDFLARE_ZONE_ID not set}"
# Check and create each CNAME yq eval '.ingress' "$GENERATED_FILE" | yq eval -o=json '.' | jq -c '.[]' | while read -r record; do
yq eval '.ingress' "$GENERATED_FILE" | yq eval -o=json '.' - | jq -c '.[]' | while read -r record; do
name=$(echo "$record" | jq -r '.hostname' | sed 's/\.dvirlabs\.com//') name=$(echo "$record" | jq -r '.hostname' | sed 's/\.dvirlabs\.com//')
# Check if already exists
exists=$(curl -s -X GET "$CLOUDFLARE_API/zones/$CLOUDFLARE_ZONE_ID/dns_records?type=CNAME&name=$name.dvirlabs.com" \ exists=$(curl -s -X GET "$CLOUDFLARE_API/zones/$CLOUDFLARE_ZONE_ID/dns_records?type=CNAME&name=$name.dvirlabs.com" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type: application/json" | jq '.result | length') -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" -H "Content-Type: application/json" | jq '.result | length')