forked from coredns/coredns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ns.go
61 lines (50 loc) · 1.19 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
package kubernetes
import (
"net"
"strings"
"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)
}
func (k *Kubernetes) nsAddr() *dns.A {
var (
svcName string
svcNamespace string
)
rr := new(dns.A)
localIP := k.interfaceAddrsFunc()
rr.A = localIP
FindEndpoint:
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)) {
svcNamespace = ep.ObjectMeta.Namespace
svcName = ep.ObjectMeta.Name
break FindEndpoint
}
}
}
}
if len(svcName) == 0 {
rr.Hdr.Name = defaultNSName
rr.A = localIP
return rr
}
FindService:
for _, svc := range k.APIConn.ServiceList() {
if svcName == svc.Name && svcNamespace == svc.Namespace {
if svc.Spec.ClusterIP == api.ClusterIPNone {
rr.A = localIP
} else {
rr.A = net.ParseIP(svc.Spec.ClusterIP)
}
break FindService
}
}
rr.Hdr.Name = strings.Join([]string{svcName, svcNamespace, "svc."}, ".")
return rr
}
const defaultNSName = "ns.dns."