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/etcd/msg"
"github.com/coredns/coredns/plugin/pkg/upstream"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
@ -39,6 +40,8 @@ type External struct {
apex string
ttl uint32
upstream *upstream.Upstream
externalFunc func(request.Request) ([]msg.Service, int)
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() {
case dns.TypeA:
m.Answer = e.a(svc, state)
m.Answer = e.a(ctx, svc, state)
case dns.TypeAAAA:
m.Answer = e.aaaa(svc, state)
m.Answer = e.aaaa(ctx, svc, state)
case dns.TypeSRV:
m.Answer, m.Extra = e.srv(svc, state)
default:

View file

@ -41,6 +41,7 @@ func TestExternal(t *testing.T) {
}
resp := w.Msg
if resp == nil {
t.Fatalf("Test %d, got nil message and no error for %q", i, r.Question[0].Name)
}
@ -147,6 +148,18 @@ var tests = []test.Case{
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{}
@ -192,6 +205,24 @@ var svcIndexExternal = map[string][]*object.Service{
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 {

View file

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

View file

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

View file

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