Skip to content

Commit

Permalink
GCP: Prepend resolv.conf on the bootstrap node with custom-dns
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sadasu committed Nov 15, 2024
1 parent f4c641c commit f84c973
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 <<EOF
if [[ "$STATUS" == dhcp* ]]; then
>&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
5 changes: 5 additions & 0 deletions pkg/asset/ignition/bootstrap/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down
21 changes: 21 additions & 0 deletions pkg/asset/ignition/bootstrap/gcp/template.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit f84c973

Please sign in to comment.