Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 0 additions & 57 deletions .github/workflows/sandbox-sanity-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -296,63 +296,6 @@ jobs:
sudo -E bash ./device-agent.sh k3s create-rsa-certs
sudo -E bash ./device-agent.sh k3s create-ecdsa-certs

- name: Configure CoreDNS for k3s
Comment thread
vireshnavalli marked this conversation as resolved.
run: |
RUNNER_IP=${RUNNER_IP}
echo "📡 Configuring CoreDNS with custom DNS entries..."

# Retry logic for ConfigMap update
for RETRY in {1..3}; do
echo "Attempt $RETRY to configure CoreDNS..."

# Get the full ConfigMap YAML
sudo KUBECONFIG=/etc/rancher/k3s/k3s.yaml \
kubectl -n kube-system get configmap coredns -o yaml > config.yaml

# Update the Corefile section
awk -v ip="$RUNNER_IP" '
/NodeHosts: \|/ {
print
getline
print " " ip " harbor.machine"
print " " ip " symphony.machine"
print
next
}
{ print }
' config.yaml > config-new.yaml

# Verify the change was made
if grep -q "harbor.machine" config-new.yaml && grep -q "symphony.machine" config-new.yaml; then
echo "✅ DNS entries added to ConfigMap"

# Apply the updated ConfigMap
sudo KUBECONFIG=/etc/rancher/k3s/k3s.yaml kubectl apply -f config-new.yaml

# Restart CoreDNS
sudo KUBECONFIG=/etc/rancher/k3s/k3s.yaml kubectl rollout restart deployment coredns -n kube-system
sudo KUBECONFIG=/etc/rancher/k3s/k3s.yaml kubectl rollout status deployment/coredns -n kube-system --timeout=60s

# Wait longer for DNS propagation
echo "⏳ Waiting for DNS propagation..."
sleep 15

echo "✅ CoreDNS configured successfully"
break
else
echo "⚠️ DNS entries not found in updated ConfigMap, retrying..."
sleep 5
fi

if [ $RETRY -eq 3 ]; then
echo "❌ Failed to configure CoreDNS after 3 attempts"
echo "ConfigMap contents:"
cat config-new.yaml
exit 1
fi
done


- name: WFM-Client (Standalone Cluster) – Start
working-directory: scripts
run: |
Expand Down
5 changes: 5 additions & 0 deletions scripts/device-agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ source "${SCRIPT_DIR}/modules/harbor.sh"
source "${SCRIPT_DIR}/modules/certificates.sh"
source "${SCRIPT_DIR}/modules/agent.sh"
source "${SCRIPT_DIR}/modules/observability.sh"
source "${SCRIPT_DIR}/modules/dns-host-config.sh"



export GOINSECURE='github.com/margo/*'
Expand Down Expand Up @@ -178,6 +180,9 @@ install_prerequisites() {
if [ "$DEVICE_TYPE" = "k3s" ]; then
setup_k3s
configure_harbor_trust_for_k3s
echo "Waiting for k3s to stabilize..."
sleep 5
configure_coredns_hosts
fi
Comment thread
vireshnavalli marked this conversation as resolved.

echo 'prerequisites installation completed.'
Expand Down
155 changes: 155 additions & 0 deletions scripts/modules/dns-host-config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
#!/bin/bash

#------------------------------------------------------------------------------
# Returns the IPv4 address of the specified hostname from /etc/hosts.
#------------------------------------------------------------------------------
get_ip_from_hosts() {
local hostname="$1"
local hosts_file="/etc/hosts"
local ip

if [[ ! -r "$hosts_file" ]]; then
echo "[ERROR] Unable to read ${hosts_file}." >&2
return 1
fi

ip=$(
awk -v host="$hostname" '
/^[[:space:]]*#/ { next }
NF >= 2 && $2 == host {
print $1
exit
}
' "$hosts_file"
)

if [[ -z "$ip" ]]; then
echo "[ERROR] Host '${hostname}' not found in ${hosts_file}." >&2
return 1
fi

if [[ ! "$ip" =~ ^([0-9]{1,3}\.){3}[0-9]{1,3}$ ]]; then
echo "[ERROR] Invalid IPv4 address '${ip}' for '${hostname}'." >&2
return 1
fi

printf '%s\n' "$ip"
}

#------------------------------------------------------------------------------
# Adds or updates NodeHosts entries in the CoreDNS ConfigMap.
#------------------------------------------------------------------------------

add_or_update_host() {
local ip="$1"
local host="$2"

if awk -v host="$host" '
/^[[:space:]]*#/ { next }
$2 == host { found=1 }
END { exit !found }
' <<<"$updated"; then

updated=$(
awk -v ip="$ip" -v host="$host" '
/^[[:space:]]*#/ {
print
next
}

$2 == host {
print ip " " host
next
}

{
print
}
' <<<"$updated"
)

else
if [[ -n "$updated" ]]; then
updated+=$'\n'
fi
updated+="${ip} ${host}"
fi
}

configure_coredns_hosts() {
set -euo pipefail

local namespace="kube-system"
local configmap="coredns"

local harbor_host="${EXPOSED_HARBOR_HOST}"
local symphony_host="${WFM_HOST}"

local harbor_ip
local symphony_ip

local nodehosts
local updated

echo "[INFO] Validating dependencies..."

for cmd in kubectl jq awk sed; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "[ERROR] Required command '$cmd' is not installed." >&2
return 1
fi
done

echo "[INFO] Reading IP addresses from /etc/hosts..."

harbor_ip=$(get_ip_from_hosts "$harbor_host")
symphony_ip=$(get_ip_from_hosts "$symphony_host")

echo "[INFO] Verifying CoreDNS ConfigMap..."

if ! kubectl -n "$namespace" get configmap "$configmap" >/dev/null 2>&1; then
echo "[ERROR] ConfigMap '${configmap}' not found in namespace '${namespace}'." >&2
return 1
fi

nodehosts=$(
kubectl -n "$namespace" get configmap "$configmap" \
-o jsonpath='{.data.NodeHosts}' 2>/dev/null || true
)

updated="$nodehosts"

add_or_update_host "$harbor_ip" "$harbor_host"
add_or_update_host "$symphony_ip" "$symphony_host"

updated=$(sed '/^[[:space:]]*$/d' <<<"$updated")

if [[ "$updated" == "$nodehosts" ]]; then
echo "[INFO] CoreDNS NodeHosts already up to date."
return 0
fi

echo "[INFO] Updating CoreDNS ConfigMap..."

kubectl -n "$namespace" patch configmap "$configmap" \
--type merge \
--patch "$(cat <<EOF
{
"data": {
"NodeHosts": $(printf '%s' "$updated" | jq -Rs .)
}
}
EOF
)"

echo "[INFO] Restarting CoreDNS..."

kubectl -n "$namespace" rollout restart deployment/coredns

echo "[INFO] Waiting for CoreDNS rollout to complete..."

kubectl -n "$namespace" rollout status deployment/coredns --timeout=180s

echo "[INFO] CoreDNS NodeHosts updated successfully."
}

Loading