Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 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,8 @@ install_prerequisites() {
if [ "$DEVICE_TYPE" = "k3s" ]; then
setup_k3s
configure_harbor_trust_for_k3s
sleep 5
configure_coredns_hosts
fi
Comment thread
vireshnavalli marked this conversation as resolved.

echo 'prerequisites installation completed.'
Expand Down
154 changes: 154 additions & 0 deletions scripts/modules/dns-host-config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
#!/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.
#------------------------------------------------------------------------------
configure_coredns_hosts() {
set -euo pipefail

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

local harbor_host="harbor.machine"
local symphony_host="symphony.machine"
Comment thread
spulkit138 marked this conversation as resolved.
Outdated

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() {
Comment thread
spulkit138 marked this conversation as resolved.
Outdated
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
}

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