Kubernetes srv (#823)

* Treat absence of port/service in SRV as wildcard

Normally, a SRV-request should have the form
_<service>._<port>.<name>.<zone>. The k8s peer-finder which is used for
bootstrapping by some applications will however query for SRV at
<name>.<zone>.

To compensate for this behaviour, treat the absence of _<service> and
_<port> as wildcards.

* Modified tests with new SRV behaviour

Added a testcase for a SRV request without port & service
Removed now valid query from invalidSRVQueries

* Forgot to run gofmt on test/kubernetes_test.go
This commit is contained in:
Markus Sommer 2017-08-04 15:41:55 +02:00 committed by John Belamaric
parent 2c0fc3182c
commit d0d7f4c89a
3 changed files with 35 additions and 25 deletions

View file

@ -299,34 +299,42 @@ func (k *Kubernetes) parseRequest(lowerCasedName string, qtype uint16) (r record
offset := 0 offset := 0
if qtype == dns.TypeSRV { if qtype == dns.TypeSRV {
if len(segs) != 5 { // The kubernetes peer-finder expects queries with empty port and service to resolve
return r, errInvalidRequest // If neither is specified, treat it as a wildcard
} if len(segs) == 3 {
// This is a SRV style request, get first two elements as port and r.port = "*"
// protocol, stripping leading underscores if present. r.service = "*"
if segs[0][0] == '_' { offset = 0
r.port = segs[0][1:]
} else { } else {
r.port = segs[0] if len(segs) != 5 {
if !symbolContainsWildcard(r.port) {
return r, errInvalidRequest return r, errInvalidRequest
} }
} // This is a SRV style request, get first two elements as port and
if segs[1][0] == '_' { // protocol, stripping leading underscores if present.
r.protocol = segs[1][1:] if segs[0][0] == '_' {
if r.protocol != "tcp" && r.protocol != "udp" { r.port = segs[0][1:]
return r, errInvalidRequest } else {
} r.port = segs[0]
} else { if !symbolContainsWildcard(r.port) {
r.protocol = segs[1] return r, errInvalidRequest
if !symbolContainsWildcard(r.protocol) { }
}
if segs[1][0] == '_' {
r.protocol = segs[1][1:]
if r.protocol != "tcp" && r.protocol != "udp" {
return r, errInvalidRequest
}
} else {
r.protocol = segs[1]
if !symbolContainsWildcard(r.protocol) {
return r, errInvalidRequest
}
}
if r.port == "" || r.protocol == "" {
return r, errInvalidRequest return r, errInvalidRequest
} }
offset = 2
} }
if r.port == "" || r.protocol == "" {
return r, errInvalidRequest
}
offset = 2
} }
if (qtype == dns.TypeA || qtype == dns.TypeAAAA) && len(segs) == 4 { if (qtype == dns.TypeA || qtype == dns.TypeAAAA) && len(segs) == 4 {
// This is an endpoint A/AAAA record request. Get first element as endpoint. // This is an endpoint A/AAAA record request. Get first element as endpoint.

View file

@ -227,7 +227,6 @@ func TestParseRequest(t *testing.T) {
} }
invalidSRVQueries := []string{ invalidSRVQueries := []string{
"webs.mynamespace.svc.inter.webs.test.", // SRV requests must have port and protocol
"_http._pcp.webs.mynamespace.svc.inter.webs.test.", // SRV protocol must be tcp or udp "_http._pcp.webs.mynamespace.svc.inter.webs.test.", // SRV protocol must be tcp or udp
"_http._tcp.ep.webs.ns.svc.inter.webs.test.", // SRV requests cannot have an endpoint "_http._tcp.ep.webs.ns.svc.inter.webs.test.", // SRV requests cannot have an endpoint
"_*._*.webs.mynamespace.svc.inter.webs.test.", // SRV request with invalid wildcards "_*._*.webs.mynamespace.svc.inter.webs.test.", // SRV request with invalid wildcards

View file

@ -207,8 +207,11 @@ var dnsTestCases = []test.Case{
}, },
{ {
Qname: "svc-1-a.test-1.svc.cluster.local.", Qtype: dns.TypeSRV, Qname: "svc-1-a.test-1.svc.cluster.local.", Qtype: dns.TypeSRV,
Rcode: dns.RcodeNameError, Rcode: dns.RcodeSuccess,
Answer: []dns.RR{}, Answer: []dns.RR{
test.SRV("_http._tcp.svc-1-a.test-1.svc.cluster.local. 303 IN SRV 10 100 80 svc-1-a.test-1.svc.cluster.local."),
test.SRV("_https._tcp.svc-1-a.test-1.svc.cluster.local. 303 IN SRV 10 100 443 svc-1-a.test-1.svc.cluster.local."),
},
}, },
{ {
Qname: "10-20-0-101.test-1.pod.cluster.local.", Qtype: dns.TypeA, Qname: "10-20-0-101.test-1.pod.cluster.local.", Qtype: dns.TypeA,