filesystem: "root"
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
    IFACE=$1
    STATUS=$2
    # 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}."
        NAMESERVER_IP=$(/usr/local/bin/non_virtual_ip \
            "{{.Infra.Status.PlatformStatus.VSphere.APIServerInternalIP}}" \
            "{{.Infra.Status.PlatformStatus.VSphere.IngressIP}}")
        DOMAIN="{{.EtcdDiscoveryDomain}}"
        if [[ -n "$NAMESERVER_IP" ]]; then
            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
        else
            logger -s "Couldn't find a non-virtual IP, just updating resolv.conf"
            cp /var/run/NetworkManager/resolv.conf /etc/resolv.tmp
        fi
        mv -f /etc/resolv.tmp /etc/resolv.conf
        ;;
        *)
        ;;
    esac
    {{ end -}}
    {{ end -}}
    {{ end -}}
    {{ end -}}
    {{ end -}}
