From f84c9733b961963117d6fd21100f405ef87da046 Mon Sep 17 00:00:00 2001 From: Sandhya Dasu Date: Fri, 15 Nov 2024 09:44:51 -0500 Subject: [PATCH] GCP: Prepend resolv.conf on the bootstrap node with custom-dns When UserProvisionedDNS is enabled during an install on GCP, prepend the NetworkManager generated resolv.conf file on the bootstrap node with the IP of the localhost. --- .../30-local-dns-prepender.template | 53 +++++++++++++++++++ pkg/asset/ignition/bootstrap/common.go | 5 ++ pkg/asset/ignition/bootstrap/gcp/template.go | 21 ++++++++ 3 files changed, 79 insertions(+) create mode 100755 data/data/bootstrap/gcp/files/etc/NetworkManager/dispatcher.d/30-local-dns-prepender.template create mode 100644 pkg/asset/ignition/bootstrap/gcp/template.go diff --git a/data/data/bootstrap/gcp/files/etc/NetworkManager/dispatcher.d/30-local-dns-prepender.template b/data/data/bootstrap/gcp/files/etc/NetworkManager/dispatcher.d/30-local-dns-prepender.template new file mode 100755 index 00000000000..a65a363a303 --- /dev/null +++ b/data/data/bootstrap/gcp/files/etc/NetworkManager/dispatcher.d/30-local-dns-prepender.template @@ -0,0 +1,53 @@ +#!/bin/bash +IFACE=$1 +STATUS=$2 +case "$STATUS" in + up|dhcp4-change|dhcp6-change|dns-change) +{{if .PlatformData.GCP.UserProvisionedDNS}} + logger -s "NM local-dns-prepender triggered by ${1} ${2}." + + # In DHCP connections, the resolv.conf content may be late, thus we wait for nameservers + timeout 45s /bin/bash <&2 echo "NM resolv-prepender: Checking for nameservers in /var/run/NetworkManager/resolv.conf" + while ! grep nameserver /var/run/NetworkManager/resolv.conf; do + >&2 echo "NM resolv-prepender: NM resolv.conf still empty of nameserver" + sleep 0.5 + done + fi +EOF + + DNS_IP="127.0.0.1" + set +e + if systemctl -q is-enabled systemd-resolved; then + >&2 echo "NM resolv-prepender: Setting up systemd-resolved for local DNS" + if [[ ! -f /etc/systemd/resolved.conf.d/60-kni.conf ]]; then + >&2 echo "NM resolv-prepender: Creating /etc/systemd/resolved.conf.d/60-kni.conf" + mkdir -p /etc/systemd/resolved.conf.d + echo "[Resolve]" > /etc/systemd/resolved.conf.d/60-kni.conf + echo "DNS=$DNS_IP" >> /etc/systemd/resolved.conf.d/60-kni.conf + echo "Domains=api.{{.ClusterDomain}} api-int.{{.ClusterDomain}} apps.{{.ClusterDomain}}" >> \ + /etc/systemd/resolved.conf.d/60-kni.conf + if systemctl -q is-active systemd-resolved; then + >&2 echo "NM resolv-prepender: restarting systemd-resolved" + systemctl restart systemd-resolved + fi + fi + else + cp -f /var/run/NetworkManager/resolv.conf /etc/resolv.tmp + sed -i "/^# Generated by.*$/a nameserver $DNS_IP" /etc/resolv.tmp + if cmp -s /etc/resolv.tmp /etc/resolv.conf; then + logger -s "NM local-dns-prepender: /etc/resolv.conf is already up to date" + rm -f /etc/resolv.tmp + exit 0 + + else + logger -s "NM local-dns-prepender: overwriting /etc/resolv.conf to add local DNS IP and DNS servers obtained by DHCP" + mv -f /etc/resolv.tmp /etc/resolv.conf + fi + fi +{{end}} + ;; + *) + ;; +esac diff --git a/pkg/asset/ignition/bootstrap/common.go b/pkg/asset/ignition/bootstrap/common.go index 0638d2b246d..8359a9965a9 100644 --- a/pkg/asset/ignition/bootstrap/common.go +++ b/pkg/asset/ignition/bootstrap/common.go @@ -29,6 +29,7 @@ import ( "github.com/openshift/installer/pkg/asset" "github.com/openshift/installer/pkg/asset/ignition" "github.com/openshift/installer/pkg/asset/ignition/bootstrap/baremetal" + "github.com/openshift/installer/pkg/asset/ignition/bootstrap/gcp" "github.com/openshift/installer/pkg/asset/ignition/bootstrap/vsphere" mcign "github.com/openshift/installer/pkg/asset/ignition/machine" "github.com/openshift/installer/pkg/asset/installconfig" @@ -40,6 +41,7 @@ import ( "github.com/openshift/installer/pkg/asset/tls" "github.com/openshift/installer/pkg/types" baremetaltypes "github.com/openshift/installer/pkg/types/baremetal" + gcptypes "github.com/openshift/installer/pkg/types/gcp" nutanixtypes "github.com/openshift/installer/pkg/types/nutanix" vspheretypes "github.com/openshift/installer/pkg/types/vsphere" ) @@ -99,6 +101,7 @@ type bootstrapTemplateData struct { type platformTemplateData struct { BareMetal *baremetal.TemplateData VSphere *vsphere.TemplateData + GCP *gcp.TemplateData } // Common is an asset that generates the ignition config for bootstrap nodes. @@ -314,6 +317,8 @@ func (a *Common) getTemplateData(dependencies asset.Parents, bootstrapInPlace bo ironicCreds.Password, dependencies, ) + case gcptypes.Name: + platformData.GCP = gcp.GetTemplateData(installConfig.Config.Platform.GCP) case vspheretypes.Name: platformData.VSphere = vsphere.GetTemplateData(installConfig.Config.Platform.VSphere) } diff --git a/pkg/asset/ignition/bootstrap/gcp/template.go b/pkg/asset/ignition/bootstrap/gcp/template.go new file mode 100644 index 00000000000..42e60c26336 --- /dev/null +++ b/pkg/asset/ignition/bootstrap/gcp/template.go @@ -0,0 +1,21 @@ +package gcp + +import ( + "github.com/openshift/installer/pkg/types/dns" + "github.com/openshift/installer/pkg/types/gcp" +) + +// TemplateData holds data specific to templates used for the gcp platform. +type TemplateData struct { + // UserProvisionedDNS indicates whether this feature has been enabled on GCP + UserProvisionedDNS bool +} + +// GetTemplateData returns platform-specific data for bootstrap templates. +func GetTemplateData(config *gcp.Platform) *TemplateData { + var templateData TemplateData + + templateData.UserProvisionedDNS = (config.UserProvisionedDNS == dns.UserProvisionedDNSEnabled) + + return &templateData +}