diff options
author | 2019-09-05 09:07:55 -0400 | |
---|---|---|
committer | 2019-09-05 09:07:55 -0400 | |
commit | 630d3d60b9fa3b9c1f070e4aa0504e50a85d70c2 (patch) | |
tree | b9b95d55ecca3964142f6d064b48065f1cb5128b /plugin/kubernetes/local.go | |
parent | d79562842aacfa5808ec0b8d160d720470ece060 (diff) | |
download | coredns-630d3d60b9fa3b9c1f070e4aa0504e50a85d70c2.tar.gz coredns-630d3d60b9fa3b9c1f070e4aa0504e50a85d70c2.tar.zst coredns-630d3d60b9fa3b9c1f070e4aa0504e50a85d70c2.zip |
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
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 + } } } } |