Skip to content

Agent Mesh

fix dns sycn script

Antonio Ojea0f2eab722a62
diff --git a/.github/k8s/dns-sync-cronjob-template.yaml b/.github/k8s/dns-sync-cronjob-template.yaml
index 1439c1ed..6c59a06e 100644
--- a/.github/k8s/dns-sync-cronjob-template.yaml
+++ b/.github/k8s/dns-sync-cronjob-template.yaml
@@ -45,7 +45,7 @@ spec:
serviceAccountName: dns-sync-sa
containers:
- name: sync-dns
- image: google/cloud-sdk:latest
+ image: google/cloud-sdk:stable
command:
- /bin/bash
- -c
@@ -55,48 +55,40 @@ spec:
echo "Finding sam-hub pods in ${NAMESPACE}..."
PODS=$(kubectl get pods -n ${NAMESPACE} -l app=sam-hub-${ENV_NAME} -o jsonpath='{range .items[?(@.status.phase=="Running")]}{.metadata.name}{" "}{.spec.nodeName}{"\n"}{end}')
- IPS=""
- TXT_RECORDS=""
+ declare -a IP_ARRAY=()
while read -r pod node; do
if [ -z "$pod" ]; then continue; fi
echo "Checking pod: $pod on node: $node"
- IP=$(kubectl get node $node -o jsonpath='{.status.addresses[?(@.type=="ExternalIP")].address}')
- PEER_ID=$(kubectl logs $pod -n ${NAMESPACE} | grep "PeerID:" | tail -n 1 | awk '{print $NF}')
+ IP_LIST=$(kubectl get node $node -o jsonpath='{.status.addresses[?(@.type=="ExternalIP")].address}')
+ POD_IP=$(kubectl get pod $pod -n ${NAMESPACE} -o jsonpath='{.status.podIP}')
- if [ -n "$IP" ] && [ -n "$PEER_ID" ]; then
- echo "Found IP $IP and PeerID $PEER_ID"
- IPS="$IPS $IP"
- TXT_RECORDS="$TXT_RECORDS \"dnsaddr=/dns4/bootstrap.${ENV_NAME}.sam-mesh.dev/tcp/4501/p2p/$PEER_ID\""
- TXT_RECORDS="$TXT_RECORDS \"dnsaddr=/dns4/bootstrap.${ENV_NAME}.sam-mesh.dev/udp/4501/quic-v1/p2p/$PEER_ID\""
+ if [ -n "$IP_LIST" ]; then
+ echo "Found IP $IP_LIST "
+ for ip in $IP_LIST; do
+ IP_ARRAY+=("$ip")
+ done
fi
done <<< "$PODS"
# Unique IPs
- UNIQUE_IPS=$(echo $IPS | tr ' ' '\n' | sort -u | tr '\n' ' ' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
+ UNIQUE_IPS=$(printf "%s\n" "${IP_ARRAY[@]}" | sort -u | tr '\n' ' ' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
if [ -z "$UNIQUE_IPS" ]; then
- echo "No external IPs or PeerIDs found for sam-hub nodes."
+ echo "No external IPs found for sam-hub nodes."
exit 0
fi
-
- # Clean TXT records for comparison
- TXT_RECORDS_CLEAN=$(echo "$TXT_RECORDS" | tr ' ' '\n' | sort -u | tr '\n' ' ' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
-
+
OLD_IPS=$(gcloud dns record-sets list --zone=${ENV_NAME} --project=${GCP_PROJECT_ID} --name="bootstrap.${ENV_NAME}.sam-mesh.dev." --type=A --format="value(rrdatas)" 2>/dev/null || true)
OLD_IPS_CLEAN=$(echo $OLD_IPS | tr ';' '\n' | sort -u | tr '\n' ' ' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
- OLD_TXT=$(gcloud dns record-sets list --zone=${ENV_NAME} --project=${GCP_PROJECT_ID} --name="_dnsaddr.bootstrap.${ENV_NAME}.sam-mesh.dev." --type=TXT --format="value(rrdatas)" 2>/dev/null || true)
- OLD_TXT_CLEAN=$(echo "$OLD_TXT" | tr ';' '\n' | sort -u | tr '\n' ' ' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
-
- if [ "$UNIQUE_IPS" == "$OLD_IPS_CLEAN" ] && [ "$TXT_RECORDS_CLEAN" == "$OLD_TXT_CLEAN" ]; then
+ if [ "$UNIQUE_IPS" == "$OLD_IPS_CLEAN" ] ; then
echo "DNS records are already up to date. No changes needed."
exit 0
fi
echo "DNS records changed! Updating..."
echo "Updating DNS record bootstrap.${ENV_NAME}.sam-mesh.dev. with IPs: $UNIQUE_IPS"
- echo "Updating TXT record _dnsaddr.bootstrap.${ENV_NAME}.sam-mesh.dev."
# Start transaction
gcloud dns record-sets transaction start --zone=${ENV_NAME} --project=${GCP_PROJECT_ID}
@@ -107,18 +99,10 @@ spec:
gcloud dns record-sets transaction remove --zone=${ENV_NAME} --project=${GCP_PROJECT_ID} --name="bootstrap.${ENV_NAME}.sam-mesh.dev." --type=A --ttl=300 $OLD_IPS_CLEAN_REMV || true
fi
- # Remove old TXT records if they exist
- if [ -n "$OLD_TXT" ]; then
- OLD_TXT_CLEAN_REMV=$(echo "$OLD_TXT" | tr ';' ' ')
- echo "$OLD_TXT_CLEAN_REMV" | xargs -r gcloud dns record-sets transaction remove --zone=${ENV_NAME} --project=${GCP_PROJECT_ID} --name="_dnsaddr.bootstrap.${ENV_NAME}.sam-mesh.dev." --type=TXT --ttl=300 || true
- fi
-
+
# Add new A records
gcloud dns record-sets transaction add --zone=${ENV_NAME} --project=${GCP_PROJECT_ID} --name="bootstrap.${ENV_NAME}.sam-mesh.dev." --type=A --ttl=300 $UNIQUE_IPS
-
- # Add new TXT records
- echo "$TXT_RECORDS" | xargs -r gcloud dns record-sets transaction add --zone=${ENV_NAME} --project=${GCP_PROJECT_ID} --name="_dnsaddr.bootstrap.${ENV_NAME}.sam-mesh.dev." --type=TXT --ttl=300
-
+
# Execute transaction
gcloud dns record-sets transaction execute --zone=${ENV_NAME} --project=${GCP_PROJECT_ID}
restartPolicy: OnFailure