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
8582830b
Commit
8582830b
authored
Jan 16, 2020
by
Miek Gieben
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Stop hardcoding things
Signed-off-by:
Miek Gieben
<
miek@miek.nl
>
parent
0736ccb1
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
75 additions
and
21 deletions
+75
-21
plugin/traffic/README.md
plugin/traffic/README.md
+5
-3
plugin/traffic/setup.go
plugin/traffic/setup.go
+10
-6
plugin/traffic/traffic.go
plugin/traffic/traffic.go
+60
-12
No files found.
plugin/traffic/README.md
View file @
8582830b
...
@@ -42,7 +42,7 @@ traffic TO...
...
@@ -42,7 +42,7 @@ traffic TO...
The extended syntax is available is you want more control.
The extended syntax is available is you want more control.
~~~
~~~
traffic {
traffic
TO...
{
server SERVER [SERVER]...
server SERVER [SERVER]...
node ID
node ID
}
}
...
@@ -60,9 +60,11 @@ names. For example if the Server Block specifies `lb.example.org` as one of the
...
@@ -60,9 +60,11 @@ names. For example if the Server Block specifies `lb.example.org` as one of the
## Examples
## Examples
~~~
corefile
~~~
lb.example.org {
lb.example.org {
traffic grpc://127.0.0.1:18000
traffic grpc://127.0.0.1:18000 {
node test-id
}
debug
debug
log
log
}
}
...
...
plugin/traffic/setup.go
View file @
8582830b
...
@@ -55,8 +55,14 @@ func setup(c *caddy.Controller) error {
...
@@ -55,8 +55,14 @@ func setup(c *caddy.Controller) error {
func
parseTraffic
(
c
*
caddy
.
Controller
)
(
*
Traffic
,
error
)
{
func
parseTraffic
(
c
*
caddy
.
Controller
)
(
*
Traffic
,
error
)
{
node
:=
"coredns"
node
:=
"coredns"
toHosts
:=
[]
string
{}
toHosts
:=
[]
string
{}
t
:=
&
Traffic
{}
var
err
error
var
err
error
t
.
origins
=
make
([]
string
,
len
(
c
.
ServerBlockKeys
))
for
i
:=
range
c
.
ServerBlockKeys
{
t
.
origins
[
i
]
=
plugin
.
Host
(
c
.
ServerBlockKeys
[
i
])
.
Normalize
()
}
for
c
.
Next
()
{
for
c
.
Next
()
{
args
:=
c
.
RemainingArgs
()
args
:=
c
.
RemainingArgs
()
if
len
(
args
)
<
1
{
if
len
(
args
)
<
1
{
...
@@ -70,8 +76,8 @@ func parseTraffic(c *caddy.Controller) (*Traffic, error) {
...
@@ -70,8 +76,8 @@ func parseTraffic(c *caddy.Controller) (*Traffic, error) {
if
!
strings
.
HasPrefix
(
toHosts
[
i
],
transport
.
GRPC
+
"://"
)
{
if
!
strings
.
HasPrefix
(
toHosts
[
i
],
transport
.
GRPC
+
"://"
)
{
return
nil
,
fmt
.
Errorf
(
"not a %s scheme: %s"
,
transport
.
GRPC
,
toHosts
[
i
])
return
nil
,
fmt
.
Errorf
(
"not a %s scheme: %s"
,
transport
.
GRPC
,
toHosts
[
i
])
}
}
// now cut the prefix off again, because the dialer needs to see normal address strings. All this
// now cut the prefix off again, because the dial
l
er needs to see normal address strings. All this
// grpc:// stuff is to enfor
e uniformaty ac
cross plugins and future proofing for other protocols.
// grpc:// stuff is to enfor
ce uniform a
cross plugins and future proofing for other protocols.
toHosts
[
i
]
=
toHosts
[
i
][
len
(
transport
.
GRPC
+
"://"
)
:
]
toHosts
[
i
]
=
toHosts
[
i
][
len
(
transport
.
GRPC
+
"://"
)
:
]
}
}
for
c
.
NextBlock
()
{
for
c
.
NextBlock
()
{
...
@@ -88,12 +94,10 @@ func parseTraffic(c *caddy.Controller) (*Traffic, error) {
...
@@ -88,12 +94,10 @@ func parseTraffic(c *caddy.Controller) (*Traffic, error) {
}
}
}
}
// TODO: only the first host is used.
// TODO: only the first host is used, need to figure out how to reconcile multiple upstream providers.
x
,
err
:=
xds
.
New
(
toHosts
[
0
],
node
)
if
t
.
c
,
err
=
xds
.
New
(
toHosts
[
0
],
node
);
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
t
:=
&
Traffic
{
c
:
x
}
return
t
,
nil
return
t
,
nil
}
}
plugin/traffic/traffic.go
View file @
8582830b
...
@@ -2,6 +2,8 @@ package traffic
...
@@ -2,6 +2,8 @@ package traffic
import
(
import
(
"context"
"context"
"strings"
"time"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/dnsutil"
"github.com/coredns/coredns/plugin/pkg/dnsutil"
...
@@ -13,9 +15,10 @@ import (
...
@@ -13,9 +15,10 @@ import (
// Traffic is a plugin that load balances according to assignments.
// Traffic is a plugin that load balances according to assignments.
type
Traffic
struct
{
type
Traffic
struct
{
c
*
xds
.
Client
c
*
xds
.
Client
id
string
id
string
Next
plugin
.
Handler
origins
[]
string
Next
plugin
.
Handler
}
}
// shutdown closes the connection to the managment endpoints and stops any running goroutines.
// shutdown closes the connection to the managment endpoints and stops any running goroutines.
...
@@ -25,26 +28,71 @@ func (t *Traffic) shutdown() { t.c.Close() }
...
@@ -25,26 +28,71 @@ func (t *Traffic) shutdown() { t.c.Close() }
func
(
t
*
Traffic
)
ServeDNS
(
ctx
context
.
Context
,
w
dns
.
ResponseWriter
,
r
*
dns
.
Msg
)
(
int
,
error
)
{
func
(
t
*
Traffic
)
ServeDNS
(
ctx
context
.
Context
,
w
dns
.
ResponseWriter
,
r
*
dns
.
Msg
)
(
int
,
error
)
{
state
:=
request
.
Request
{
Req
:
r
,
W
:
w
}
state
:=
request
.
Request
{
Req
:
r
,
W
:
w
}
cluster
,
_
:=
dnsutil
.
TrimZone
(
state
.
Name
(),
"example.org"
)
cluster
:=
""
addr
:=
t
.
c
.
Select
(
cluster
)
for
_
,
o
:=
range
t
.
origins
{
if
addr
==
nil
{
println
(
o
,
state
.
Name
())
if
strings
.
HasSuffix
(
state
.
Name
(),
o
)
{
cluster
,
_
=
dnsutil
.
TrimZone
(
state
.
Name
(),
o
)
state
.
Zone
=
o
break
}
}
if
cluster
==
""
{
// TODO(miek): can this actually happen?
return
plugin
.
NextOrFailure
(
t
.
Name
(),
t
.
Next
,
ctx
,
w
,
r
)
return
plugin
.
NextOrFailure
(
t
.
Name
(),
t
.
Next
,
ctx
,
w
,
r
)
}
}
log
.
Debugf
(
"Found address %q for %q"
,
addr
,
cluster
)
addr
:=
t
.
c
.
Select
(
cluster
)
if
addr
!=
nil
{
log
.
Debugf
(
"Found endpoint %q for %q"
,
addr
,
cluster
)
}
else
{
log
.
Debugf
(
"No healthy endpoints found for %q"
,
cluster
)
}
// assemble reply
m
:=
new
(
dns
.
Msg
)
m
:=
new
(
dns
.
Msg
)
m
.
SetReply
(
r
)
m
.
SetReply
(
r
)
m
.
Answer
=
[]
dns
.
RR
{
&
dns
.
A
{
if
addr
==
nil
{
Hdr
:
dns
.
RR_Header
{
Name
:
state
.
QName
(),
Rrtype
:
dns
.
TypeA
,
Class
:
dns
.
ClassINET
,
Ttl
:
5
},
m
.
Ns
=
soa
(
state
.
Zone
)
A
:
addr
,
w
.
WriteMsg
(
m
)
}}
return
0
,
nil
}
switch
state
.
QType
()
{
case
dns
.
TypeA
:
if
addr
.
To4
()
==
nil
{
// it's an IPv6 address, return nodata in that case.
m
.
Ns
=
soa
(
state
.
Zone
)
break
}
m
.
Answer
=
[]
dns
.
RR
{
&
dns
.
A
{
Hdr
:
dns
.
RR_Header
{
Name
:
state
.
QName
(),
Rrtype
:
dns
.
TypeA
,
Class
:
dns
.
ClassINET
,
Ttl
:
5
},
A
:
addr
}}
case
dns
.
TypeAAAA
:
if
addr
.
To4
()
!=
nil
{
// it's an IPv4 address, return nodata in that case.
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
}}
default
:
m
.
Ns
=
soa
(
state
.
Zone
)
}
w
.
WriteMsg
(
m
)
w
.
WriteMsg
(
m
)
return
0
,
nil
return
0
,
nil
}
}
// soa returns a synthetic so for this zone.
func
soa
(
z
string
)
[]
dns
.
RR
{
return
[]
dns
.
RR
{
&
dns
.
SOA
{
Hdr
:
dns
.
RR_Header
{
Name
:
z
,
Rrtype
:
dns
.
TypeSOA
,
Class
:
dns
.
ClassINET
,
Ttl
:
5
},
Ns
:
dnsutil
.
Join
(
"ns"
,
z
),
Mbox
:
dnsutil
.
Join
(
"coredns"
,
z
),
Serial
:
uint32
(
time
.
Now
()
.
UTC
()
.
Unix
()),
Refresh
:
14400
,
Retry
:
3600
,
Expire
:
604800
,
Minttl
:
5
,
}}
}
// Name implements the plugin.Handler interface.
// Name implements the plugin.Handler interface.
func
(
t
*
Traffic
)
Name
()
string
{
return
"traffic"
}
func
(
t
*
Traffic
)
Name
()
string
{
return
"traffic"
}
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