-
Notifications
You must be signed in to change notification settings - Fork 11
/
ns.go
90 lines (80 loc) · 2.44 KB
/
ns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package kubernetes
import (
"net"
"strings"
"github.com/coredns/coredns/plugin/kubernetes/object"
"github.com/miekg/dns"
api "k8s.io/api/core/v1"
)
func isDefaultNS(name, zone string) bool {
return strings.Index(name, defaultNSName) == 0 && strings.Index(name, zone) == len(defaultNSName)
}
// nsAddrs returns the A or AAAA records for the CoreDNS service in the cluster. If the service cannot be found,
// it returns a record for the local address of the machine we're running on.
func (k *Kubernetes) nsAddrs(external bool, zone string) []dns.RR {
var (
svcNames []string
svcIPs []net.IP
)
// Find the CoreDNS Endpoints
for _, localIP := range k.localIPs {
endpoints := k.APIConn.EpIndexReverse(localIP.String())
// Collect IPs for all Services of the Endpoints
for _, endpoint := range endpoints {
svcs := k.APIConn.SvcIndex(object.ServiceKey(endpoint.Name, endpoint.Namespace))
for _, svc := range svcs {
if external {
svcName := strings.Join([]string{svc.Name, svc.Namespace, zone}, ".")
for _, exIP := range svc.ExternalIPs {
svcNames = append(svcNames, svcName)
svcIPs = append(svcIPs, net.ParseIP(exIP))
}
continue
}
svcName := strings.Join([]string{svc.Name, svc.Namespace, Svc, zone}, ".")
if svc.ClusterIP == api.ClusterIPNone {
// For a headless service, use the endpoints IPs
for _, s := range endpoint.Subsets {
for _, a := range s.Addresses {
svcNames = append(svcNames, endpointHostname(a, k.endpointNameMode)+"."+svcName)
svcIPs = append(svcIPs, net.ParseIP(a.IP))
}
}
} else {
svcNames = append(svcNames, svcName)
svcIPs = append(svcIPs, net.ParseIP(svc.ClusterIP))
}
}
}
}
// If no local IPs matched any endpoints, use the localIPs directly
if len(svcIPs) == 0 {
svcIPs = make([]net.IP, len(k.localIPs))
svcNames = make([]string, len(k.localIPs))
for i, localIP := range k.localIPs {
svcNames[i] = defaultNSName + zone
svcIPs[i] = localIP
}
}
// Create an RR slice of collected IPs
rrs := make([]dns.RR, len(svcIPs))
for i, ip := range svcIPs {
if ip.To4() == nil {
rr := new(dns.AAAA)
rr.Hdr.Class = dns.ClassINET
rr.Hdr.Rrtype = dns.TypeAAAA
rr.Hdr.Name = svcNames[i]
rr.AAAA = ip
rrs[i] = rr
continue
}
rr := new(dns.A)
rr.Hdr.Class = dns.ClassINET
rr.Hdr.Rrtype = dns.TypeA
rr.Hdr.Name = svcNames[i]
rr.A = ip
rrs[i] = rr
}
return rrs
}
const defaultNSName = "ns.dns."