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 23, 2025
1 parent 6fef6ad commit 0236f68
Show file tree
Hide file tree
Showing 2 changed files with 55 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/hosts/conf.d/etc-hosts.conf"
if [ ! -f "$etc_hosts_config_filename" ]; then
exit 0
fi
cat /etc/hosts/conf.d/etc-hosts.conf >> /etc/hosts
echo "Done updating /etc/hosts"
44 changes: 44 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,44 @@
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 (eq .Infra.Status.PlatformStatus.Type "GCP") (.Infra.Status.PlatformStatus.GCP) (.Infra.Status.PlatformStatus.GCP.CloudLoadBalancerConfig) (eq .Infra.Status.PlatformStatus.GCP.CloudLoadBalancerConfig.DNSType "ClusterHosted") }}
{{ $apiLBIPs := cloudPlatformAPILoadBalancerIPs .}}
{{ $apiIntLBIPs := cloudPlatformAPIIntLoadBalancerIPs .}}
{{ else }}
exit 0
{{ end }}
if [ -z "${apiIntLBIPs}" ]; then
# We don't have API-Int LB IPs. Nothing to do here.
exit 0
fi
if [ -z "${apiLBIPs}" ]; then
# Private cluster
apiLBIPs=${apiIntLBIPs}
fi
apiServerURL={{ .Infra.Status.APIServerURL }}
apiServerIntURL={{ .Infra.Status.APIServerInternalURL }}
# Add the/etc/hosts configuration file
mkdir -p /etc/hosts/conf.d
cat <<EOF | tee /etc/hosts/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 0236f68

Please sign in to comment.