mirror of
https://github.com/coredns/coredns.git
synced 2025-11-01 02:33:14 -04:00
plugin/federation: Add upstream option to federation (#2177)
* add upstream * add upstream * debug ci * debug ci * set context * update readme * update readme * remove empty if
This commit is contained in:
committed by
Francois Tur
parent
1847ef6bd3
commit
6beeabc47c
@@ -17,11 +17,16 @@ Enabling *federation* without also having *kubernetes* is a noop.
|
|||||||
~~~
|
~~~
|
||||||
federation [ZONES...] {
|
federation [ZONES...] {
|
||||||
NAME DOMAIN
|
NAME DOMAIN
|
||||||
|
upstream [ADDRESS...]
|
||||||
}
|
}
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
* Each **NAME** and **DOMAIN** defines federation membership. One entry for each. A duplicate
|
* Each **NAME** and **DOMAIN** defines federation membership. One entry for each. A duplicate
|
||||||
**NAME** will silently overwrite any previous value.
|
**NAME** will silently overwrite any previous value.
|
||||||
|
* `upstream` [**ADDRESS**...] defines the upstream resolvers used for resolving the `CNAME` target
|
||||||
|
produced by this plugin. If no **ADDRESS** is given, CoreDNS
|
||||||
|
will resolve External Services against itself. **ADDRESS** can be an IP, an IP:port, or a path
|
||||||
|
to a file structured like resolv.conf.
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
@@ -33,6 +38,7 @@ Here we handle all service requests in the `prod` and `stage` federations.
|
|||||||
federation cluster.local {
|
federation cluster.local {
|
||||||
prod prod.feddomain.com
|
prod prod.feddomain.com
|
||||||
staging staging.feddomain.com
|
staging staging.feddomain.com
|
||||||
|
upstream
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
~~~
|
~~~
|
||||||
@@ -45,6 +51,7 @@ cluster.local {
|
|||||||
federation {
|
federation {
|
||||||
prod prod.feddomain.com
|
prod prod.feddomain.com
|
||||||
staging staging.feddomain.com
|
staging staging.feddomain.com
|
||||||
|
upstream
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
~~~
|
~~~
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import (
|
|||||||
"github.com/coredns/coredns/plugin/etcd/msg"
|
"github.com/coredns/coredns/plugin/etcd/msg"
|
||||||
"github.com/coredns/coredns/plugin/pkg/dnsutil"
|
"github.com/coredns/coredns/plugin/pkg/dnsutil"
|
||||||
"github.com/coredns/coredns/plugin/pkg/nonwriter"
|
"github.com/coredns/coredns/plugin/pkg/nonwriter"
|
||||||
|
"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"
|
||||||
@@ -27,8 +28,9 @@ import (
|
|||||||
|
|
||||||
// Federation contains the name to zone mapping used for federation in kubernetes.
|
// Federation contains the name to zone mapping used for federation in kubernetes.
|
||||||
type Federation struct {
|
type Federation struct {
|
||||||
f map[string]string
|
f map[string]string
|
||||||
zones []string
|
zones []string
|
||||||
|
Upstream *upstream.Upstream
|
||||||
|
|
||||||
Next plugin.Handler
|
Next plugin.Handler
|
||||||
Federations Func
|
Federations Func
|
||||||
@@ -49,7 +51,8 @@ func (f *Federation) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.
|
|||||||
return plugin.NextOrFailure(f.Name(), f.Next, ctx, w, r)
|
return plugin.NextOrFailure(f.Name(), f.Next, ctx, w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
state := request.Request{W: w, Req: r}
|
state := request.Request{W: w, Req: r, Context: ctx}
|
||||||
|
|
||||||
zone := plugin.Zones(f.zones).Matches(state.Name())
|
zone := plugin.Zones(f.zones).Matches(state.Name())
|
||||||
if zone == "" {
|
if zone == "" {
|
||||||
return plugin.NextOrFailure(f.Name(), f.Next, ctx, w, r)
|
return plugin.NextOrFailure(f.Name(), f.Next, ctx, w, r)
|
||||||
@@ -105,6 +108,13 @@ func (f *Federation) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.
|
|||||||
|
|
||||||
m.Answer = []dns.RR{service.NewCNAME(state.QName(), service.Host)}
|
m.Answer = []dns.RR{service.NewCNAME(state.QName(), service.Host)}
|
||||||
|
|
||||||
|
if f.Upstream != nil {
|
||||||
|
aRecord, err := f.Upstream.Lookup(state, service.Host, state.QType())
|
||||||
|
if err == nil && aRecord != nil && len(aRecord.Answer) > 0 {
|
||||||
|
m.Answer = append(m.Answer, aRecord.Answer...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
w.WriteMsg(m)
|
w.WriteMsg(m)
|
||||||
return dns.RcodeSuccess, nil
|
return dns.RcodeSuccess, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,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/kubernetes"
|
"github.com/coredns/coredns/plugin/kubernetes"
|
||||||
|
"github.com/coredns/coredns/plugin/pkg/upstream"
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
|
|
||||||
"github.com/mholt/caddy"
|
"github.com/mholt/caddy"
|
||||||
@@ -62,6 +63,13 @@ func federationParse(c *caddy.Controller) (*Federation, error) {
|
|||||||
for c.NextBlock() {
|
for c.NextBlock() {
|
||||||
x := c.Val()
|
x := c.Val()
|
||||||
switch x {
|
switch x {
|
||||||
|
case "upstream":
|
||||||
|
args := c.RemainingArgs()
|
||||||
|
u, err := upstream.New(args)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
fed.Upstream = &u
|
||||||
default:
|
default:
|
||||||
args := c.RemainingArgs()
|
args := c.RemainingArgs()
|
||||||
if x := len(args); x != 1 {
|
if x := len(args); x != 1 {
|
||||||
|
|||||||
Reference in New Issue
Block a user