Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
C
Coredns
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Railgun
Coredns
Commits
9d912fe2
Commit
9d912fe2
authored
Jan 18, 2020
by
Miek Gieben
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement SRV records
Signed-off-by:
Miek Gieben
<
miek@miek.nl
>
parent
63d3dfb0
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
89 additions
and
29 deletions
+89
-29
plugin/traffic/README.md
plugin/traffic/README.md
+8
-6
plugin/traffic/traffic.go
plugin/traffic/traffic.go
+30
-6
plugin/traffic/traffic_test.go
plugin/traffic/traffic_test.go
+37
-6
plugin/traffic/xds/assignment.go
plugin/traffic/xds/assignment.go
+12
-9
plugin/traffic/xds/client.go
plugin/traffic/xds/client.go
+2
-2
No files found.
plugin/traffic/README.md
View file @
9d912fe2
...
...
@@ -24,10 +24,13 @@ endpoints need to be drained from it.
discovered every 10 seconds. The plugin hands out responses that adhere to these assignments. Only
endpoints that are
*healthy*
are handed out.
Each DNS response contains a single IP address that's considered the best one.
*Traffic*
will load
balance A and AAAA queries. The TTL on these answer is set to 5s. It will only return successful
responses either with an answer or otherwise a NODATA response. Queries for non-existent clusters
get a NXDOMAIN, where the minimal TTL is also set to 5s.
Each DNS response contains a single IP address (or SRV record) that's considered the best one.
*Traffic*
will load balance A, AAAA and SRV queries. The TTL on these answer is set to 5s. It will
only return successful responses either with an answer or otherwise a NODATA response. Queries for
non-existent clusters get a NXDOMAIN, where the minimal TTL is also set to 5s.
When an SRV record is returned an endpoint DNS name is synthesized
`endpoint-0.<cluster>.<zone>`
that
carries the IP address. Querying for these synthesized names works as well.
The
*traffic*
plugin has no notion of draining, drop overload and anything that advanced,
*
it just
acts upon assignments
*. This is means that if a endpoint goes down and *
traffic
*
has not seen a new
...
...
@@ -37,7 +40,7 @@ Load reporting is not supported for the following reason. A DNS query is done by
Behind this resolver (which can also cache) there may be many clients that will use this reply. The
responding server (CoreDNS) has no idea how many clients use this resolver. So reporting a load of
+1 on the CoreDNS side can results in anything from 1 to 1000+ of queries on the endpoint, making
the load reporting from
*traf
if
c*
highly inaccurate.
the load reporting from
*traf
fi
c*
highly inaccurate.
## Syntax
...
...
@@ -97,7 +100,6 @@ If monitoring is enabled (via the *prometheus* plugin) then the following metric
*
`coredns_traffic_clusters_tracked{}`
the number of tracked clusters.
## Examples
~~~
...
...
plugin/traffic/traffic.go
View file @
9d912fe2
...
...
@@ -38,12 +38,27 @@ func (t *Traffic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
m
.
SetReply
(
r
)
m
.
Authoritative
=
true
addr
,
ok
:=
t
.
c
.
Select
(
cluster
)
addr
,
port
,
ok
:=
t
.
c
.
Select
(
cluster
)
if
!
ok
{
m
.
Ns
=
soa
(
state
.
Zone
)
m
.
Rcode
=
dns
.
RcodeNameError
w
.
WriteMsg
(
m
)
return
0
,
nil
// ok the cluster (which has potentially extra labels), doesn't exist, but we may have a query for endpoint-0.<cluster>.
// check if we have 2 labels and that the first equals endpoint-0.
if
dns
.
CountLabel
(
cluster
)
!=
2
{
m
.
Ns
=
soa
(
state
.
Zone
)
m
.
Rcode
=
dns
.
RcodeNameError
w
.
WriteMsg
(
m
)
return
0
,
nil
}
labels
:=
dns
.
SplitDomainName
(
cluster
)
if
strings
.
Compare
(
labels
[
0
],
"endpoint-0"
)
==
0
{
// recheck if the cluster exist.
addr
,
port
,
ok
=
t
.
c
.
Select
(
labels
[
1
])
if
!
ok
{
m
.
Ns
=
soa
(
state
.
Zone
)
m
.
Rcode
=
dns
.
RcodeNameError
w
.
WriteMsg
(
m
)
return
0
,
nil
}
}
}
if
addr
==
nil
{
...
...
@@ -66,7 +81,16 @@ func (t *Traffic) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
m
.
Ns
=
soa
(
state
.
Zone
)
break
}
m
.
Answer
=
[]
dns
.
RR
{
&
dns
.
AAAA
{
Hdr
:
dns
.
RR_Header
{
Name
:
state
.
QName
(),
Rrtype
:
dns
.
TypeA
,
Class
:
dns
.
ClassINET
,
Ttl
:
5
},
AAAA
:
addr
}}
m
.
Answer
=
[]
dns
.
RR
{
&
dns
.
AAAA
{
Hdr
:
dns
.
RR_Header
{
Name
:
state
.
QName
(),
Rrtype
:
dns
.
TypeAAAA
,
Class
:
dns
.
ClassINET
,
Ttl
:
5
},
AAAA
:
addr
}}
case
dns
.
TypeSRV
:
target
:=
dnsutil
.
Join
(
"endpoint-0"
,
cluster
)
+
state
.
Zone
m
.
Answer
=
[]
dns
.
RR
{
&
dns
.
SRV
{
Hdr
:
dns
.
RR_Header
{
Name
:
state
.
QName
(),
Rrtype
:
dns
.
TypeA
,
Class
:
dns
.
ClassINET
,
Ttl
:
5
},
Priority
:
100
,
Weight
:
100
,
Port
:
port
,
Target
:
target
}}
if
addr
.
To4
()
==
nil
{
m
.
Extra
=
[]
dns
.
RR
{
&
dns
.
AAAA
{
Hdr
:
dns
.
RR_Header
{
Name
:
target
,
Rrtype
:
dns
.
TypeA
,
Class
:
dns
.
ClassINET
,
Ttl
:
5
},
AAAA
:
addr
}}
}
else
{
m
.
Extra
=
[]
dns
.
RR
{
&
dns
.
A
{
Hdr
:
dns
.
RR_Header
{
Name
:
target
,
Rrtype
:
dns
.
TypeA
,
Class
:
dns
.
ClassINET
,
Ttl
:
5
},
A
:
addr
}}
}
default
:
m
.
Ns
=
soa
(
state
.
Zone
)
}
...
...
plugin/traffic/traffic_test.go
View file @
9d912fe2
...
...
@@ -47,15 +47,22 @@ func TestTraffic(t *testing.T) {
{
cla
:
&
xdspb
.
ClusterLoadAssignment
{
ClusterName
:
"web"
,
Endpoints
:
endpoints
([]
EndpointHealth
{{
"127.0.0.1"
,
corepb
.
HealthStatus_HEALTHY
}}),
Endpoints
:
endpoints
([]
EndpointHealth
{{
"127.0.0.1"
,
18008
,
corepb
.
HealthStatus_HEALTHY
}}),
},
cluster
:
"web"
,
qtype
:
dns
.
TypeA
,
rcode
:
dns
.
RcodeSuccess
,
answer
:
"127.0.0.1"
,
},
{
cla
:
&
xdspb
.
ClusterLoadAssignment
{
ClusterName
:
"web"
,
Endpoints
:
endpoints
([]
EndpointHealth
{{
"::1"
,
18008
,
corepb
.
HealthStatus_HEALTHY
}}),
},
cluster
:
"web"
,
qtype
:
dns
.
TypeAAAA
,
rcode
:
dns
.
RcodeSuccess
,
answer
:
"::1"
,
},
// unknown backend
{
cla
:
&
xdspb
.
ClusterLoadAssignment
{
ClusterName
:
"web"
,
Endpoints
:
endpoints
([]
EndpointHealth
{{
"127.0.0.1"
,
corepb
.
HealthStatus_UNKNOWN
}}),
Endpoints
:
endpoints
([]
EndpointHealth
{{
"127.0.0.1"
,
18008
,
corepb
.
HealthStatus_UNKNOWN
}}),
},
cluster
:
"web"
,
qtype
:
dns
.
TypeA
,
rcode
:
dns
.
RcodeSuccess
,
ns
:
true
,
},
...
...
@@ -64,12 +71,32 @@ func TestTraffic(t *testing.T) {
cla
:
&
xdspb
.
ClusterLoadAssignment
{
ClusterName
:
"web"
,
Endpoints
:
endpoints
([]
EndpointHealth
{
{
"127.0.0.1"
,
corepb
.
HealthStatus_UNKNOWN
},
{
"127.0.0.2"
,
corepb
.
HealthStatus_HEALTHY
},
{
"127.0.0.1"
,
18008
,
corepb
.
HealthStatus_UNKNOWN
},
{
"127.0.0.2"
,
18008
,
corepb
.
HealthStatus_HEALTHY
},
}),
},
cluster
:
"web"
,
qtype
:
dns
.
TypeA
,
rcode
:
dns
.
RcodeSuccess
,
answer
:
"127.0.0.2"
,
},
// SRV query healthy backend
{
cla
:
&
xdspb
.
ClusterLoadAssignment
{
ClusterName
:
"web"
,
Endpoints
:
endpoints
([]
EndpointHealth
{
{
"127.0.0.2"
,
18008
,
corepb
.
HealthStatus_HEALTHY
},
}),
},
cluster
:
"web"
,
qtype
:
dns
.
TypeSRV
,
rcode
:
dns
.
RcodeSuccess
,
answer
:
"endpoint-0.web.lb.example.org."
,
},
// A query for endpoint-0.
{
cla
:
&
xdspb
.
ClusterLoadAssignment
{
ClusterName
:
"web"
,
Endpoints
:
endpoints
([]
EndpointHealth
{
{
"127.0.0.2"
,
18008
,
corepb
.
HealthStatus_HEALTHY
},
}),
},
cluster
:
"endpoint-0.web"
,
qtype
:
dns
.
TypeA
,
rcode
:
dns
.
RcodeSuccess
,
answer
:
"127.0.0.2"
,
},
}
ctx
:=
context
.
TODO
()
...
...
@@ -105,18 +132,19 @@ func TestTraffic(t *testing.T) {
addr
=
x
.
A
.
String
()
case
*
dns
.
AAAA
:
addr
=
x
.
AAAA
.
String
()
case
*
dns
.
SRV
:
addr
=
x
.
Target
}
if
tc
.
answer
!=
addr
{
t
.
Errorf
(
"Test %d: Expected answer %s, but got %s"
,
i
,
tc
.
answer
,
addr
)
}
}
}
}
type
EndpointHealth
struct
{
Address
string
Port
uint16
Health
corepb
.
HealthStatus
}
...
...
@@ -131,6 +159,9 @@ func endpoints(e []EndpointHealth) []*endpointpb.LocalityLbEndpoints {
Address
:
&
corepb
.
Address_SocketAddress
{
SocketAddress
:
&
corepb
.
SocketAddress
{
Address
:
e
[
i
]
.
Address
,
PortSpecifier
:
&
corepb
.
SocketAddress_PortValue
{
PortValue
:
uint32
(
e
[
i
]
.
Port
),
},
},
},
},
...
...
plugin/traffic/xds/assignment.go
View file @
9d912fe2
...
...
@@ -59,12 +59,11 @@ func (a *assignment) clusters() []string {
return
clusters
}
// Select selects a backend from cluster load assignments, using weighted random selection. It only selects
// backends that are reporting healthy.
func
(
a
*
assignment
)
Select
(
cluster
string
)
(
net
.
IP
,
bool
)
{
// Select selects a backend from cluster load assignments, using weighted random selection. It only selects backends that are reporting healthy.
func
(
a
*
assignment
)
Select
(
cluster
string
)
(
ip
net
.
IP
,
port
uint16
,
exists
bool
)
{
cla
:=
a
.
ClusterLoadAssignment
(
cluster
)
if
cla
==
nil
{
return
nil
,
false
return
nil
,
0
,
false
}
total
:=
0
...
...
@@ -79,7 +78,7 @@ func (a *assignment) Select(cluster string) (net.IP, bool) {
}
}
if
healthy
==
0
{
return
nil
,
true
return
nil
,
0
,
true
}
if
total
==
0
{
...
...
@@ -92,12 +91,14 @@ func (a *assignment) Select(cluster string) (net.IP, bool) {
continue
}
if
r
==
i
{
return
net
.
ParseIP
(
lb
.
GetEndpoint
()
.
GetAddress
()
.
GetSocketAddress
()
.
GetAddress
()),
true
addr
:=
net
.
ParseIP
(
lb
.
GetEndpoint
()
.
GetAddress
()
.
GetSocketAddress
()
.
GetAddress
())
port
:=
uint16
(
lb
.
GetEndpoint
()
.
GetAddress
()
.
GetSocketAddress
()
.
GetPortValue
())
return
addr
,
port
,
true
}
i
++
}
}
return
nil
,
true
return
nil
,
0
,
true
}
r
:=
rand
.
Intn
(
total
)
+
1
...
...
@@ -108,9 +109,11 @@ func (a *assignment) Select(cluster string) (net.IP, bool) {
}
r
-=
int
(
lb
.
GetLoadBalancingWeight
()
.
GetValue
())
if
r
<=
0
{
return
net
.
ParseIP
(
lb
.
GetEndpoint
()
.
GetAddress
()
.
GetSocketAddress
()
.
GetAddress
()),
true
addr
:=
net
.
ParseIP
(
lb
.
GetEndpoint
()
.
GetAddress
()
.
GetSocketAddress
()
.
GetAddress
())
port
:=
uint16
(
lb
.
GetEndpoint
()
.
GetAddress
()
.
GetSocketAddress
()
.
GetPortValue
())
return
addr
,
port
,
true
}
}
}
return
nil
,
true
return
nil
,
0
,
true
}
plugin/traffic/xds/client.go
View file @
9d912fe2
...
...
@@ -228,9 +228,9 @@ func (c *Client) receive(stream adsStream) error {
// Select returns an address that is deemed to be the correct one for this cluster. The returned
// boolean indicates if the cluster exists.
func
(
c
*
Client
)
Select
(
cluster
string
)
(
net
.
IP
,
bool
)
{
func
(
c
*
Client
)
Select
(
cluster
string
)
(
net
.
IP
,
uint16
,
bool
)
{
if
cluster
==
""
{
return
nil
,
false
return
nil
,
0
,
false
}
return
c
.
assignments
.
Select
(
cluster
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment