mode: 0755
path: "/etc/NetworkManager/dispatcher.d/30-resolv-prepender"
contents:
  inline: |
    {{ if .Infra -}}
    {{ if .Infra.Status -}}
    {{ if .Infra.Status.PlatformStatus -}}
    {{ if .Infra.Status.PlatformStatus.VSphere -}}
    {{ if .Infra.Status.PlatformStatus.VSphere.APIServerInternalIP -}}
    #!/bin/bash
    set -eo pipefail
    IFACE=$1
    STATUS=$2

    {{if .Proxy -}}
    {{if .Proxy.HTTPProxy -}}
    export HTTP_PROXY={{.Proxy.HTTPProxy}}
    {{end -}}
    {{if .Proxy.HTTPSProxy -}}
    export HTTPS_PROXY={{.Proxy.HTTPSProxy}}
    {{end -}}
    {{if .Proxy.NoProxy -}}
    export NO_PROXY={{.Proxy.NoProxy}}
    {{end -}}
    {{end -}}

    # If $DHCP6_FQDN_FQDN is not empty and is not localhost.localdomain
    [[ -n "$DHCP6_FQDN_FQDN" && "$DHCP6_FQDN_FQDN" != "localhost.localdomain" && "$DHCP6_FQDN_FQDN" =~ "." ]] && hostnamectl set-hostname --static --transient $DHCP6_FQDN_FQDN
    case "$STATUS" in
        up|down|dhcp4-change|dhcp6-change)
        logger -s "NM resolv-prepender triggered by ${1} ${2}."

        # Ensure resolv.conf exists before we try to run podman
        if [[ ! -e /etc/resolv.conf ]] || ! grep -q nameserver /etc/resolv.conf; then
            cp /var/run/NetworkManager/resolv.conf /etc/resolv.conf
        fi

        NAMESERVER_IP=$(/usr/bin/podman run --rm \
            --authfile /var/lib/kubelet/config.json \
            --net=host \
            {{ .Images.baremetalRuntimeCfgImage }} \
            node-ip \
            show \
            "{{.Infra.Status.PlatformStatus.VSphere.APIServerInternalIP}}" \
            "{{.Infra.Status.PlatformStatus.VSphere.IngressIP}}")
        DOMAIN="{{.Infra.Status.EtcdDiscoveryDomain}}"

        if [[ -n "$NAMESERVER_IP" ]]; then
            if systemctl -q is-enabled systemd-resolved; then
                >&2 echo "NM resolv-prepender: Setting up systemd-resolved for OKD domain and local IP"
                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"
                    echo "[Resolve]" > /etc/systemd/resolved.conf.d/60-kni.conf
                    echo "DNS=$NAMESERVER_IP" >> /etc/systemd/resolved.conf.d/60-kni.conf
                    echo "Domains=$DOMAIN" >> /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
                logger -s "NM resolv-prepender: Prepending 'nameserver $NAMESERVER_IP' to /etc/resolv.conf (other nameservers from /var/run/NetworkManager/resolv.conf)"
                sed -e "/^search/d" \
                    -e "/Generated by/c# Generated by KNI resolv prepender NM dispatcher script\nsearch $DOMAIN\nnameserver $NAMESERVER_IP" \
                    /var/run/NetworkManager/resolv.conf > /etc/resolv.tmp
                mv -f /etc/resolv.tmp /etc/resolv.conf
            fi
        fi
        ;;
        *)
        ;;
    esac
    {{ end -}}
    {{ end -}}
    {{ end -}}
    {{ end -}}
    {{ end -}}
