@@ -728,81 +728,85 @@ EOF
728728
729729 log_success " dnsmasq configured. All *.${local_domain} resolves to ${node_ip} ."
730730
731- # Configure CoreDNS to forward local domain queries to dnsmasq.
732- # Pods use CoreDNS (kube-system/rke2-coredns-rke2-coredns) for DNS, which
733- # doesn't know about our local domains. We add a custom server block that
734- # forwards *.openg2p.test to dnsmasq on the node IP.
735- # This runs after dnsmasq is up but before K8s is necessarily ready,
736- # so we save the config and apply it after RKE2 starts (in run_phase1).
737- log_info " Preparing CoreDNS custom config for ${local_domain} -> dnsmasq..."
731+ # Save local domain and node IP for the CoreDNS patching step (step 7b).
732+ # CoreDNS patching requires kubectl, which is available after RKE2 starts.
738733 mkdir -p /var/lib/openg2p/deploy-state
739- cat > /var/lib/openg2p/deploy-state/coredns-custom.yaml << DNSEOF
740- apiVersion: v1
741- kind: ConfigMap
742- metadata:
743- name: rke2-coredns-rke2-coredns-custom
744- namespace: kube-system
745- data:
746- openg2p.server: |
747- ${local_domain} :53 {
748- errors
749- cache 30
750- forward . ${node_ip}
751- }
752- DNSEOF
753- log_info " CoreDNS custom config saved. Will be applied after RKE2 starts."
734+ echo " ${local_domain} " > /var/lib/openg2p/deploy-state/coredns-local-domain
735+ echo " ${node_ip} " > /var/lib/openg2p/deploy-state/coredns-node-ip
754736
755737 mark_step_done " $step_id "
756738}
757739
758740# ─────────────────────────────────────────────────────────────────────────────
759- # Step 7b: Apply CoreDNS custom config ( local mode only)
741+ # Step 7b: Patch CoreDNS Corefile for local domain forwarding
760742# ─────────────────────────────────────────────────────────────────────────────
761- # In local mode, pods inside the cluster need to resolve *.openg2p.test.
762- # CoreDNS only knows about cluster.local — we add a custom server block
763- # that forwards local domain queries to dnsmasq on the node.
764- # RKE2's CoreDNS watches the ConfigMap rke2-coredns-rke2-coredns-custom
765- # and auto-reloads when it changes.
743+ # In local mode, pods use CoreDNS which only knows about cluster.local.
744+ # We patch the main CoreDNS Corefile to add a server block that forwards
745+ # *.openg2p.test queries to dnsmasq on the node IP.
746+ #
747+ # Note: RKE2's CoreDNS does NOT mount a custom ConfigMap volume by default,
748+ # so the "import custom/*.server" approach doesn't work. We inject the
749+ # server block directly into the main rke2-coredns-rke2-coredns ConfigMap.
766750phase1_step7b_coredns_custom () {
767751 local domain_mode=$( cfg " domain_mode" " custom" )
768752 [[ " $domain_mode " == " local" ]] || return 0
769753
770754 local step_id=" phase1.coredns_custom"
771- skip_if_done " $step_id " " CoreDNS custom config " && return 0
755+ skip_if_done " $step_id " " CoreDNS local domain forwarding " && return 0
772756
773- local coredns_file=" /var/lib/openg2p/deploy-state/coredns-custom.yaml"
774- if [[ ! -f " $coredns_file " ]]; then
775- log_info " No CoreDNS custom config found — skipping."
776- return 0
777- fi
757+ local local_domain=$( cfg " local_domain" " openg2p.test" )
758+ local node_ip=$( cfg " node_ip" )
778759
779- log_info " Applying CoreDNS custom config for local domain forwarding ..."
760+ log_info " Patching CoreDNS Corefile to forward ${local_domain} -> dnsmasq ( ${node_ip} ) ..."
780761 ensure_kubeconfig || return 1
781762
782- kubectl apply -f " $coredns_file " || {
783- log_error " Failed to apply CoreDNS custom config" \
784- " kubectl apply failed" \
785- " Check CoreDNS ConfigMap" \
786- " kubectl -n kube-system get configmap rke2-coredns-rke2-coredns-custom -o yaml"
763+ # Check if the Corefile already has the local domain block
764+ local current_corefile
765+ current_corefile=$( kubectl -n kube-system get configmap rke2-coredns-rke2-coredns \
766+ -o jsonpath=' {.data.Corefile}' 2> /dev/null || true)
767+
768+ if [[ -z " $current_corefile " ]]; then
769+ log_error " CoreDNS ConfigMap not found" \
770+ " rke2-coredns-rke2-coredns ConfigMap missing in kube-system" \
771+ " Check RKE2 CoreDNS deployment" \
772+ " kubectl -n kube-system get configmap"
787773 return 1
788- }
774+ fi
789775
790- # Restart CoreDNS to pick up the change immediately
791- kubectl -n kube-system rollout restart deployment rke2-coredns-rke2-coredns > /dev/null 2>&1 || true
792- sleep 5
776+ if echo " $current_corefile " | grep -q " ${local_domain} :53" ; then
777+ log_info " CoreDNS Corefile already contains ${local_domain} server block — skipping."
778+ else
779+ log_info " Injecting ${local_domain} server block into CoreDNS Corefile..."
780+ kubectl -n kube-system get configmap rke2-coredns-rke2-coredns -o json | \
781+ jq --arg domain " $local_domain " --arg ip " $node_ip " '
782+ .data.Corefile = $domain + ":53 {\n errors\n cache 30\n forward . " + $ip + "\n}\n" + .data.Corefile
783+ ' | kubectl apply -f - || {
784+ log_error " Failed to patch CoreDNS Corefile" \
785+ " jq/kubectl pipeline failed" \
786+ " Check CoreDNS ConfigMap" \
787+ " kubectl -n kube-system get configmap rke2-coredns-rke2-coredns -o yaml"
788+ return 1
789+ }
793790
794- # Verify: resolve the local domain from inside CoreDNS
795- local local_domain=$( cfg " local_domain" " openg2p.test" )
796- local node_ip=$( cfg " node_ip" )
791+ # Restart CoreDNS to pick up the change
792+ kubectl -n kube-system rollout restart deployment rke2-coredns-rke2-coredns > /dev/null 2>&1 || true
793+ log_info " CoreDNS restarting..."
794+ sleep 10
795+ fi
796+
797+ # Verify: resolve the local domain from inside a pod
798+ log_info " Verifying DNS resolution from inside a pod..."
797799 local test_ip
798800 test_ip=$( kubectl run dns-test --rm -i --restart=Never --image=busybox:1.36 \
799- -- nslookup " keycloak.${local_domain} " 2> /dev/null | grep -A1 " Name:" | tail -1 | awk ' {print $2}' || true)
801+ -- nslookup " keycloak.${local_domain} " 2> /dev/null | \
802+ grep -A1 " Name:" | tail -1 | awk ' {print $2}' || true)
800803
801804 if [[ " $test_ip " == " $node_ip " ]]; then
802805 log_success " CoreDNS resolves keycloak.${local_domain} -> ${node_ip} from inside pods."
803806 else
804807 log_warn " CoreDNS verification returned '${test_ip} ' (expected ${node_ip} )."
805- log_warn " CoreDNS may still be reloading. Pods should resolve after a few seconds."
808+ log_warn " Pods may need a few more seconds. Check manually:"
809+ log_warn " kubectl run dns-test --rm -it --restart=Never --image=busybox:1.36 -- nslookup keycloak.${local_domain} "
806810 fi
807811
808812 mark_step_done " $step_id "
0 commit comments