diff --git a/plugin/k8s_external/external.go b/plugin/k8s_external/external.go index 1dffe1daa..720556969 100644 --- a/plugin/k8s_external/external.go +++ b/plugin/k8s_external/external.go @@ -97,7 +97,7 @@ func (e *External) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms case dns.TypeAAAA: m.Answer = e.aaaa(ctx, svc, state) case dns.TypeSRV: - m.Answer, m.Extra = e.srv(svc, state) + m.Answer, m.Extra = e.srv(ctx, svc, state) default: m.Ns = []dns.RR{e.soa(state)} } diff --git a/plugin/k8s_external/external_test.go b/plugin/k8s_external/external_test.go index 02266a71a..cc29fd7a6 100644 --- a/plugin/k8s_external/external_test.go +++ b/plugin/k8s_external/external_test.go @@ -153,6 +153,12 @@ var tests = []test.Case{ test.A("svc11.testns.example.com. 5 IN A 1.2.3.4"), }, }, + { + Qname: "_http._tcp.svc12.testns.example.com.", Qtype: dns.TypeSRV, Rcode: dns.RcodeSuccess, + Answer: []dns.RR{ + test.SRV("_http._tcp.svc12.testns.example.com. 5 IN SRV 0 100 80 dummy.hostname."), + }, + }, { Qname: "svc12.testns.example.com.", Qtype: dns.TypeA, Rcode: dns.RcodeSuccess, Answer: []dns.RR{ diff --git a/plugin/k8s_external/msg_to_dns.go b/plugin/k8s_external/msg_to_dns.go index e61adf657..8920f4a00 100644 --- a/plugin/k8s_external/msg_to_dns.go +++ b/plugin/k8s_external/msg_to_dns.go @@ -22,9 +22,7 @@ func (e *External) a(ctx context.Context, services []msg.Service, state request. rr := s.NewCNAME(state.QName(), s.Host) records = append(records, rr) if resp, err := e.upstream.Lookup(ctx, state, dns.Fqdn(s.Host), dns.TypeA); err == nil { - for _, rr := range resp.Answer { - records = append(records, rr) - } + records = append(records, resp.Answer...) } case dns.TypeA: @@ -54,9 +52,7 @@ func (e *External) aaaa(ctx context.Context, services []msg.Service, state reque rr := s.NewCNAME(state.QName(), s.Host) records = append(records, rr) if resp, err := e.upstream.Lookup(ctx, state, dns.Fqdn(s.Host), dns.TypeAAAA); err == nil { - for _, rr := range resp.Answer { - records = append(records, rr) - } + records = append(records, resp.Answer...) } case dns.TypeA: @@ -74,7 +70,7 @@ func (e *External) aaaa(ctx context.Context, services []msg.Service, state reque return records } -func (e *External) srv(services []msg.Service, state request.Request) (records, extra []dns.RR) { +func (e *External) srv(ctx context.Context, services []msg.Service, state request.Request) (records, extra []dns.RR) { dup := make(map[item]struct{}) // Looping twice to get the right weight vs priority. This might break because we may drop duplicate SRV records latter on. @@ -111,9 +107,21 @@ func (e *External) srv(services []msg.Service, state request.Request) (records, what, ip := s.HostType() switch what { - case dns.TypeCNAME: - // can't happen + case dns.TypeCNAME: + addr := dns.Fqdn(s.Host) + srv := s.NewSRV(state.QName(), weight) + if ok := isDuplicate(dup, srv.Target, "", srv.Port); !ok { + records = append(records, srv) + } + if ok := isDuplicate(dup, srv.Target, addr, 0); !ok { + if resp, err := e.upstream.Lookup(ctx, state, addr, dns.TypeA); err == nil { + extra = append(extra, resp.Answer...) + } + if resp, err := e.upstream.Lookup(ctx, state, addr, dns.TypeAAAA); err == nil { + extra = append(extra, resp.Answer...) + } + } case dns.TypeA, dns.TypeAAAA: addr := s.Host s.Host = msg.Domain(s.Key)