From 433fff52bd371b7edbd90fc6040ee198ff8f8adb Mon Sep 17 00:00:00 2001 From: Pulkit Sharma Date: Wed, 5 Aug 2026 10:32:50 +0000 Subject: [PATCH] chore(scripts): autoupdating coredns host config while installing pre-requisites Signed-off-by: Pulkit Sharma --- .github/workflows/sandbox-sanity-test.yml | 58 +------- docs/setup-guide.md | 53 +------- scripts/device-agent.sh | 5 +- scripts/modules/dns-host-config.sh | 154 ++++++++++++++++++++++ 4 files changed, 162 insertions(+), 108 deletions(-) create mode 100644 scripts/modules/dns-host-config.sh diff --git a/.github/workflows/sandbox-sanity-test.yml b/.github/workflows/sandbox-sanity-test.yml index 1c5d5332..4f74f905 100644 --- a/.github/workflows/sandbox-sanity-test.yml +++ b/.github/workflows/sandbox-sanity-test.yml @@ -289,69 +289,13 @@ jobs: echo "=========================================" docker logs symphony-api-container --tail=100 2>&1 || echo "Could not retrieve Symphony logs" - - name: WFM-Client (K3s) – Setup + - name: WFM-Client (K3s) - Setup working-directory: scripts run: | sudo -E bash ./device-agent.sh k3s install 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 - 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 (Helm-capable device) - Start working-directory: scripts diff --git a/docs/setup-guide.md b/docs/setup-guide.md index ad495b8d..92e71029 100644 --- a/docs/setup-guide.md +++ b/docs/setup-guide.md @@ -261,68 +261,21 @@ On each VM, you need to configure environment variables (settings that tell the cd $HOME/workspace/sandbox/scripts ``` -2. **Configure the domain/host(name) resolution in coredns** - - The k3s based setups don't pick the changes from `/etc/hosts` system file, hence it is better to add hostname details in the the coredns config itself. - - 2.1. Fetch the coredns config first: - ```bash - sudo kubectl -n kube-system edit configmap coredns - ``` - - 2.2. Then add only the highlighted lines in the `NodeHosts` section with your IP addresses in them and save: - ```yaml - apiVersion: v1 - data: - Corefile: | - .:53 { - errors - health - ready - kubernetes cluster.local in-addr.arpa ip6.arpa { - pods insecure - fallthrough in-addr.arpa ip6.arpa - } - hosts /etc/coredns/NodeHosts { - ttl 60 - reload 15s - fallthrough - } - prometheus :9153 - forward . /etc/resolv.conf - cache 30 - loop - reload - loadbalance - import /etc/coredns/custom/*.override - } - import /etc/coredns/custom/*.server - NodeHosts: | - 20.11.000.000 harbor.machine # Newly added line to resolve harbor.machine - 20.11.000.000 symphony.machine # Newly added line to resolve symphony.machine - 10.0.0.11 margo-wfm-v10 - ``` - - 2.3. Then apply the changes: - ```bash - sudo kubectl -n kube-system rollout restart deployment coredns - ``` - -3. **Start the device's Workload Fleet Management Client** +2. **Start the device's Workload Fleet Management Client** ```bash sudo -E bash device-agent.sh k3s ``` - Type `5` and press Enter - Choose: `Option 5: Device-agent-Start(k3s-device)` -4. **Check device status** +3. **Check device status** ```bash sudo -E bash device-agent.sh k3s ``` - Type `7` and press Enter - Choose: `Option 7: Device-agent-Status` -5. **View device logs** +4. **View device logs** ```bash # View the logs (replace with actual pod name from above using #7) sudo kubectl logs -f -n default diff --git a/scripts/device-agent.sh b/scripts/device-agent.sh index 31f4621e..b516a18a 100644 --- a/scripts/device-agent.sh +++ b/scripts/device-agent.sh @@ -115,7 +115,7 @@ 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/*' export GONOPROXY='github.com/margo/*' @@ -178,6 +178,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 echo 'prerequisites installation completed.' diff --git a/scripts/modules/dns-host-config.sh b/scripts/modules/dns-host-config.sh new file mode 100644 index 00000000..33d2b3dd --- /dev/null +++ b/scripts/modules/dns-host-config.sh @@ -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. +#------------------------------------------------------------------------------ + +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 <