aboutsummaryrefslogtreecommitdiff
path: root/plugin/k8s_external/transfer.go
blob: 781f19f21f3d2920084402c27fedb7180fc5b136 (plain) (blame)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package external

import (
	"context"
	"strings"

	"github.com/coredns/coredns/plugin"
	"github.com/coredns/coredns/plugin/etcd/msg"
	"github.com/coredns/coredns/plugin/kubernetes"
	"github.com/coredns/coredns/plugin/transfer"
	"github.com/coredns/coredns/request"

	"github.com/miekg/dns"
)

// Transfer implements transfer.Transferer
func (e *External) Transfer(zone string, serial uint32) (<-chan []dns.RR, error) {
	z := plugin.Zones(e.Zones).Matches(zone)
	if z != zone {
		return nil, transfer.ErrNotAuthoritative
	}

	ctx := context.Background()
	ch := make(chan []dns.RR, 2)
	if zone == "." {
		zone = ""
	}
	state := request.Request{Zone: zone}

	// SOA
	soa := e.soa(state)
	ch <- []dns.RR{soa}
	if serial != 0 && serial >= soa.Serial {
		close(ch)
		return ch, nil
	}

	go func() {
		// Add NS
		nsName := "ns1." + e.apex + "." + zone
		nsHdr := dns.RR_Header{Name: zone, Rrtype: dns.TypeNS, Ttl: e.ttl, Class: dns.ClassINET}
		ch <- []dns.RR{&dns.NS{Hdr: nsHdr, Ns: nsName}}

		// Add Nameserver A/AAAA records
		nsRecords := e.externalAddrFunc(state, e.headless)
		for i := range nsRecords {
			// externalAddrFunc returns incomplete header names, correct here
			nsRecords[i].Header().Name = nsName
			nsRecords[i].Header().Ttl = e.ttl
			ch <- []dns.RR{nsRecords[i]}
		}

		svcs, headlessSvcs := e.externalServicesFunc(zone, e.headless)
		srvSeen := make(map[string]struct{})

		for i := range svcs {
			name := msg.Domain(svcs[i].Key)

			if svcs[i].TargetStrip == 0 {
				// Add Service A/AAAA records
				s := request.Request{Req: &dns.Msg{Question: []dns.Question{{Name: name}}}}
				as, _ := e.a(ctx, []msg.Service{svcs[i]}, s)
				if len(as) > 0 {
					ch <- as
				}
				aaaas, _ := e.aaaa(ctx, []msg.Service{svcs[i]}, s)
				if len(aaaas) > 0 {
					ch <- aaaas
				}
				// Add bare SRV record, ensuring uniqueness
				recs, _ := e.srv(ctx, []msg.Service{svcs[i]}, s)
				for _, srv := range recs {
					if !nameSeen(srvSeen, srv) {
						ch <- []dns.RR{srv}
					}
				}
				continue
			}
			// Add full SRV record, ensuring uniqueness
			s := request.Request{Req: &dns.Msg{Question: []dns.Question{{Name: name}}}}
			recs, _ := e.srv(ctx, []msg.Service{svcs[i]}, s)
			for _, srv := range recs {
				if !nameSeen(srvSeen, srv) {
					ch <- []dns.RR{srv}
				}
			}
		}
		for key, svcs := range headlessSvcs {
			// we have to strip the leading key because it's either port.protocol or endpoint
			name := msg.Domain(key[:strings.LastIndex(key, "/")])
			switchKey := key[strings.LastIndex(key, "/")+1:]
			switch switchKey {
			case kubernetes.Endpoint:
				// headless.namespace.example.com records
				s := request.Request{Req: &dns.Msg{Question: []dns.Question{{Name: name}}}}
				as, _ := e.a(ctx, svcs, s)
				if len(as) > 0 {
					ch <- as
				}
				aaaas, _ := e.aaaa(ctx, svcs, s)
				if len(aaaas) > 0 {
					ch <- aaaas
				}
				// Add bare SRV record, ensuring uniqueness
				recs, _ := e.srv(ctx, svcs, s)
				ch <- recs
				for _, srv := range recs {
					ch <- []dns.RR{srv}
				}

				for i := range svcs {
					// endpoint.headless.namespace.example.com record
					s := request.Request{Req: &dns.Msg{Question: []dns.Question{{Name: msg.Domain(svcs[i].Key)}}}}

					as, _ := e.a(ctx, []msg.Service{svcs[i]}, s)
					if len(as) > 0 {
						ch <- as
					}
					aaaas, _ := e.aaaa(ctx, []msg.Service{svcs[i]}, s)
					if len(aaaas) > 0 {
						ch <- aaaas
					}
					// Add bare SRV record, ensuring uniqueness
					recs, _ := e.srv(ctx, []msg.Service{svcs[i]}, s)
					ch <- recs
					for _, srv := range recs {
						ch <- []dns.RR{srv}
					}
				}

			case kubernetes.PortProtocol:
				s := request.Request{Req: &dns.Msg{Question: []dns.Question{{Name: name}}}}
				recs, _ := e.srv(ctx, svcs, s)
				ch <- recs
			}
		}
		ch <- []dns.RR{soa}
		close(ch)
	}()

	return ch, nil
}

func nameSeen(namesSeen map[string]struct{}, rr dns.RR) bool {
	if _, duplicate := namesSeen[rr.Header().Name]; duplicate {
		return true
	}
	namesSeen[rr.Header().Name] = struct{}{}
	return false
}