mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 08:14:18 -04:00
plugin/k8s_external: Add support for PTR requests (#5435)
* Exclude External IP addresses from being added to the existing kubernetes' plugin IP->Service index * Add support for PTR requests on External IPs of Services to the k8s_external plugin Signed-off-by: Chris O'Haver <cohaver@infoblox.com>
This commit is contained in:
@@ -10,7 +10,7 @@ This plugin allows an additional zone to resolve the external IP address(es) of
|
||||
service. This plugin is only useful if the *kubernetes* plugin is also loaded.
|
||||
|
||||
The plugin uses an external zone to resolve in-cluster IP addresses. It only handles queries for A,
|
||||
AAAA and SRV records; all others result in NODATA responses. To make it a proper DNS zone, it handles
|
||||
AAAA, SRV, and PTR records; all others result in NODATA responses. To make it a proper DNS zone, it handles
|
||||
SOA and NS queries for the apex of the zone.
|
||||
|
||||
By default the apex of the zone will look like the following (assuming the zone used is `example.org`):
|
||||
@@ -101,6 +101,3 @@ zone transfers. Notifies are not supported.
|
||||
For some background see [resolve external IP address](https://github.com/kubernetes/dns/issues/242).
|
||||
And [A records for services with Load Balancer IP](https://github.com/coredns/coredns/issues/1851).
|
||||
|
||||
# Bugs
|
||||
|
||||
PTR queries for the reverse zone is not supported.
|
||||
|
||||
@@ -105,6 +105,8 @@ func (e *External) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms
|
||||
m.Answer, m.Truncated = e.aaaa(ctx, svc, state)
|
||||
case dns.TypeSRV:
|
||||
m.Answer, m.Extra = e.srv(ctx, svc, state)
|
||||
case dns.TypePTR:
|
||||
m.Answer = e.ptr(svc, state)
|
||||
default:
|
||||
m.Ns = []dns.RR{e.soa(state)}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ func TestExternal(t *testing.T) {
|
||||
k.APIConn = &external{}
|
||||
|
||||
e := New()
|
||||
e.Zones = []string{"example.com."}
|
||||
e.Zones = []string{"example.com.", "in-addr.arpa."}
|
||||
e.Next = test.NextHandler(dns.RcodeSuccess, nil)
|
||||
e.externalFunc = k.External
|
||||
e.externalAddrFunc = externalAddress // internal test function
|
||||
@@ -49,12 +49,33 @@ func TestExternal(t *testing.T) {
|
||||
t.Error("Expected authoritative answer")
|
||||
}
|
||||
if err = test.SortAndCheck(resp, tc); err != nil {
|
||||
t.Error(err)
|
||||
t.Errorf("Test %d: %v", i, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var tests = []test.Case{
|
||||
// PTR reverse lookup
|
||||
{
|
||||
Qname: "4.3.2.1.in-addr.arpa.", Qtype: dns.TypePTR, Rcode: dns.RcodeSuccess,
|
||||
Answer: []dns.RR{
|
||||
test.PTR("4.3.2.1.in-addr.arpa. 5 IN PTR svc1.testns.example.com."),
|
||||
},
|
||||
},
|
||||
// Bad PTR reverse lookup using existing service name
|
||||
{
|
||||
Qname: "svc1.testns.example.com.", Qtype: dns.TypePTR, Rcode: dns.RcodeSuccess,
|
||||
Ns: []dns.RR{
|
||||
test.SOA("example.com. 5 IN SOA ns1.dns.example.com. hostmaster.example.com. 1499347823 7200 1800 86400 5"),
|
||||
},
|
||||
},
|
||||
// Bad PTR reverse lookup using non-existing service name
|
||||
{
|
||||
Qname: "not-existing.testns.example.com.", Qtype: dns.TypePTR, Rcode: dns.RcodeNameError,
|
||||
Ns: []dns.RR{
|
||||
test.SOA("example.com. 5 IN SOA ns1.dns.example.com. hostmaster.example.com. 1499347823 7200 1800 86400 5"),
|
||||
},
|
||||
},
|
||||
// A Service
|
||||
{
|
||||
Qname: "svc1.testns.example.com.", Qtype: dns.TypeA, Rcode: dns.RcodeSuccess,
|
||||
@@ -155,7 +176,7 @@ var tests = []test.Case{
|
||||
{
|
||||
Qname: "svc11.testns.example.com.", Qtype: dns.TypeA, Rcode: dns.RcodeSuccess,
|
||||
Answer: []dns.RR{
|
||||
test.A("svc11.testns.example.com. 5 IN A 1.2.3.4"),
|
||||
test.A("svc11.testns.example.com. 5 IN A 2.3.4.5"),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -164,7 +185,7 @@ var tests = []test.Case{
|
||||
test.SRV("_http._tcp.svc11.testns.example.com. 5 IN SRV 0 100 80 svc11.testns.example.com."),
|
||||
},
|
||||
Extra: []dns.RR{
|
||||
test.A("svc11.testns.example.com. 5 IN A 1.2.3.4"),
|
||||
test.A("svc11.testns.example.com. 5 IN A 2.3.4.5"),
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -173,7 +194,7 @@ var tests = []test.Case{
|
||||
test.SRV("svc11.testns.example.com. 5 IN SRV 0 100 80 svc11.testns.example.com."),
|
||||
},
|
||||
Extra: []dns.RR{
|
||||
test.A("svc11.testns.example.com. 5 IN A 1.2.3.4"),
|
||||
test.A("svc11.testns.example.com. 5 IN A 2.3.4.5"),
|
||||
},
|
||||
},
|
||||
// svc12
|
||||
@@ -211,6 +232,20 @@ func (external) GetNodeByName(ctx context.Context, name string) (*api.Node, erro
|
||||
func (external) SvcIndex(s string) []*object.Service { return svcIndexExternal[s] }
|
||||
func (external) PodIndex(string) []*object.Pod { return nil }
|
||||
|
||||
func (external) SvcExtIndexReverse(ip string) (result []*object.Service) {
|
||||
for _, svcs := range svcIndexExternal {
|
||||
for _, svc := range svcs {
|
||||
for _, exIp := range svc.ExternalIPs {
|
||||
if exIp != ip {
|
||||
continue
|
||||
}
|
||||
result = append(result, svc)
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (external) GetNamespaceByName(name string) (*object.Namespace, error) {
|
||||
return &object.Namespace{
|
||||
Name: name,
|
||||
@@ -243,7 +278,7 @@ var svcIndexExternal = map[string][]*object.Service{
|
||||
Name: "svc11",
|
||||
Namespace: "testns",
|
||||
Type: api.ServiceTypeLoadBalancer,
|
||||
ExternalIPs: []string{"1.2.3.4"},
|
||||
ExternalIPs: []string{"2.3.4.5"},
|
||||
Ports: []api.ServicePort{{Name: "http", Protocol: "tcp", Port: 80}},
|
||||
},
|
||||
},
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"math"
|
||||
|
||||
"github.com/coredns/coredns/plugin/etcd/msg"
|
||||
"github.com/coredns/coredns/plugin/pkg/dnsutil"
|
||||
"github.com/coredns/coredns/request"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
@@ -76,6 +77,19 @@ func (e *External) aaaa(ctx context.Context, services []msg.Service, state reque
|
||||
return records, truncated
|
||||
}
|
||||
|
||||
func (e *External) ptr(services []msg.Service, state request.Request) (records []dns.RR) {
|
||||
dup := make(map[string]struct{})
|
||||
for _, s := range services {
|
||||
if _, ok := dup[s.Host]; !ok {
|
||||
dup[s.Host] = struct{}{}
|
||||
rr := s.NewPTR(state.QName(), dnsutil.Join(s.Host, e.Zones[0]))
|
||||
rr.Hdr.Ttl = e.ttl
|
||||
records = append(records, rr)
|
||||
}
|
||||
}
|
||||
return records
|
||||
}
|
||||
|
||||
func (e *External) srv(ctx context.Context, services []msg.Service, state request.Request) (records, extra []dns.RR) {
|
||||
dup := make(map[item]struct{})
|
||||
|
||||
|
||||
@@ -59,6 +59,11 @@ func TestTransferAXFR(t *testing.T) {
|
||||
if ans.Header().Rrtype == dns.TypeTXT {
|
||||
continue
|
||||
}
|
||||
|
||||
// Exclude PTR records
|
||||
if ans.Header().Rrtype == dns.TypePTR {
|
||||
continue
|
||||
}
|
||||
expect = append(expect, ans)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user