Skip to content

Commit

Permalink
GCP: Update /etc/hosts file when ClusterHostedDNS is enable
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sadasu committed Jan 27, 2025
1 parent 2e39cbc commit 8f34527
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
11 changes: 11 additions & 0 deletions templates/common/gcp/files/usr-local-bin-update-etc-hosts.yaml
Original file line number Diff line number Diff line change
@@ -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"
34 changes: 34 additions & 0 deletions templates/common/gcp/units/gcp-update-etc-hosts.service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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=$apiIntLBIPs \
fi \
apiServerURL={{ .Infra.Status.APIServerURL }} \
apiServerIntURL={{ .Infra.Status.APIServerInternalURL }} \
mkdir -p /etc/conf.d \
cat <<EOF | tee /etc/conf.d/etc-hosts.conf \
# Added by OpenShift \
${apiLBIPs[0]} ${apiServerURL} \
${apiIntLBIPs[0]} ${apiServerIntURL} \
EOF \
# Update /etc/hosts \
/usr/local/bin/update-etc-hosts"
[Install]
RequiredBy=kubelet-dependencies.target

0 comments on commit 8f34527

Please sign in to comment.