Test order values by sections

This commit is contained in:
dvirlabs 2025-06-22 22:59:55 +03:00
parent 8ab1e56825
commit 6f3bd9a70c

View File

@ -13,16 +13,15 @@ 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="/woodpecker/src/git.dvirlabs.com/dvirlabs/apps-gitops/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"
echo "📦 Cloning sandbox-apps..." echo "📦 Cloning repos..."
git clone --depth=1 "$SANDBOX_REPO_URL" "$SANDBOX_CLONE" git clone --depth=1 "$SANDBOX_REPO_URL" "$SANDBOX_CLONE"
echo "📦 Cloning infra..."
git clone --depth=1 "$INFRA_REPO_URL" "$INFRA_CLONE" git clone --depth=1 "$INFRA_REPO_URL" "$INFRA_CLONE"
echo "⚙️ Generating sandbox ingress list..." echo "⚙️ Generating ingress entries..."
cat <<EOF > "$GENERATED_FILE" cat <<EOF > "$GENERATED_FILE"
ingress: [] ingress: []
EOF EOF
@ -31,26 +30,28 @@ find "$SANDBOX_CLONE/manifests" -name cname.yaml | while read -r cname_file; do
app_dir=$(dirname "$cname_file") app_dir=$(dirname "$cname_file")
app_name=$(basename "$app_dir") app_name=$(basename "$app_dir")
namespace=$(basename "$(dirname "$app_dir")") namespace=$(basename "$(dirname "$app_dir")")
enabled=$(yq '.enabled' "$cname_file") 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}.${namespace}.svc.cluster.local:80" service="http://${app_name}.${namespace}.svc.cluster.local:80"
echo "Found $hostname$service" echo "$hostname$service"
yq eval ".ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\", \"namespace\": \"$namespace\"}]" -i "$GENERATED_FILE" yq eval ".ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\", \"namespace\": \"$namespace\"}]" -i "$GENERATED_FILE"
fi fi
done done
echo "📄 Generated Ingress:" echo "📄 Ingress generated:"
cat "$GENERATED_FILE" cat "$GENERATED_FILE"
echo "🔁 Merging new entries into: $ORIGINAL_FILE" # === Merge new ingress into cloudflare.ingress ===
echo "🔁 Merging new entries..."
TEMP_FILE=$(mktemp) TEMP_FILE=$(mktemp)
cp "$ORIGINAL_FILE" "$TEMP_FILE" cp "$ORIGINAL_FILE" "$TEMP_FILE"
yq eval -o=json '.cloudflare.ingress' "$TEMP_FILE" > /tmp/existing.json existing_json=$(yq e -o=json '.cloudflare.ingress' "$TEMP_FILE" 2>/dev/null || echo "[]")
echo "$existing_json" > /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')
@ -60,37 +61,42 @@ yq eval '.ingress' "$GENERATED_FILE" | yq -o=json | jq -c '.[]' | while read -r
exists=$(jq --arg hostname "$hostname" '.[] | select(.hostname == $hostname)' /tmp/existing.json) 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"
yq eval ".cloudflare.ingress += [{\"hostname\": \"$hostname\", \"service\": \"$service\", \"namespace\": \"$namespace\"}]" -i "$TEMP_FILE" yq e ".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
echo "🧼 Grouping ingress entries by namespace..." # === Format ingress list ===
GROUPED=$(mktemp) echo "🧼 Regrouping by namespace..."
yq eval 'del(.cloudflare.ingress)' "$TEMP_FILE" > "$GROUPED"
echo " ingress:" >> "$GROUPED" FINAL_FILE=$(mktemp)
yq e 'del(.cloudflare.ingress)' "$TEMP_FILE" > "$FINAL_FILE"
echo " ingress:" >> "$FINAL_FILE"
yq eval '.cloudflare.ingress' "$TEMP_FILE" | yq -o=json | jq -s 'group_by(.namespace)[]' | while read -r group; do yq e '.cloudflare.ingress' "$TEMP_FILE" | yq -o=json | jq -s 'group_by(.namespace)[]' | while read -r group; do
namespace=$(echo "$group" | jq -r '.[0].namespace') namespace=$(echo "$group" | jq -r '.[0].namespace')
echo " # ############ $namespace ############" >> "$GROUPED" echo " # ############ $namespace ############" >> "$FINAL_FILE"
echo "$group" | jq -c '.[]' | while read -r item; do echo "$group" | jq -c '.[]' | while read -r item; do
hostname=$(echo "$item" | jq -r '.hostname') hostname=$(echo "$item" | jq -r '.hostname')
service=$(echo "$item" | jq -r '.service') service=$(echo "$item" | jq -r '.service')
echo " - hostname: $hostname" >> "$GROUPED" echo " - hostname: $hostname" >> "$FINAL_FILE"
echo " service: $service" >> "$GROUPED" echo " service: $service" >> "$FINAL_FILE"
done done
done >> "$GROUPED" done
sed -i '/^cloudflare:/r /dev/stdin' "$GROUPED" <<< "$(tail -n +2 "$GROUPED")" # Merge back under cloudflare.ingress
FINAL_MERGED=$(mktemp)
yq e 'del(.cloudflare.ingress)' "$TEMP_FILE" > "$FINAL_MERGED"
cat "$FINAL_FILE" >> "$FINAL_MERGED"
cp "$GROUPED" "$MERGED_FILE" cp "$FINAL_MERGED" "$MERGED_FILE"
echo "✅ Final merged values.yaml:" echo "✅ Final values.yaml:"
cat "$MERGED_FILE" cat "$MERGED_FILE"
# === Git commit/push ===
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"
@ -98,14 +104,14 @@ 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): grouped ingress by namespace" git commit -m "chore(cloudflared): auto-merge & group ingress"
git push origin HEAD git push origin HEAD
echo "✅ Changes pushed successfully."
else else
echo " No changes to commit." echo " No changes to commit."
fi fi
echo "🌐 Creating CNAME records in Cloudflare..." # === Cloudflare CNAME ===
echo "🌐 Creating CNAME records..."
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"
@ -114,12 +120,11 @@ TARGET="b50bbf48-0a2f-47ce-b73e-336b6718318b.cfargotunnel.com"
yq eval '.ingress' "$GENERATED_FILE" | yq -o=json | jq -c '.[]' | while read -r record; do yq eval '.ingress' "$GENERATED_FILE" | yq -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//')
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')
if [ "$exists" -eq 0 ]; then if [ "$exists" -eq 0 ]; then
echo " Creating CNAME: $name.dvirlabs.com → $TARGET" echo " Creating $name.dvirlabs.com → $TARGET"
curl -s -X POST "$CLOUDFLARE_API/zones/$CLOUDFLARE_ZONE_ID/dns_records" \ curl -s -X POST "$CLOUDFLARE_API/zones/$CLOUDFLARE_ZONE_ID/dns_records" \
-H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \ -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
-H "Content-Type: application/json" \ -H "Content-Type: application/json" \
@ -131,7 +136,6 @@ yq eval '.ingress' "$GENERATED_FILE" | yq -o=json | jq -c '.[]' | while read -r
\"proxied\": true \"proxied\": true
}" > /dev/null }" > /dev/null
else else
echo "⚠️ CNAME for $name.dvirlabs.com already exists, skipping" echo "⚠️ $name.dvirlabs.com already exists"
fi fi
done done