-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
plugin/kubernetes: Handle multiple local IPs and bind #3208
Changes from all commits
3e901b2
0514696
98608f2
837b755
1b0a47c
8742ca8
661a216
1901720
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
At least partly ... "LocalNodeName is exclusively used in federation plugin": True, only used by "... will be deprecated later": Maybe - depends on what "remove" means in #3041. I thought it means deprecate/remove... but maybe it just means to move it to an external repo (since thats all that was done, and the issue was closed). |
||
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 | ||
} | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
seen
to avoid answer duplication in cases where a single answer has multiple additional records.