diff options
Diffstat (limited to 'plugin/kubernetes/local.go')
-rw-r--r-- | plugin/kubernetes/local.go | 49 |
1 files changed, 31 insertions, 18 deletions
diff --git a/plugin/kubernetes/local.go b/plugin/kubernetes/local.go index 199af6f0d..d09255061 100644 --- a/plugin/kubernetes/local.go +++ b/plugin/kubernetes/local.go @@ -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 + } } } } |