plugin/kubernetes: Handle multiple local IPs and bind (#3208)

* use all local IPs

* mult/bind ips

* gofmt + boundIPs fix

* fix no matching endpoint case

* don't duplicate NS records in answer

* fix answer dedup

* fix comment

* add multi local ip test case
This commit is contained in:
Chris O'Haver 2019-09-05 09:07:55 -04:00 committed by GitHub
parent d79562842a
commit 630d3d60b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 91 additions and 51 deletions

View file

@ -2,41 +2,54 @@ package kubernetes
import (
"net"
"github.com/caddyserver/caddy"
"github.com/coredns/coredns/core/dnsserver"
)
func localPodIP() net.IP {
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil
// boundIPs returns the list of non-loopback IPs that CoreDNS is bound to
func boundIPs(c *caddy.Controller) (ips []net.IP) {
conf := dnsserver.GetConfig(c)
hosts := conf.ListenHosts
if hosts == nil || hosts[0] == "" {
hosts = nil
addrs, err := net.InterfaceAddrs()
if err != nil {
return nil
}
for _, addr := range addrs {
hosts = append(hosts, addr.String())
}
}
for _, addr := range addrs {
ip, _, _ := net.ParseCIDR(addr.String())
for _, host := range hosts {
ip, _, _ := net.ParseCIDR(host)
ip4 := ip.To4()
if ip4 != nil && !ip4.IsLoopback() {
return ip4
ips = append(ips, ip4)
continue
}
ip6 := ip.To16()
if ip6 != nil && !ip6.IsLoopback() {
return ip6
ips = append(ips, ip6)
}
}
return nil
return ips
}
// LocalNodeName is exclusively used in federation plugin, will be deprecated later.
func (k *Kubernetes) LocalNodeName() string {
localIP := k.interfaceAddrsFunc()
if localIP == nil {
if len(k.localIPs) == 0 {
return ""
}
// Find endpoint matching localIP
for _, ep := range k.APIConn.EpIndexReverse(localIP.String()) {
for _, eps := range ep.Subsets {
for _, addr := range eps.Addresses {
if localIP.Equal(net.ParseIP(addr.IP)) {
return addr.NodeName
// Find fist endpoint matching any localIP
for _, localIP := range k.localIPs {
for _, ep := range k.APIConn.EpIndexReverse(localIP.String()) {
for _, eps := range ep.Subsets {
for _, addr := range eps.Addresses {
if localIP.Equal(net.ParseIP(addr.IP)) {
return addr.NodeName
}
}
}
}