From aadbd9f671fbcff41ce55f59cab154413a59a93c Mon Sep 17 00:00:00 2001 From: Sandhya Dasu Date: Wed, 22 Jan 2025 12:05:35 -0500 Subject: [PATCH] GCP: Update /etc/hosts file when ClusterHostedDNS is enable Append /etc/hosts files with entries to resolve cluster api and api-int URLS. /etc/hosts will provide resolution for these URLs until kubelet joins the cluster and runs its CoreDNS pod which will then take over resolution of those 2 URLs --- pkg/controller/template/render.go | 4 -- .../files/usr-local-bin-update-etc-hosts.yaml | 11 ++++++ .../units/gcp-update-etc-hosts.service.yaml | 39 +++++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 templates/common/gcp/files/usr-local-bin-update-etc-hosts.yaml create mode 100644 templates/common/gcp/units/gcp-update-etc-hosts.service.yaml diff --git a/pkg/controller/template/render.go b/pkg/controller/template/render.go index 2fd20f39e0..d57070eb88 100644 --- a/pkg/controller/template/render.go +++ b/pkg/controller/template/render.go @@ -11,7 +11,6 @@ import ( "strings" "text/template" - "github.com/sirupsen/logrus" "k8s.io/klog/v2" configv1 "github.com/openshift/api/config/v1" @@ -782,16 +781,13 @@ func cloudPlatformIngressLoadBalancerIPs(cfg RenderConfig) (interface{}, error) // cloudPlatformLBIPAvailable returns true when DNSType is set to `ClusterHosted` // and LB IPs are provided as part of `PlatformStatus`. func cloudPlatformLBIPAvailable(cfg RenderConfig) bool { - logrus.Infof("Inside cloudPlatformLBIPAvailable") if cfg.Infra.Status.PlatformStatus != nil { switch cfg.Infra.Status.PlatformStatus.Type { case configv1.GCPPlatformType: switch cloudPlatformLoadBalancerIPState(cfg) { case availableLBIPState: - logrus.Infof("LB IPs available") return true default: - logrus.Infof("LB IPs not available") return false } case configv1.AWSPlatformType: diff --git a/templates/common/gcp/files/usr-local-bin-update-etc-hosts.yaml b/templates/common/gcp/files/usr-local-bin-update-etc-hosts.yaml new file mode 100644 index 0000000000..0dcfe5fbb6 --- /dev/null +++ b/templates/common/gcp/files/usr-local-bin-update-etc-hosts.yaml @@ -0,0 +1,11 @@ +mode: 0755 +path: "/usr/local/bin/update-etc-hosts" +contents: + inline: | + #!/bin/bash + etc_hosts_config_filename="/etc/conf.d/etc-hosts.conf" + if [ ! -f "$etc_hosts_config_filename" ]; then + exit 0 + fi + cat /etc/conf.d/etc-hosts.conf >> /etc/hosts + echo "Done updating /etc/hosts" diff --git a/templates/common/gcp/units/gcp-update-etc-hosts.service.yaml b/templates/common/gcp/units/gcp-update-etc-hosts.service.yaml new file mode 100644 index 0000000000..49d464ce6b --- /dev/null +++ b/templates/common/gcp/units/gcp-update-etc-hosts.service.yaml @@ -0,0 +1,39 @@ +name: gcp-update-etc-hosts.service +enabled: {{if and (eq .Infra.Status.PlatformStatus.Type "GCP") (.Infra.Status.PlatformStatus.GCP) (.Infra.Status.PlatformStatus.GCP.CloudLoadBalancerConfig) (eq .Infra.Status.PlatformStatus.GCP.CloudLoadBalancerConfig.DNSType "ClusterHosted") }}true{{else}}false{{end}} +contents: | + [Unit] + Description=Update Default GCP /etc/hosts + # We don't need to do this on the firstboot + After=firstboot-osupdate.target + # Wait for NetworkManager to report it's online + After=NetworkManager-wait-online.service + # Run before kubelet + Before=kubelet-dependencies.target + + [Service] + # Need oneshot to delay kubelet + Type=oneshot + ExecStart=/bin/bash -c " \ + {{ if and (cloudPlatformLBIPAvailable .) (gt (len (cloudPlatformAPIIntLoadBalancerIPs .)) 0) }} \ + apiIntLBIPs={{ cloudPlatformAPIIntLoadBalancerIPs . }} \ + {{ end }} \ + {{ if and (cloudPlatformLBIPAvailable .) (gt (len (cloudPlatformAPILoadBalancerIPs .)) 0) }} \ + apiLBIPs={{ cloudPlatformAPILoadBalancerIPs . }} \ + {{ end }} \ + if [ -z "$apiLBIPs" ]; then \ + # apiLBIPs will not be set on private clusters + apiLBIPs=$apiIntLBIPs \ + fi \ + apiServerURL={{ .Infra.Status.APIServerURL }} \ + apiServerIntURL={{ .Infra.Status.APIServerInternalURL }} \ + mkdir -p /etc/conf.d \ + cat <