From 4e7f00649823ce1bf2affa0c59d62a8f1e97cd52 Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Mon, 6 Aug 2018 17:07:46 +0800 Subject: [PATCH] =?UTF-8?q?dns:=20=E6=94=AF=E6=8C=81=20SRV?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../root/etc/yunion/region-dns.conf.sample | 2 +- cmd/region-dns/main.go | 2 - pkg/dns/dns.go | 62 +++++++++++-------- pkg/dns/parse.go | 4 ++ 4 files changed, 41 insertions(+), 29 deletions(-) diff --git a/build/region-dns/root/etc/yunion/region-dns.conf.sample b/build/region-dns/root/etc/yunion/region-dns.conf.sample index 167be6e98e..5d18411b8b 100644 --- a/build/region-dns/root/etc/yunion/region-dns.conf.sample +++ b/build/region-dns/root/etc/yunion/region-dns.conf.sample @@ -1,8 +1,8 @@ .:53 { cache 30 - whoami yunion . { sql_connection mysql+pymysql://yunioncloud:AxuaId26ZhfPXWgr@10.168.222.183:3306/yunioncloud?charset=utf8 + dns_domain yunion.local kube_config /home/lzx/.kube/config-hq fallthrough . } diff --git a/cmd/region-dns/main.go b/cmd/region-dns/main.go index b5dc26adce..fb295a365f 100644 --- a/cmd/region-dns/main.go +++ b/cmd/region-dns/main.go @@ -15,7 +15,6 @@ import ( _ "github.com/coredns/coredns/plugin/proxy" _ "github.com/coredns/coredns/plugin/reload" _ "github.com/coredns/coredns/plugin/trace" - _ "github.com/coredns/coredns/plugin/whoami" _ "github.com/mholt/caddy/startupshutdown" "github.com/coredns/coredns/core/dnsserver" @@ -37,7 +36,6 @@ var directives = []string{ "yunion", "forward", "proxy", - "whoami", "startup", "shutdown", } diff --git a/pkg/dns/dns.go b/pkg/dns/dns.go index fa39366939..9c1b135932 100644 --- a/pkg/dns/dns.go +++ b/pkg/dns/dns.go @@ -5,6 +5,8 @@ import ( "database/sql" "errors" "fmt" + "strconv" + "strings" "github.com/coredns/coredns/plugin" "github.com/coredns/coredns/plugin/etcd/msg" @@ -115,12 +117,7 @@ func (r *SRegionDNS) ServeDNS(ctx context.Context, w dns.ResponseWriter, rmsg *d opt := plugin.Options{} state := request.Request{W: w, Req: rmsg, Context: ctx} - //isMyDomain := true zone := plugin.Zones(r.Zones).Matches(state.Name()) - //if zone == "" { - ////isMyDomain = false - //return plugin.NextOrFailure(r.Name(), r.Next, ctx, w, rmsg) - //} switch state.QType() { case dns.TypeA: @@ -221,21 +218,9 @@ func (r *SRegionDNS) Services(state request.Request, exact bool, opt plugin.Opti return []msg.Service{svc}, nil } - s, e := r.Records(state, false) - ylog.Debugf("Get records: %#v, error: %v", s, e) - - // SRV is not yet implemented, so remove those records. - if state.QType() != dns.TypeSRV { - return s, e - } - - internal := []msg.Service{} - for _, svc := range s { - if t, _ := svc.HostType(); t != dns.TypeCNAME { - internal = append(internal, svc) - } - } - return internal, e + services, err = r.Records(state, false) + ylog.Debugf("Get records: %#v, error: %v", services, err) + return } // Lookup implements the ServiceBackend interface @@ -322,8 +307,28 @@ func (r *SRegionDNS) queryLocalDnsRecords(req *recordRequest) (recs []msg.Servic err = errNoItems return } + for _, ip := range ips { - s := msg.Service{Host: ip.Addr, TTL: 5 * 60} + var s = msg.Service{} + var ttl uint32 = uint32(ip.Ttl) + if ttl == 0 { + ttl = defaultTTL + } + if req.IsSRV() { + parts := strings.SplitN(ip.Addr, ":", 2) + if len(parts) != 2 { + err = fmt.Errorf("Invalid SRV records: %q", ip.Addr) + return + } + port, e := strconv.Atoi(parts[1]) + if e != nil { + err = e + return + } + s = msg.Service{Host: parts[0], Port: port, TTL: ttl} + } else { + s = msg.Service{Host: ip.Addr, TTL: ttl} + } recs = append(recs, s) } return @@ -340,6 +345,15 @@ func (r *SRegionDNS) IsK8sClientReady() bool { return r.K8sClient != nil } +func (r *SRegionDNS) isMyDomain(req *recordRequest) bool { + zones := []string{fmt.Sprintf("%s.", r.PrimaryZone)} + zone := plugin.Zones(zones).Matches(req.state.Name()) + if zone != "" { + return true + } + return false +} + func (r *SRegionDNS) findRecords(req *recordRequest) (recs []msg.Service, err error) { // 1. try local dns records table recs, err = r.queryLocalDnsRecords(req) @@ -347,11 +361,7 @@ func (r *SRegionDNS) findRecords(req *recordRequest) (recs []msg.Service, err er return } - isMyDomain := false - zone := plugin.Zones(r.Zones).Matches(req.state.Name()) - if zone != "" { - isMyDomain = true - } + isMyDomain := r.isMyDomain(req) isCloudIp := r.IsCloudNetworkIp(req) diff --git a/pkg/dns/parse.go b/pkg/dns/parse.go index 92a7f586d5..5b9b59b32c 100644 --- a/pkg/dns/parse.go +++ b/pkg/dns/parse.go @@ -53,6 +53,10 @@ func (r recordRequest) Type() string { return DNSTypeMap[r.state.QType()] } +func (r recordRequest) IsSRV() bool { + return r.Type() == DNSTypeMap[dns.TypeSRV] +} + func (r recordRequest) SrcIP4() string { ip := r.state.IP() return ip