parent
c3705ec68c
commit
b46b9880bd
20 changed files with 597 additions and 642 deletions
|
@ -11,7 +11,7 @@ to deploy CoreDNS in Kubernetes](https://github.com/coredns/deployment/tree/mast
|
|||
## Syntax
|
||||
|
||||
```
|
||||
kubernetes ZONE [ZONE...] [{
|
||||
kubernetes ZONE [ZONE...] [
|
||||
resyncperiod DURATION
|
||||
endpoint URL
|
||||
tls CERT KEY CACERT]
|
||||
|
@ -20,9 +20,8 @@ kubernetes ZONE [ZONE...] [{
|
|||
pods POD-MODE]
|
||||
upstream ADDRESS [ADDRESS...]
|
||||
federation NAME DOMAIN
|
||||
autopath [NDOTS [RESPONSE [RESOLV-CONF]]
|
||||
fallthrough
|
||||
}]
|
||||
}
|
||||
```
|
||||
|
||||
* `resyncperiod` **DURATION**
|
||||
|
@ -135,62 +134,6 @@ specified).
|
|||
}
|
||||
```
|
||||
|
||||
* `autopath` **[NDOTS [RESPONSE [RESOLV-CONF]]**
|
||||
|
||||
Enables server side search path lookups for pods. When enabled, the kubernetes middleware will identify search path queries from pods and perform the remaining lookups in the path on the pod's behalf. The search path used mimics the resolv.conf search path deployed to pods by the "cluster-first" dns-policy. E.g.
|
||||
|
||||
```
|
||||
search ns1.svc.cluster.local svc.cluster.local cluster.local foo.com
|
||||
```
|
||||
|
||||
If no domains in the path produce an answer, a lookup on the bare question will be attempted.
|
||||
|
||||
A successful response will contain a question section with the original question, and an answer section containing the record for the question that actually had an answer. This means that the question and answer will not match. To avoid potential client confusion, a dynamically generated CNAME entry is added to join the two. For example:
|
||||
|
||||
```
|
||||
# host -v -t a google.com
|
||||
Trying "google.com.default.svc.cluster.local"
|
||||
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 50957
|
||||
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0
|
||||
|
||||
;; QUESTION SECTION:
|
||||
;google.com.default.svc.cluster.local. IN A
|
||||
|
||||
;; ANSWER SECTION:
|
||||
google.com.default.svc.cluster.local. 175 IN CNAME google.com.
|
||||
google.com. 175 IN A 216.58.194.206
|
||||
```
|
||||
|
||||
**Fully Qualified Queries:** There is a known limitation of the autopath feature involving fully qualified queries. When the kubernetes middleware receives a query from a client, it cannot tell the difference between a query that was fully qualified by the user, and one that was expanded by the first search path on the client side.
|
||||
|
||||
This means that the kubernetes middleware with autopath enabled will perform a server-side path search for the query `google.com.default.svc.cluster.local.` as if the client had queried just `google.com`. In other words, a query for `google.com.default.svc.cluster.local.` will produce the IP address for `google.com` as seen below.
|
||||
|
||||
```
|
||||
# host -t a google.com
|
||||
google.com has address 216.58.194.206
|
||||
google.com.default.svc.cluster.local is an alias for google.com.
|
||||
|
||||
# host -t a google.com.default.svc.cluster.local.
|
||||
google.com has address 216.58.194.206
|
||||
google.com.default.svc.cluster.local is an alias for google.com.
|
||||
```
|
||||
|
||||
**NDOTS** (default: `0`) This provides an adjustable threshold to prevent server side lookups from triggering. If the number of dots before the first search domain is less than this number, then the search path will not executed on the server side. When autopath is enabled with default settings, the search path is always conducted when the query is in the first search domain `<pod-namespace>.svc.<zone>.`.
|
||||
|
||||
**RESPONSE** (default: `NOERROR`) This option causes the kubernetes middleware to return the given response instead of NXDOMAIN when the all searches in the path produce no results. Valid values: `NXDOMAIN`, `SERVFAIL` or `NOERROR`. Setting this to `SERVFAIL` or `NOERROR` should prevent the client from fruitlessly continuing the client side searches in the path after the server already checked them.
|
||||
|
||||
**RESOLV-CONF** (default: `/etc/resolv.conf`) If specified, the kubernetes middleware uses this file to get the host's search domains. The kubernetes middleware performs a lookup on these domains if the in-cluster search domains in the path fail to produce an answer. If not specified, the values will be read from the local resolv.conf file (i.e the resolv.conf file in the pod containing CoreDNS). In practice, this option should only need to be used if running CoreDNS outside of the cluster and the search path in /etc/resolv.conf does not match the cluster's "default" dns-policiy.
|
||||
|
||||
Enabling autopath requires more memory, since it needs to maintain a watch on all pods. If autopath and `pods verified` mode are both enabled, they will share the same watch. Enabling both options should have an equivalent memory impact of just one.
|
||||
|
||||
Example:
|
||||
|
||||
```
|
||||
kubernetes cluster.local. {
|
||||
autopath 0 NXDOMAIN /etc/resolv.conf
|
||||
}
|
||||
```
|
||||
|
||||
* `fallthrough`
|
||||
|
||||
If a query for a record in the cluster zone results in NXDOMAIN, normally that is what the response will be. However, if you specify this option, the query will instead be passed on down the middleware chain, which can include another middleware to handle the query.
|
||||
|
@ -204,13 +147,12 @@ specified).
|
|||
|
||||
**Example 2:** Handle all queries in the `cluster.local` zone. Connect to Kubernetes in-cluster.
|
||||
Handle all `PTR` requests for `10.0.0.0/16` . Verify the existence of pods when answering pod
|
||||
requests. Resolve upstream records against `10.102.3.10`. Enable the autopath feature.
|
||||
requests. Resolve upstream records against `10.102.3.10`.
|
||||
|
||||
10.0.0.0/16 cluster.local {
|
||||
kubernetes {
|
||||
pods verified
|
||||
upstream 10.102.3.10:53
|
||||
autopath
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -232,14 +174,13 @@ specified).
|
|||
}
|
||||
|
||||
**Out-Of-Cluster Example:** Handle all queries in the `cluster.local` zone. Connect to Kubernetes from outside the cluster.
|
||||
Verify the existence of pods when answering pod requests. Resolve upstream records against `10.102.3.10`. Enable the autopath feature, using the `cluster.conf` file instead of `/etc/resolv.conf`.
|
||||
Verify the existence of pods when answering pod requests. Resolve upstream records against `10.102.3.10`.
|
||||
|
||||
kubernetes cluster.local {
|
||||
endpoint https://k8s-endpoint:8443
|
||||
tls cert key cacert
|
||||
pods verified
|
||||
upstream 10.102.3.10:53
|
||||
autopath 0 NOERROR cluster.conf
|
||||
}
|
||||
|
||||
|
||||
|
|
18
middleware/kubernetes/autopath.go
Normal file
18
middleware/kubernetes/autopath.go
Normal file
|
@ -0,0 +1,18 @@
|
|||
package kubernetes
|
||||
|
||||
import "k8s.io/client-go/1.5/pkg/api"
|
||||
|
||||
// TODO(miek): rename and put in autopath.go file. This will be for the
|
||||
// external middleware autopath to use. Mostly to get the namespace:
|
||||
//name, path, ok := autopath.SplitSearch(zone, state.QName(), p.Namespace)
|
||||
func (k *Kubernetes) findPodWithIP(ip string) (p *api.Pod) {
|
||||
objList := k.APIConn.PodIndex(ip)
|
||||
for _, o := range objList {
|
||||
p, ok := o.(*api.Pod)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return p
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -1,75 +0,0 @@
|
|||
package autopath
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
|
||||
// Writer implements a ResponseWriter that also does the following:
|
||||
// * reverts question section of a packet to its original state.
|
||||
// This is done to avoid the 'Question section mismatch:' error in client.
|
||||
// * Defers write to the client if the response code is NXDOMAIN. This is needed
|
||||
// to enable further search path probing if a search was not successful.
|
||||
// * Allow forced write to client regardless of response code. This is needed to
|
||||
// write the packet to the client if the final search in the path fails to
|
||||
// produce results.
|
||||
// * Overwrites response code with Writer.Rcode if the response code
|
||||
// is NXDOMAIN (NameError). This is needed to support the AutoPath.OnNXDOMAIN
|
||||
// function, which returns a NOERROR to client instead of NXDOMAIN if the final
|
||||
// search in the path fails to produce results.
|
||||
type Writer struct {
|
||||
dns.ResponseWriter
|
||||
original dns.Question
|
||||
Rcode int
|
||||
}
|
||||
|
||||
// AutoPath enables server side search path lookups for pods.
|
||||
type AutoPath struct {
|
||||
NDots int
|
||||
ResolvConfFile string
|
||||
HostSearchPath []string
|
||||
OnNXDOMAIN int
|
||||
}
|
||||
|
||||
// NewWriter returns a pointer to a new Writer
|
||||
func NewWriter(w dns.ResponseWriter, r *dns.Msg) *Writer {
|
||||
return &Writer{
|
||||
ResponseWriter: w,
|
||||
original: r.Question[0],
|
||||
Rcode: dns.RcodeSuccess,
|
||||
}
|
||||
}
|
||||
|
||||
// WriteMsg writes to client, unless response will be NXDOMAIN.
|
||||
func (apw *Writer) WriteMsg(res *dns.Msg) error {
|
||||
if res.Rcode == dns.RcodeNameError {
|
||||
res.Rcode = apw.Rcode
|
||||
}
|
||||
|
||||
for _, a := range res.Answer {
|
||||
if apw.original.Name == a.Header().Name {
|
||||
continue
|
||||
}
|
||||
res.Answer = append(res.Answer, nil)
|
||||
copy(res.Answer[1:], res.Answer)
|
||||
res.Answer[0] = CNAME(apw.original.Name, a.Header().Name, a.Header().Ttl)
|
||||
}
|
||||
res.Question[0] = apw.original
|
||||
|
||||
return apw.ResponseWriter.WriteMsg(res)
|
||||
}
|
||||
|
||||
// Write is a wrapper that records the size of the message that gets written.
|
||||
func (apw *Writer) Write(buf []byte) (int, error) {
|
||||
n, err := apw.ResponseWriter.Write(buf)
|
||||
return n, err
|
||||
}
|
||||
|
||||
func SplitSearch(zone, question, namespace string) (name, search string, ok bool) {
|
||||
search = strings.Join([]string{namespace, "svc", zone}, ".")
|
||||
if dns.IsSubDomain(search, question) {
|
||||
return question[:len(question)-len(search)-1], search, true
|
||||
}
|
||||
return "", "", false
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
package autopath
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSplitSearchPath(t *testing.T) {
|
||||
type testCase struct {
|
||||
question string
|
||||
namespace string
|
||||
expectedName string
|
||||
expectedSearch string
|
||||
expectedOk bool
|
||||
}
|
||||
tests := []testCase{
|
||||
{question: "test.blah.com", namespace: "ns1", expectedName: "", expectedSearch: "", expectedOk: false},
|
||||
{question: "foo.com.ns2.svc.interwebs.nets", namespace: "ns1", expectedName: "", expectedSearch: "", expectedOk: false},
|
||||
{question: "foo.com.svc.interwebs.nets", namespace: "ns1", expectedName: "", expectedSearch: "", expectedOk: false},
|
||||
{question: "foo.com.ns1.svc.interwebs.nets", namespace: "ns1", expectedName: "foo.com", expectedSearch: "ns1.svc.interwebs.nets", expectedOk: true},
|
||||
}
|
||||
zone := "interwebs.nets"
|
||||
for _, c := range tests {
|
||||
name, search, ok := SplitSearch(zone, c.question, c.namespace)
|
||||
if c.expectedName != name || c.expectedSearch != search || c.expectedOk != ok {
|
||||
t.Errorf("Case %v: Expected name'%v', search:'%v', ok:'%v'. Got name:'%v', search:'%v', ok:'%v'.", c.question, c.expectedName, c.expectedSearch, c.expectedOk, name, search, ok)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
package autopath
|
||||
|
||||
import "github.com/miekg/dns"
|
||||
|
||||
// CNAME returns a new CNAME formed from name target and ttl.
|
||||
func CNAME(name string, target string, ttl uint32) *dns.CNAME {
|
||||
return &dns.CNAME{
|
||||
Hdr: dns.RR_Header{Name: name, Rrtype: dns.TypeCNAME, Class: dns.ClassINET, Ttl: ttl},
|
||||
Target: dns.Fqdn(target)}
|
||||
}
|
|
@ -1,176 +0,0 @@
|
|||
package kubernetes
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/coredns/coredns/middleware/kubernetes/autopath"
|
||||
"github.com/coredns/coredns/middleware/test"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
var autopathCases = map[string](*test.Case){
|
||||
"A Autopath Service (Second Search)": {
|
||||
Qname: "svc1.testns.podns.svc.cluster.local.", Qtype: dns.TypeA,
|
||||
Rcode: dns.RcodeSuccess,
|
||||
Answer: []dns.RR{
|
||||
test.CNAME("svc1.testns.podns.svc.cluster.local. 0 IN CNAME svc1.testns.svc.cluster.local."),
|
||||
test.A("svc1.testns.svc.cluster.local. 0 IN A 10.0.0.1"),
|
||||
},
|
||||
},
|
||||
"A Autopath Service (Third Search)": {
|
||||
Qname: "svc1.testns.svc.podns.svc.cluster.local.", Qtype: dns.TypeA,
|
||||
Rcode: dns.RcodeSuccess,
|
||||
Answer: []dns.RR{
|
||||
test.CNAME("svc1.testns.svc.podns.svc.cluster.local. 0 IN CNAME svc1.testns.svc.cluster.local."),
|
||||
test.A("svc1.testns.svc.cluster.local. 0 IN A 10.0.0.1"),
|
||||
},
|
||||
},
|
||||
"A Autopath Next Middleware (Host Domain Search)": {
|
||||
Qname: "test1.podns.svc.cluster.local.", Qtype: dns.TypeA,
|
||||
Rcode: dns.RcodeSuccess,
|
||||
Answer: []dns.RR{
|
||||
test.CNAME("test1.podns.svc.cluster.local. 0 IN CNAME test1.hostdom.test."),
|
||||
test.A("test1.hostdom.test. 0 IN A 11.22.33.44"),
|
||||
},
|
||||
},
|
||||
"A Autopath Service (Bare Search)": {
|
||||
Qname: "svc1.testns.svc.cluster.local.podns.svc.cluster.local.", Qtype: dns.TypeA,
|
||||
Rcode: dns.RcodeSuccess,
|
||||
Answer: []dns.RR{
|
||||
test.CNAME("svc1.testns.svc.cluster.local.podns.svc.cluster.local. 0 IN CNAME svc1.testns.svc.cluster.local."),
|
||||
test.A("svc1.testns.svc.cluster.local. 0 IN A 10.0.0.1"),
|
||||
},
|
||||
},
|
||||
"A Autopath Next Middleware (Bare Search)": {
|
||||
Qname: "test2.interwebs.podns.svc.cluster.local.", Qtype: dns.TypeA,
|
||||
Rcode: dns.RcodeSuccess,
|
||||
Answer: []dns.RR{
|
||||
test.CNAME("test2.interwebs.podns.svc.cluster.local. 0 IN CNAME test2.interwebs."),
|
||||
test.A("test2.interwebs. 0 IN A 55.66.77.88"),
|
||||
},
|
||||
},
|
||||
"AAAA Autopath Next Middleware (Bare Search)": {
|
||||
Qname: "test2.interwebs.podns.svc.cluster.local.", Qtype: dns.TypeAAAA,
|
||||
Rcode: dns.RcodeSuccess,
|
||||
Answer: []dns.RR{
|
||||
test.CNAME("test2.interwebs.podns.svc.cluster.local. 0 IN CNAME test2.interwebs."),
|
||||
test.AAAA("test2.interwebs. 0 IN AAAA 5555:6666:7777::8888"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
var autopathBareSearch = map[string](*test.Case){
|
||||
"A Autopath Next Middleware (Bare Search) Non-existing OnNXDOMAIN default": {
|
||||
Qname: "nothere.interwebs.podns.svc.cluster.local.", Qtype: dns.TypeA,
|
||||
Rcode: dns.RcodeSuccess,
|
||||
Answer: []dns.RR{},
|
||||
},
|
||||
}
|
||||
|
||||
var autopathBareSearchExpectNameErr = map[string](*test.Case){
|
||||
"A Autopath Next Middleware (Bare Search) Non-existing OnNXDOMAIN disabled": {
|
||||
Qname: "nothere.interwebs.podns.svc.cluster.local.", Qtype: dns.TypeA,
|
||||
Rcode: dns.RcodeNameError,
|
||||
Answer: []dns.RR{},
|
||||
},
|
||||
}
|
||||
|
||||
var autopath2NDotsCases = map[string](*test.Case){
|
||||
"A Service (0 Dots)": {
|
||||
Qname: "foo.podns.svc.cluster.local.", Qtype: dns.TypeA,
|
||||
Rcode: dns.RcodeNameError,
|
||||
Answer: []dns.RR{},
|
||||
Ns: []dns.RR{
|
||||
test.SOA("cluster.local. 300 IN SOA ns.dns.cluster.local. hostmaster.cluster.local. 1499347823 7200 1800 86400 60"),
|
||||
},
|
||||
},
|
||||
"A Service (1 Dots)": {
|
||||
Qname: "foo.foo.podns.svc.cluster.local.", Qtype: dns.TypeA,
|
||||
Rcode: dns.RcodeNameError,
|
||||
Answer: []dns.RR{},
|
||||
Ns: []dns.RR{
|
||||
test.SOA("cluster.local. 300 IN SOA ns.dns.cluster.local. hostmaster.cluster.local. 1499347823 7200 1800 86400 60"),
|
||||
},
|
||||
},
|
||||
"A Service (2 Dots)": {
|
||||
Qname: "foo.foo.foo.podns.svc.cluster.local.", Qtype: dns.TypeA,
|
||||
Rcode: dns.RcodeSuccess,
|
||||
Answer: []dns.RR{
|
||||
test.A("foo.foo.foo.hostdom.test. 0 IN A 11.22.33.44"),
|
||||
test.CNAME("foo.foo.foo.podns.svc.cluster.local. 0 IN CNAME foo.foo.foo.hostdom.test."),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// Disabled because broken.
|
||||
func testServeDNSAutoPath(t *testing.T) {
|
||||
|
||||
k := Kubernetes{}
|
||||
k.Zones = []string{"cluster.local."}
|
||||
_, cidr, _ := net.ParseCIDR("10.0.0.0/8")
|
||||
|
||||
k.ReverseCidrs = []net.IPNet{*cidr}
|
||||
k.Federations = []Federation{{name: "fed", zone: "federal.test."}}
|
||||
k.APIConn = &APIConnServeTest{}
|
||||
k.autoPath = new(autopath.AutoPath)
|
||||
k.autoPath.HostSearchPath = []string{"hostdom.test"}
|
||||
k.interfaceAddrsFunc = localPodIP
|
||||
k.Next = nextHandler(nextMap)
|
||||
|
||||
ctx := context.TODO()
|
||||
runServeDNSTests(ctx, t, autopathCases, k)
|
||||
runServeDNSTests(ctx, t, autopathBareSearch, k)
|
||||
|
||||
// Set ndots to 2 for the ndots test cases
|
||||
k.autoPath.NDots = 2
|
||||
runServeDNSTests(ctx, t, autopath2NDotsCases, k)
|
||||
k.autoPath.NDots = defautNdots
|
||||
// Disable the NXDOMAIN override (enabled by default)
|
||||
k.autoPath.OnNXDOMAIN = dns.RcodeNameError
|
||||
runServeDNSTests(ctx, t, autopathCases, k)
|
||||
runServeDNSTests(ctx, t, autopathBareSearchExpectNameErr, k)
|
||||
}
|
||||
|
||||
var nextMap = map[dns.Question]dns.Msg{
|
||||
{Name: "test1.hostdom.test.", Qtype: dns.TypeA, Qclass: dns.ClassINET}: {
|
||||
Answer: []dns.RR{test.A("test1.hostdom.test. 0 IN A 11.22.33.44")},
|
||||
},
|
||||
{Name: "test2.interwebs.", Qtype: dns.TypeA, Qclass: dns.ClassINET}: {
|
||||
Answer: []dns.RR{test.A("test2.interwebs. 0 IN A 55.66.77.88")},
|
||||
},
|
||||
{Name: "test2.interwebs.", Qtype: dns.TypeAAAA, Qclass: dns.ClassINET}: {
|
||||
Answer: []dns.RR{test.AAAA("test2.interwebs. 0 IN AAAA 5555:6666:7777::8888")},
|
||||
},
|
||||
{Name: "foo.hostdom.test.", Qtype: dns.TypeA, Qclass: dns.ClassINET}: {
|
||||
Answer: []dns.RR{test.A("foo.hostdom.test. 0 IN A 11.22.33.44")},
|
||||
},
|
||||
{Name: "foo.foo.hostdom.test.", Qtype: dns.TypeA, Qclass: dns.ClassINET}: {
|
||||
Answer: []dns.RR{test.A("foo.foo.hostdom.test. 0 IN A 11.22.33.44")},
|
||||
},
|
||||
{Name: "foo.foo.foo.hostdom.test.", Qtype: dns.TypeA, Qclass: dns.ClassINET}: {
|
||||
Answer: []dns.RR{test.A("foo.foo.foo.hostdom.test. 0 IN A 11.22.33.44")},
|
||||
},
|
||||
}
|
||||
|
||||
// nextHandler returns a Handler that returns an answer for the question in the
|
||||
// request per the question->answer map qMap.
|
||||
func nextHandler(mm map[dns.Question]dns.Msg) test.Handler {
|
||||
return test.HandlerFunc(func(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
|
||||
m := new(dns.Msg)
|
||||
m.SetReply(r)
|
||||
|
||||
msg, ok := mm[r.Question[0]]
|
||||
if !ok {
|
||||
r.Rcode = dns.RcodeNameError
|
||||
w.WriteMsg(m)
|
||||
return r.Rcode, nil
|
||||
}
|
||||
r.Rcode = dns.RcodeSuccess
|
||||
m.Answer = append(m.Answer, msg.Answer...)
|
||||
w.WriteMsg(m)
|
||||
return r.Rcode, nil
|
||||
})
|
||||
}
|
|
@ -2,10 +2,8 @@ package kubernetes
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/coredns/coredns/middleware"
|
||||
"github.com/coredns/coredns/middleware/kubernetes/autopath"
|
||||
"github.com/coredns/coredns/middleware/pkg/dnsutil"
|
||||
"github.com/coredns/coredns/request"
|
||||
|
||||
|
@ -40,70 +38,9 @@ func (k Kubernetes) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.M
|
|||
zone = state.Name()
|
||||
}
|
||||
|
||||
// TODO(miek): place contents of route-request back here.
|
||||
records, extra, _, err := k.routeRequest(zone, state)
|
||||
|
||||
// Check for Autopath search eligibility
|
||||
if (k.autoPath != nil) && k.IsNameError(err) && (state.QType() == dns.TypeA || state.QType() == dns.TypeAAAA) {
|
||||
p := k.findPodWithIP(state.IP())
|
||||
|
||||
for p != nil {
|
||||
name, path, ok := autopath.SplitSearch(zone, state.QName(), p.Namespace)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if (dns.CountLabel(name) - 1) < k.autoPath.NDots {
|
||||
break
|
||||
}
|
||||
origQName := state.QName()
|
||||
// Search "svc.cluster.local." and "cluster.local."
|
||||
for i := 0; i < 2; i++ {
|
||||
path = strings.Join(dns.SplitDomainName(path)[1:], ".")
|
||||
newstate := state.NewWithQuestion(strings.Join([]string{name, path}, "."), state.QType())
|
||||
records, extra, _, err = k.routeRequest(zone, newstate)
|
||||
if !k.IsNameError(err) && len(records) > 0 {
|
||||
records = append(records, nil)
|
||||
copy(records[1:], records)
|
||||
records[0] = autopath.CNAME(origQName, records[0].Header().Name, records[0].Header().Ttl)
|
||||
break
|
||||
}
|
||||
}
|
||||
if !k.IsNameError(err) {
|
||||
break
|
||||
}
|
||||
// Try host search path (if set) in the next middleware
|
||||
apw := autopath.NewWriter(w, r)
|
||||
for _, hostsearch := range k.autoPath.HostSearchPath {
|
||||
newstate := state.NewWithQuestion(strings.Join([]string{name, hostsearch}, "."), state.QType())
|
||||
rcode, nextErr := middleware.NextOrFailure(k.Name(), k.Next, ctx, apw, newstate.Req)
|
||||
|
||||
if middleware.ClientWrite(rcode) || rcode == dns.RcodeNameError {
|
||||
return rcode, nextErr
|
||||
}
|
||||
}
|
||||
// Search . in this middleware
|
||||
newstate := state.NewWithQuestion(strings.Join([]string{name, "."}, ""), state.QType())
|
||||
records, extra, _, err = k.routeRequest(zone, newstate)
|
||||
if !k.IsNameError(err) && len(records) > 0 {
|
||||
records = append(records, nil)
|
||||
copy(records[1:], records)
|
||||
records[0] = autopath.CNAME(origQName, records[0].Header().Name, records[0].Header().Ttl)
|
||||
break
|
||||
}
|
||||
// Search . in the next middleware
|
||||
apw.Rcode = k.autoPath.OnNXDOMAIN
|
||||
newstate = state.NewWithQuestion(strings.Join([]string{name, "."}, ""), state.QType())
|
||||
r = newstate.Req
|
||||
rcode, nextErr := middleware.NextOrFailure(k.Name(), k.Next, ctx, apw, r)
|
||||
if !(middleware.ClientWrite(rcode) || rcode == dns.RcodeNameError) && nextErr == nil {
|
||||
r = dnsutil.Dedup(r)
|
||||
state.SizeAndDo(r)
|
||||
r, _ = state.Scrub(r)
|
||||
apw.WriteMsg(r)
|
||||
}
|
||||
return rcode, nextErr
|
||||
}
|
||||
}
|
||||
|
||||
if k.IsNameError(err) {
|
||||
if k.Fallthrough {
|
||||
return middleware.NextOrFailure(k.Name(), k.Next, ctx, w, r)
|
||||
|
|
|
@ -11,7 +11,6 @@ import (
|
|||
|
||||
"github.com/coredns/coredns/middleware"
|
||||
"github.com/coredns/coredns/middleware/etcd/msg"
|
||||
"github.com/coredns/coredns/middleware/kubernetes/autopath"
|
||||
"github.com/coredns/coredns/middleware/pkg/dnsutil"
|
||||
dnsstrings "github.com/coredns/coredns/middleware/pkg/strings"
|
||||
"github.com/coredns/coredns/middleware/proxy"
|
||||
|
@ -47,7 +46,6 @@ type Kubernetes struct {
|
|||
ReverseCidrs []net.IPNet
|
||||
Fallthrough bool
|
||||
|
||||
autoPath *autopath.AutoPath
|
||||
interfaceAddrsFunc func() net.IP
|
||||
}
|
||||
|
||||
|
@ -252,7 +250,7 @@ func (k *Kubernetes) InitKubeCache() (err error) {
|
|||
}
|
||||
|
||||
opts := dnsControlOpts{
|
||||
initPodCache: (k.PodMode == PodModeVerified || (k.autoPath != nil)),
|
||||
initPodCache: k.PodMode == PodModeVerified,
|
||||
}
|
||||
k.APIConn = newdnsController(kubeClient, k.ResyncPeriod, k.Selector, opts)
|
||||
|
||||
|
@ -460,21 +458,6 @@ func (k *Kubernetes) getRecordsForK8sItems(services []service, pods []pod, r rec
|
|||
return records
|
||||
}
|
||||
|
||||
func (k *Kubernetes) findPodWithIP(ip string) (p *api.Pod) {
|
||||
if k.autoPath == nil {
|
||||
return nil
|
||||
}
|
||||
objList := k.APIConn.PodIndex(ip)
|
||||
for _, o := range objList {
|
||||
p, ok := o.(*api.Pod)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return p
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *Kubernetes) findPods(namespace, podname string) (pods []pod, err error) {
|
||||
if k.PodMode == PodModeDisabled {
|
||||
return pods, errPodsDisabled
|
||||
|
|
|
@ -5,13 +5,11 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/coredns/coredns/core/dnsserver"
|
||||
"github.com/coredns/coredns/middleware"
|
||||
"github.com/coredns/coredns/middleware/kubernetes/autopath"
|
||||
"github.com/coredns/coredns/middleware/pkg/dnsutil"
|
||||
"github.com/coredns/coredns/middleware/proxy"
|
||||
"github.com/miekg/dns"
|
||||
|
@ -196,47 +194,6 @@ func kubernetesParse(c *caddy.Controller) (*Kubernetes, error) {
|
|||
continue
|
||||
}
|
||||
return nil, fmt.Errorf("incorrect number of arguments for federation, got %v, expected 2", len(args))
|
||||
case "autopath": // name zone
|
||||
args := c.RemainingArgs()
|
||||
k8s.autoPath = &autopath.AutoPath{
|
||||
NDots: defautNdots,
|
||||
HostSearchPath: []string{},
|
||||
ResolvConfFile: defaultResolvConfFile,
|
||||
OnNXDOMAIN: defaultOnNXDOMAIN,
|
||||
}
|
||||
if len(args) > 3 {
|
||||
return nil, fmt.Errorf("incorrect number of arguments for autopath, got %v, expected at most 3", len(args))
|
||||
|
||||
}
|
||||
if len(args) > 0 {
|
||||
ndots, err := strconv.Atoi(args[0])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid NDOTS argument for autopath, got '%v', expected an integer", ndots)
|
||||
}
|
||||
k8s.autoPath.NDots = ndots
|
||||
}
|
||||
if len(args) > 1 {
|
||||
switch args[1] {
|
||||
case dns.RcodeToString[dns.RcodeNameError]:
|
||||
k8s.autoPath.OnNXDOMAIN = dns.RcodeNameError
|
||||
case dns.RcodeToString[dns.RcodeSuccess]:
|
||||
k8s.autoPath.OnNXDOMAIN = dns.RcodeSuccess
|
||||
case dns.RcodeToString[dns.RcodeServerFailure]:
|
||||
k8s.autoPath.OnNXDOMAIN = dns.RcodeServerFailure
|
||||
default:
|
||||
return nil, fmt.Errorf("invalid RESPONSE argument for autopath, got '%v', expected SERVFAIL, NOERROR, or NXDOMAIN", args[1])
|
||||
}
|
||||
}
|
||||
if len(args) > 2 {
|
||||
k8s.autoPath.ResolvConfFile = args[2]
|
||||
}
|
||||
rc, err := dns.ClientConfigFromFile(k8s.autoPath.ResolvConfFile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error when parsing %v: %v", k8s.autoPath.ResolvConfFile, err)
|
||||
}
|
||||
k8s.autoPath.HostSearchPath = rc.Search
|
||||
middleware.Zones(k8s.autoPath.HostSearchPath).Normalize()
|
||||
continue
|
||||
}
|
||||
}
|
||||
return k8s, nil
|
||||
|
|
|
@ -2,17 +2,11 @@ package kubernetes
|
|||
|
||||
import (
|
||||
"net"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/coredns/coredns/middleware/kubernetes/autopath"
|
||||
"github.com/coredns/coredns/middleware/test"
|
||||
|
||||
"github.com/mholt/caddy"
|
||||
"github.com/miekg/dns"
|
||||
unversionedapi "k8s.io/client-go/1.5/pkg/api/unversioned"
|
||||
)
|
||||
|
||||
|
@ -22,13 +16,6 @@ func parseCidr(cidr string) net.IPNet {
|
|||
}
|
||||
|
||||
func TestKubernetesParse(t *testing.T) {
|
||||
f, rm, err := test.TempFile(os.TempDir(), testResolveConf)
|
||||
autoPathResolvConfFile := f
|
||||
if err != nil {
|
||||
t.Fatalf("Could not create resolv.conf TempFile: %s", err)
|
||||
}
|
||||
defer rm()
|
||||
|
||||
tests := []struct {
|
||||
description string // Human-facing description of test case
|
||||
input string // Corefile data as string
|
||||
|
@ -43,7 +30,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
expectedFallthrough bool
|
||||
expectedUpstreams []string
|
||||
expectedFederations []Federation
|
||||
expectedAutoPath *autopath.AutoPath
|
||||
}{
|
||||
// positive
|
||||
{
|
||||
|
@ -60,7 +46,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"kubernetes keyword with multiple zones",
|
||||
|
@ -76,7 +61,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"kubernetes keyword with zone and empty braces",
|
||||
|
@ -93,7 +77,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"endpoint keyword with url",
|
||||
|
@ -111,7 +94,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"namespaces keyword with one namespace",
|
||||
|
@ -129,7 +111,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"namespaces keyword with multiple namespaces",
|
||||
|
@ -147,7 +128,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"resync period in seconds",
|
||||
|
@ -165,7 +145,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"resync period in minutes",
|
||||
|
@ -183,7 +162,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"basic label selector",
|
||||
|
@ -201,7 +179,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"multi-label selector",
|
||||
|
@ -219,7 +196,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"fully specified valid config",
|
||||
|
@ -241,7 +217,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
true,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// negative
|
||||
{
|
||||
|
@ -258,7 +233,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"kubernetes keyword without a zone",
|
||||
|
@ -274,7 +248,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"endpoint keyword without an endpoint value",
|
||||
|
@ -292,7 +265,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"namespace keyword without a namespace value",
|
||||
|
@ -310,7 +282,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"resyncperiod keyword without a duration value",
|
||||
|
@ -328,7 +299,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"resync period no units",
|
||||
|
@ -346,7 +316,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"resync period invalid",
|
||||
|
@ -364,7 +333,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"labels with no selector value",
|
||||
|
@ -382,7 +350,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"labels with invalid selector value",
|
||||
|
@ -400,7 +367,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// pods disabled
|
||||
{
|
||||
|
@ -419,7 +385,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// pods insecure
|
||||
{
|
||||
|
@ -438,7 +403,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// pods verified
|
||||
{
|
||||
|
@ -457,7 +421,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// pods invalid
|
||||
{
|
||||
|
@ -476,7 +439,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// cidrs ok
|
||||
{
|
||||
|
@ -495,7 +457,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// cidrs ok
|
||||
{
|
||||
|
@ -514,7 +475,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// fallthrough invalid
|
||||
{
|
||||
|
@ -533,7 +493,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// Valid upstream
|
||||
{
|
||||
|
@ -552,7 +511,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
[]string{"13.14.15.16:53"},
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// Invalid upstream
|
||||
{
|
||||
|
@ -571,7 +529,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// Valid federations
|
||||
{
|
||||
|
@ -594,7 +551,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
{name: "foo", zone: "bar.crawl.com"},
|
||||
{name: "fed", zone: "era.tion.com"},
|
||||
},
|
||||
nil,
|
||||
},
|
||||
// Invalid federations
|
||||
{
|
||||
|
@ -613,103 +569,6 @@ func TestKubernetesParse(t *testing.T) {
|
|||
false,
|
||||
nil,
|
||||
[]Federation{},
|
||||
nil,
|
||||
},
|
||||
// autopath
|
||||
{
|
||||
"valid autopath",
|
||||
`kubernetes coredns.local {
|
||||
autopath 1 NXDOMAIN ` + autoPathResolvConfFile + `
|
||||
}`,
|
||||
false,
|
||||
"",
|
||||
1,
|
||||
0,
|
||||
defaultResyncPeriod,
|
||||
"",
|
||||
PodModeDisabled,
|
||||
nil,
|
||||
false,
|
||||
nil,
|
||||
nil,
|
||||
&autopath.AutoPath{
|
||||
NDots: 1,
|
||||
HostSearchPath: []string{"bar.com.", "baz.com."},
|
||||
ResolvConfFile: autoPathResolvConfFile,
|
||||
OnNXDOMAIN: dns.RcodeNameError,
|
||||
},
|
||||
},
|
||||
{
|
||||
"invalid autopath RESPONSE",
|
||||
`kubernetes coredns.local {
|
||||
autopath 0 CRY
|
||||
}`,
|
||||
true,
|
||||
"invalid RESPONSE argument for autopath",
|
||||
-1,
|
||||
0,
|
||||
defaultResyncPeriod,
|
||||
"",
|
||||
PodModeDisabled,
|
||||
nil,
|
||||
false,
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"invalid autopath NDOTS",
|
||||
`kubernetes coredns.local {
|
||||
autopath polka
|
||||
}`,
|
||||
true,
|
||||
"invalid NDOTS argument for autopath",
|
||||
-1,
|
||||
0,
|
||||
defaultResyncPeriod,
|
||||
"",
|
||||
PodModeDisabled,
|
||||
nil,
|
||||
false,
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"invalid autopath RESOLV-CONF",
|
||||
`kubernetes coredns.local {
|
||||
autopath 1 NOERROR /wrong/path/to/resolv.conf
|
||||
}`,
|
||||
true,
|
||||
"error when parsing",
|
||||
-1,
|
||||
0,
|
||||
defaultResyncPeriod,
|
||||
"",
|
||||
PodModeDisabled,
|
||||
nil,
|
||||
false,
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"invalid autopath invalid option",
|
||||
`kubernetes coredns.local {
|
||||
autopath 1 SERVFAIL ` + autoPathResolvConfFile + ` foo
|
||||
}`,
|
||||
true,
|
||||
"incorrect number of arguments",
|
||||
-1,
|
||||
0,
|
||||
defaultResyncPeriod,
|
||||
"",
|
||||
PodModeDisabled,
|
||||
nil,
|
||||
false,
|
||||
nil,
|
||||
nil,
|
||||
nil,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -810,15 +669,5 @@ func TestKubernetesParse(t *testing.T) {
|
|||
|
||||
}
|
||||
}
|
||||
// autopath
|
||||
if !reflect.DeepEqual(test.expectedAutoPath, k8sController.autoPath) {
|
||||
t.Errorf("Test %d: Expected kubernetes controller to be initialized with autopath '%v'. Instead found autopath '%v' for input '%s'", i, test.expectedAutoPath, k8sController.autoPath, test.input)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const testResolveConf = `nameserver 1.2.3.4
|
||||
domain foo.com
|
||||
search bar.com baz.com
|
||||
options ndots:5
|
||||
`
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue