k8s_external can now resolve CNAME returned by AWS ELB/NLB (#3916)

Automatically submitted.
This commit is contained in:
Michael Kashin 2020-05-29 10:04:23 -07:00 committed by GitHub
parent 54fb2112ac
commit 2e3ef77731
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 17 deletions

View file

@ -16,6 +16,7 @@ import (
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/etcd/msg" "github.com/coredns/coredns/plugin/etcd/msg"
"github.com/coredns/coredns/plugin/pkg/upstream"
"github.com/coredns/coredns/request" "github.com/coredns/coredns/request"
"github.com/miekg/dns" "github.com/miekg/dns"
@ -39,6 +40,8 @@ type External struct {
apex string apex string
ttl uint32 ttl uint32
upstream *upstream.Upstream
externalFunc func(request.Request) ([]msg.Service, int) externalFunc func(request.Request) ([]msg.Service, int)
externalAddrFunc func(request.Request) []dns.RR externalAddrFunc func(request.Request) []dns.RR
} }
@ -90,9 +93,9 @@ func (e *External) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Ms
switch state.QType() { switch state.QType() {
case dns.TypeA: case dns.TypeA:
m.Answer = e.a(svc, state) m.Answer = e.a(ctx, svc, state)
case dns.TypeAAAA: case dns.TypeAAAA:
m.Answer = e.aaaa(svc, state) m.Answer = e.aaaa(ctx, svc, state)
case dns.TypeSRV: case dns.TypeSRV:
m.Answer, m.Extra = e.srv(svc, state) m.Answer, m.Extra = e.srv(svc, state)
default: default:

View file

@ -41,6 +41,7 @@ func TestExternal(t *testing.T) {
} }
resp := w.Msg resp := w.Msg
if resp == nil { if resp == nil {
t.Fatalf("Test %d, got nil message and no error for %q", i, r.Question[0].Name) t.Fatalf("Test %d, got nil message and no error for %q", i, r.Question[0].Name)
} }
@ -147,21 +148,33 @@ var tests = []test.Case{
test.SOA("example.com. 5 IN SOA ns1.dns.example.com. hostmaster.example.com. 1499347823 7200 1800 86400 5"), test.SOA("example.com. 5 IN SOA ns1.dns.example.com. hostmaster.example.com. 1499347823 7200 1800 86400 5"),
}, },
}, },
{
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"),
},
},
{
Qname: "svc12.testns.example.com.", Qtype: dns.TypeA, Rcode: dns.RcodeSuccess,
Answer: []dns.RR{
test.CNAME("svc12.testns.example.com. 5 IN CNAME dummy.hostname"),
},
},
} }
type external struct{} type external struct{}
func (external) HasSynced() bool { return true } func (external) HasSynced() bool { return true }
func (external) Run() {} func (external) Run() {}
func (external) Stop() error { return nil } func (external) Stop() error { return nil }
func (external) EpIndexReverse(string) []*object.Endpoints { return nil } func (external) EpIndexReverse(string) []*object.Endpoints { return nil }
func (external) SvcIndexReverse(string) []*object.Service { return nil } func (external) SvcIndexReverse(string) []*object.Service { return nil }
func (external) Modified() int64 { return 0 } func (external) Modified() int64 { return 0 }
func (external) EpIndex(s string) []*object.Endpoints { return nil } func (external) EpIndex(s string) []*object.Endpoints { return nil }
func (external) EndpointsList() []*object.Endpoints { return nil } func (external) EndpointsList() []*object.Endpoints { return nil }
func (external) GetNodeByName(ctx context.Context, name string) (*api.Node, error) { return nil, nil } func (external) GetNodeByName(ctx context.Context, name string) (*api.Node, error) { return nil, nil }
func (external) SvcIndex(s string) []*object.Service { return svcIndexExternal[s] } func (external) SvcIndex(s string) []*object.Service { return svcIndexExternal[s] }
func (external) PodIndex(string) []*object.Pod { return nil } func (external) PodIndex(string) []*object.Pod { return nil }
func (external) GetNamespaceByName(name string) (*api.Namespace, error) { func (external) GetNamespaceByName(name string) (*api.Namespace, error) {
return &api.Namespace{ return &api.Namespace{
@ -192,6 +205,24 @@ var svcIndexExternal = map[string][]*object.Service{
Ports: []api.ServicePort{{Name: "http", Protocol: "tcp", Port: 80}}, Ports: []api.ServicePort{{Name: "http", Protocol: "tcp", Port: 80}},
}, },
}, },
"svc11.testns": {
{
Name: "svc11",
Namespace: "testns",
Type: api.ServiceTypeLoadBalancer,
ExternalIPs: []string{"1.2.3.4"},
Ports: []api.ServicePort{{Name: "http", Protocol: "tcp", Port: 80}},
},
},
"svc12.testns": {
{
Name: "svc12",
Namespace: "testns",
Type: api.ServiceTypeLoadBalancer,
ExternalIPs: []string{"dummy.hostname"},
Ports: []api.ServicePort{{Name: "http", Protocol: "tcp", Port: 80}},
},
},
} }
func (external) ServiceList() []*object.Service { func (external) ServiceList() []*object.Service {

View file

@ -1,6 +1,7 @@
package external package external
import ( import (
"context"
"math" "math"
"github.com/coredns/coredns/plugin/etcd/msg" "github.com/coredns/coredns/plugin/etcd/msg"
@ -9,7 +10,7 @@ import (
"github.com/miekg/dns" "github.com/miekg/dns"
) )
func (e *External) a(services []msg.Service, state request.Request) (records []dns.RR) { func (e *External) a(ctx context.Context, services []msg.Service, state request.Request) (records []dns.RR) {
dup := make(map[string]struct{}) dup := make(map[string]struct{})
for _, s := range services { for _, s := range services {
@ -18,7 +19,13 @@ func (e *External) a(services []msg.Service, state request.Request) (records []d
switch what { switch what {
case dns.TypeCNAME: case dns.TypeCNAME:
// can't happen 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)
}
}
case dns.TypeA: case dns.TypeA:
if _, ok := dup[s.Host]; !ok { if _, ok := dup[s.Host]; !ok {
@ -35,7 +42,7 @@ func (e *External) a(services []msg.Service, state request.Request) (records []d
return records return records
} }
func (e *External) aaaa(services []msg.Service, state request.Request) (records []dns.RR) { func (e *External) aaaa(ctx context.Context, services []msg.Service, state request.Request) (records []dns.RR) {
dup := make(map[string]struct{}) dup := make(map[string]struct{})
for _, s := range services { for _, s := range services {
@ -44,7 +51,13 @@ func (e *External) aaaa(services []msg.Service, state request.Request) (records
switch what { switch what {
case dns.TypeCNAME: case dns.TypeCNAME:
// can't happen 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)
}
}
case dns.TypeA: case dns.TypeA:
// nada // nada

View file

@ -5,6 +5,7 @@ import (
"github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/upstream"
"github.com/caddyserver/caddy" "github.com/caddyserver/caddy"
) )
@ -30,6 +31,8 @@ func setup(c *caddy.Controller) error {
return nil return nil
}) })
e.upstream = upstream.New()
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler { dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
e.Next = next e.Next = next
return e return e

View file

@ -62,7 +62,12 @@ func toService(skipCleanup bool, svc *api.Service) *Service {
li := copy(s.ExternalIPs, svc.Spec.ExternalIPs) li := copy(s.ExternalIPs, svc.Spec.ExternalIPs)
for i, lb := range svc.Status.LoadBalancer.Ingress { for i, lb := range svc.Status.LoadBalancer.Ingress {
s.ExternalIPs[li+i] = lb.IP if lb.IP != "" {
s.ExternalIPs[li+i] = lb.IP
continue
}
s.ExternalIPs[li+i] = lb.Hostname
} }
if !skipCleanup { if !skipCleanup {