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
5e04c272
Commit
5e04c272
authored
Dec 17, 2019
by
Zou Nengren
Committed by
Miek Gieben
Dec 17, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Dedup policy implement between grpc and proxy plugin (#3537)
Signed-off-by:
zouyee
<
zounengren@cmss.chinamobile.com
>
parent
acb75ea9
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
104 additions
and
78 deletions
+104
-78
plugin/forward/forward.go
plugin/forward/forward.go
+11
-5
plugin/forward/setup.go
plugin/forward/setup.go
+4
-3
plugin/grpc/grpc.go
plugin/grpc/grpc.go
+9
-3
plugin/grpc/policy.go
plugin/grpc/policy.go
+0
-64
plugin/grpc/setup.go
plugin/grpc/setup.go
+4
-3
plugin/pkg/policy/policy.go
plugin/pkg/policy/policy.go
+76
-0
No files found.
plugin/forward/forward.go
View file @
5e04c272
...
...
@@ -12,6 +12,7 @@ import (
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/debug"
"github.com/coredns/coredns/plugin/pkg/policy"
clog
"github.com/coredns/coredns/plugin/pkg/log"
"github.com/coredns/coredns/request"
...
...
@@ -25,7 +26,7 @@ var log = clog.NewWithPlugin("forward")
// of proxies each representing one upstream proxy.
type
Forward
struct
{
proxies
[]
*
Proxy
p
Policy
p
policy
.
Policy
hcInterval
time
.
Duration
from
string
...
...
@@ -43,7 +44,7 @@ type Forward struct {
// New returns a new Forward.
func
New
()
*
Forward
{
f
:=
&
Forward
{
maxfails
:
2
,
tlsConfig
:
new
(
tls
.
Config
),
expire
:
defaultExpire
,
p
:
new
(
r
andom
),
from
:
"."
,
hcInterval
:
hcInterval
}
f
:=
&
Forward
{
maxfails
:
2
,
tlsConfig
:
new
(
tls
.
Config
),
expire
:
defaultExpire
,
p
:
new
(
policy
.
R
andom
),
from
:
"."
,
hcInterval
:
hcInterval
}
return
f
}
...
...
@@ -91,8 +92,8 @@ func (f *Forward) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
}
// All upstream proxies are dead, assume healthcheck is completely broken and randomly
// select an upstream to connect to.
r
:=
new
(
r
andom
)
proxy
=
r
.
List
(
f
.
proxies
)[
0
]
r
:=
new
(
policy
.
R
andom
)
proxy
=
r
.
List
(
f
.
proxies
)[
0
]
.
([]
*
Proxy
)[
0
]
HealthcheckBrokenCount
.
Add
(
1
)
}
...
...
@@ -188,7 +189,12 @@ func (f *Forward) ForceTCP() bool { return f.opts.forceTCP }
func
(
f
*
Forward
)
PreferUDP
()
bool
{
return
f
.
opts
.
preferUDP
}
// List returns a set of proxies to be used for this client depending on the policy in f.
func
(
f
*
Forward
)
List
()
[]
*
Proxy
{
return
f
.
p
.
List
(
f
.
proxies
)
}
func
(
f
*
Forward
)
List
()
[]
*
Proxy
{
if
len
(
f
.
p
.
List
(
f
.
proxies
))
==
1
{
return
f
.
p
.
List
(
f
.
proxies
)[
0
]
.
([]
*
Proxy
)
}
return
nil
}
var
(
// ErrNoHealthy means no healthy proxies left.
...
...
plugin/forward/setup.go
View file @
5e04c272
...
...
@@ -8,6 +8,7 @@ import (
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics"
"github.com/coredns/coredns/plugin/pkg/policy"
"github.com/coredns/coredns/plugin/pkg/parse"
pkgtls
"github.com/coredns/coredns/plugin/pkg/tls"
"github.com/coredns/coredns/plugin/pkg/transport"
...
...
@@ -202,11 +203,11 @@ func parseBlock(c *caddy.Controller, f *Forward) error {
}
switch
x
:=
c
.
Val
();
x
{
case
"random"
:
f
.
p
=
&
r
andom
{}
f
.
p
=
&
policy
.
R
andom
{}
case
"round_robin"
:
f
.
p
=
&
r
oundRobin
{}
f
.
p
=
&
policy
.
R
oundRobin
{}
case
"sequential"
:
f
.
p
=
&
s
equential
{}
f
.
p
=
&
policy
.
S
equential
{}
default
:
return
c
.
Errf
(
"unknown policy '%s'"
,
x
)
}
...
...
plugin/grpc/grpc.go
View file @
5e04c272
...
...
@@ -7,6 +7,7 @@ import (
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/debug"
"github.com/coredns/coredns/plugin/pkg/policy"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
...
...
@@ -17,7 +18,7 @@ import (
// It has a list of proxies each representing one upstream proxy.
type
GRPC
struct
{
proxies
[]
*
Proxy
p
Policy
p
policy
.
Policy
from
string
ignored
[]
string
...
...
@@ -93,7 +94,7 @@ func (g *GRPC) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (
// NewGRPC returns a new GRPC.
func
newGRPC
()
*
GRPC
{
g
:=
&
GRPC
{
p
:
new
(
r
andom
),
p
:
new
(
policy
.
R
andom
),
}
return
g
}
...
...
@@ -126,6 +127,11 @@ func (g *GRPC) isAllowedDomain(name string) bool {
}
// List returns a set of proxies to be used for this client depending on the policy in p.
func
(
g
*
GRPC
)
list
()
[]
*
Proxy
{
return
g
.
p
.
List
(
g
.
proxies
)
}
func
(
g
*
GRPC
)
list
()
[]
*
Proxy
{
if
len
(
g
.
p
.
List
(
g
.
proxies
))
==
1
{
return
g
.
p
.
List
(
g
.
proxies
)[
0
]
.
([]
*
Proxy
)
}
return
nil
}
const
defaultTimeout
=
5
*
time
.
Second
plugin/grpc/policy.go
deleted
100644 → 0
View file @
acb75ea9
package
grpc
import
(
"math/rand"
"sync/atomic"
)
// Policy defines a policy we use for selecting upstreams.
type
Policy
interface
{
List
([]
*
Proxy
)
[]
*
Proxy
String
()
string
}
// random is a policy that implements random upstream selection.
type
random
struct
{}
func
(
r
*
random
)
String
()
string
{
return
"random"
}
func
(
r
*
random
)
List
(
p
[]
*
Proxy
)
[]
*
Proxy
{
switch
len
(
p
)
{
case
1
:
return
p
case
2
:
if
rand
.
Int
()
%
2
==
0
{
return
[]
*
Proxy
{
p
[
1
],
p
[
0
]}
// swap
}
return
p
}
perms
:=
rand
.
Perm
(
len
(
p
))
rnd
:=
make
([]
*
Proxy
,
len
(
p
))
for
i
,
p1
:=
range
perms
{
rnd
[
i
]
=
p
[
p1
]
}
return
rnd
}
// roundRobin is a policy that selects hosts based on round robin ordering.
type
roundRobin
struct
{
robin
uint32
}
func
(
r
*
roundRobin
)
String
()
string
{
return
"round_robin"
}
func
(
r
*
roundRobin
)
List
(
p
[]
*
Proxy
)
[]
*
Proxy
{
poolLen
:=
uint32
(
len
(
p
))
i
:=
atomic
.
AddUint32
(
&
r
.
robin
,
1
)
%
poolLen
robin
:=
[]
*
Proxy
{
p
[
i
]}
robin
=
append
(
robin
,
p
[
:
i
]
...
)
robin
=
append
(
robin
,
p
[
i
+
1
:
]
...
)
return
robin
}
// sequential is a policy that selects hosts based on sequential ordering.
type
sequential
struct
{}
func
(
r
*
sequential
)
String
()
string
{
return
"sequential"
}
func
(
r
*
sequential
)
List
(
p
[]
*
Proxy
)
[]
*
Proxy
{
return
p
}
plugin/grpc/setup.go
View file @
5e04c272
...
...
@@ -8,6 +8,7 @@ import (
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics"
"github.com/coredns/coredns/plugin/pkg/parse"
"github.com/coredns/coredns/plugin/pkg/policy"
pkgtls
"github.com/coredns/coredns/plugin/pkg/tls"
"github.com/caddyserver/caddy"
...
...
@@ -132,11 +133,11 @@ func parseBlock(c *caddy.Controller, g *GRPC) error {
}
switch
x
:=
c
.
Val
();
x
{
case
"random"
:
g
.
p
=
&
r
andom
{}
g
.
p
=
&
policy
.
R
andom
{}
case
"round_robin"
:
g
.
p
=
&
r
oundRobin
{}
g
.
p
=
&
policy
.
R
oundRobin
{}
case
"sequential"
:
g
.
p
=
&
s
equential
{}
g
.
p
=
&
policy
.
S
equential
{}
default
:
return
c
.
Errf
(
"unknown policy '%s'"
,
x
)
}
...
...
plugin/
forward
/policy.go
→
plugin/
pkg/policy
/policy.go
View file @
5e04c272
package
forward
package
policy
import
(
"math/rand"
...
...
@@ -7,28 +7,32 @@ import (
// Policy defines a policy we use for selecting upstreams.
type
Policy
interface
{
List
(
[]
*
Proxy
)
[]
*
Proxy
List
(
policy
...
interface
{})
[]
interface
{}
String
()
string
}
//
r
andom is a policy that implements random upstream selection.
type
r
andom
struct
{}
//
R
andom is a policy that implements random upstream selection.
type
R
andom
struct
{}
func
(
r
*
random
)
String
()
string
{
return
"random"
}
var
_
Policy
=
&
Random
{
}
func
(
r
*
random
)
List
(
p
[]
*
Proxy
)
[]
*
Proxy
{
// String returns the name of policy Random
func
(
r
*
Random
)
String
()
string
{
return
"random"
}
// List returns a set of proxies to be used for this client depending on Random policy.
func
(
r
*
Random
)
List
(
p
...
interface
{})
[]
interface
{}
{
switch
len
(
p
)
{
case
1
:
return
p
case
2
:
if
rand
.
Int
()
%
2
==
0
{
return
[]
*
Proxy
{
p
[
1
],
p
[
0
]}
// swap
return
[]
interface
{}
{
p
[
1
],
p
[
0
]}
// swap
}
return
p
}
perms
:=
rand
.
Perm
(
len
(
p
))
rnd
:=
make
([]
*
Proxy
,
len
(
p
))
rnd
:=
make
([]
interface
{}
,
len
(
p
))
for
i
,
p1
:=
range
perms
{
rnd
[
i
]
=
p
[
p1
]
...
...
@@ -36,29 +40,37 @@ func (r *random) List(p []*Proxy) []*Proxy {
return
rnd
}
//
r
oundRobin is a policy that selects hosts based on round robin ordering.
type
r
oundRobin
struct
{
//
R
oundRobin is a policy that selects hosts based on round robin ordering.
type
R
oundRobin
struct
{
robin
uint32
}
func
(
r
*
roundRobin
)
String
()
string
{
return
"round_robin"
}
var
_
Policy
=
&
RoundRobin
{}
// String returns the name of policy RoundRobin
func
(
r
*
RoundRobin
)
String
()
string
{
return
"round_robin"
}
func
(
r
*
roundRobin
)
List
(
p
[]
*
Proxy
)
[]
*
Proxy
{
// List returns a set of proxies to be used for this client depending on RoundRobin policy.
func
(
r
*
RoundRobin
)
List
(
p
...
interface
{})
[]
interface
{}
{
poolLen
:=
uint32
(
len
(
p
))
i
:=
atomic
.
AddUint32
(
&
r
.
robin
,
1
)
%
poolLen
robin
:=
[]
*
Proxy
{
p
[
i
]}
robin
:=
[]
interface
{}
{
p
[
i
]}
robin
=
append
(
robin
,
p
[
:
i
]
...
)
robin
=
append
(
robin
,
p
[
i
+
1
:
]
...
)
return
robin
}
// sequential is a policy that selects hosts based on sequential ordering.
type
sequential
struct
{}
// Sequential is a policy that selects hosts based on sequential ordering.
type
Sequential
struct
{}
var
_
Policy
=
&
Sequential
{}
func
(
r
*
sequential
)
String
()
string
{
return
"sequential"
}
// String returns the name of policy Sequential
func
(
r
*
Sequential
)
String
()
string
{
return
"sequential"
}
func
(
r
*
sequential
)
List
(
p
[]
*
Proxy
)
[]
*
Proxy
{
// List returns a set of proxies without filter.
func
(
r
*
Sequential
)
List
(
p
...
interface
{})
[]
interface
{}
{
return
p
}
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