mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 16:24:19 -04:00
Add option to parse resolv.conf for proxy upstreams (#353)
* Add option to parse resolv.conf for proxy upstreams * Add test and README update for resolv.conf proxy * Run gofmt
This commit is contained in:
committed by
Miek Gieben
parent
a2bd9ad3f5
commit
6d9d60081d
@@ -30,7 +30,7 @@ proxy FROM TO... {
|
|||||||
~~~
|
~~~
|
||||||
|
|
||||||
* **FROM** is the name to match for the request to be proxied.
|
* **FROM** is the name to match for the request to be proxied.
|
||||||
* **TO** is the destination endpoint to proxy to. At least one is required, but multiple may be specified.
|
* **TO** is the destination endpoint to proxy to. At least one is required, but multiple may be specified. To may be an IP:Port pair, or may reference a file in resolv.conf format
|
||||||
* `policy` is the load balancing policy to use; applies only with multiple backends. May be one of random, least_conn, or round_robin. Default is random.
|
* `policy` is the load balancing policy to use; applies only with multiple backends. May be one of random, least_conn, or round_robin. Default is random.
|
||||||
* `fail_timeout` specifies how long to consider a backend as down after it has failed. While it is down, requests will not be routed to that backend. A backend is "down" if CoreDNS fails to communicate with it. The default value is 10 seconds ("10s").
|
* `fail_timeout` specifies how long to consider a backend as down after it has failed. While it is down, requests will not be routed to that backend. A backend is "down" if CoreDNS fails to communicate with it. The default value is 10 seconds ("10s").
|
||||||
* `max_fails` is the number of failures within fail_timeout that are needed before considering a backend to be down. If 0, the backend will never be marked as down. Default is 1.
|
* `max_fails` is the number of failures within fail_timeout that are needed before considering a backend to be down. If 0, the backend will never be marked as down. Default is 1.
|
||||||
@@ -86,3 +86,11 @@ proxy . backend:1234 {
|
|||||||
except miek.nl example.org
|
except miek.nl example.org
|
||||||
}
|
}
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
|
Proxy everything except example.org using the host resolv.conf nameservers:
|
||||||
|
|
||||||
|
~~~
|
||||||
|
proxy . /etc/resolv.conf {
|
||||||
|
except miek.nl example.org
|
||||||
|
}
|
||||||
|
~~~
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
@@ -65,13 +66,27 @@ func NewStaticUpstreams(c *caddyfile.Dispenser) ([]Upstream, error) {
|
|||||||
if len(to) == 0 {
|
if len(to) == 0 {
|
||||||
return upstreams, c.ArgErr()
|
return upstreams, c.ArgErr()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// process the host list, substituting in any nameservers in files
|
||||||
|
var toHosts []string
|
||||||
for _, host := range to {
|
for _, host := range to {
|
||||||
h, _, err := net.SplitHostPort(host)
|
h, _, err := net.SplitHostPort(host)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h = host
|
h = host
|
||||||
}
|
}
|
||||||
if x := net.ParseIP(h); x == nil {
|
if x := net.ParseIP(h); x == nil {
|
||||||
return upstreams, fmt.Errorf("not an IP address: `%s'", h)
|
// it's a file, parse as resolv.conf
|
||||||
|
c, err := dns.ClientConfigFromFile(host)
|
||||||
|
if err == os.ErrNotExist {
|
||||||
|
return upstreams, fmt.Errorf("not an IP address or file: `%s'", h)
|
||||||
|
} else if err != nil {
|
||||||
|
return upstreams, err
|
||||||
|
}
|
||||||
|
for _, s := range c.Servers {
|
||||||
|
toHosts = append(toHosts, net.JoinHostPort(s, c.Port))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
toHosts = append(toHosts, host)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,8 +96,8 @@ func NewStaticUpstreams(c *caddyfile.Dispenser) ([]Upstream, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
upstream.Hosts = make([]*UpstreamHost, len(to))
|
upstream.Hosts = make([]*UpstreamHost, len(toHosts))
|
||||||
for i, host := range to {
|
for i, host := range toHosts {
|
||||||
uh := &UpstreamHost{
|
uh := &UpstreamHost{
|
||||||
Name: defaultHostPort(host),
|
Name: defaultHostPort(host),
|
||||||
Conns: 0,
|
Conns: 0,
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
package proxy
|
package proxy
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -78,6 +82,19 @@ func TestAllowedPaths(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func writeTmpFile(t *testing.T, data string) (string, string) {
|
||||||
|
tempDir, err := ioutil.TempDir("", "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("tempDir: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
path := filepath.Join(tempDir, "resolv.conf")
|
||||||
|
if err := ioutil.WriteFile(path, []byte(data), 0644); err != nil {
|
||||||
|
t.Fatalf("writeFile: %v", err)
|
||||||
|
}
|
||||||
|
return tempDir, path
|
||||||
|
}
|
||||||
|
|
||||||
func TestProxyParse(t *testing.T) {
|
func TestProxyParse(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
inputUpstreams string
|
inputUpstreams string
|
||||||
@@ -143,6 +160,11 @@ proxy . 8.8.8.8:53 {
|
|||||||
}`,
|
}`,
|
||||||
true,
|
true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
`
|
||||||
|
proxy . some_bogus_filename`,
|
||||||
|
true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for i, test := range tests {
|
for i, test := range tests {
|
||||||
c := caddy.NewTestController("dns", test.inputUpstreams)
|
c := caddy.NewTestController("dns", test.inputUpstreams)
|
||||||
@@ -152,3 +174,79 @@ proxy . 8.8.8.8:53 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestResolvParse(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
inputUpstreams string
|
||||||
|
filedata string
|
||||||
|
shouldErr bool
|
||||||
|
expected []string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
`
|
||||||
|
proxy . FILE
|
||||||
|
`,
|
||||||
|
`
|
||||||
|
nameserver 1.2.3.4
|
||||||
|
nameserver 4.3.2.1
|
||||||
|
`,
|
||||||
|
false,
|
||||||
|
[]string{"1.2.3.4:53", "4.3.2.1:53"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
`
|
||||||
|
proxy example.com 1.1.1.1:5000
|
||||||
|
proxy . FILE
|
||||||
|
proxy example.org 2.2.2.2:1234
|
||||||
|
`,
|
||||||
|
`
|
||||||
|
nameserver 1.2.3.4
|
||||||
|
`,
|
||||||
|
false,
|
||||||
|
[]string{"1.1.1.1:5000", "1.2.3.4:53", "2.2.2.2:1234"},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
`
|
||||||
|
proxy example.com 1.1.1.1:5000
|
||||||
|
proxy . FILE
|
||||||
|
proxy example.org 2.2.2.2:1234
|
||||||
|
`,
|
||||||
|
`
|
||||||
|
junky resolve.conf
|
||||||
|
`,
|
||||||
|
false,
|
||||||
|
[]string{"1.1.1.1:5000", "2.2.2.2:1234"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for i, test := range tests {
|
||||||
|
tempDir, path := writeTmpFile(t, test.filedata)
|
||||||
|
defer os.RemoveAll(tempDir)
|
||||||
|
config := strings.Replace(test.inputUpstreams, "FILE", path, -1)
|
||||||
|
c := caddy.NewTestController("dns", config)
|
||||||
|
upstreams, err := NewStaticUpstreams(&c.Dispenser)
|
||||||
|
if (err != nil) != test.shouldErr {
|
||||||
|
t.Errorf("Test %d expected no error, got %v", i+1, err)
|
||||||
|
}
|
||||||
|
var hosts []string
|
||||||
|
for _, u := range upstreams {
|
||||||
|
for _, h := range u.(*staticUpstream).Hosts {
|
||||||
|
hosts = append(hosts, h.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !test.shouldErr {
|
||||||
|
if len(hosts) != len(test.expected) {
|
||||||
|
t.Errorf("Test %d expected %d hosts got %d", i+1, len(test.expected), len(upstreams))
|
||||||
|
} else {
|
||||||
|
ok := true
|
||||||
|
for i, v := range test.expected {
|
||||||
|
if v != hosts[i] {
|
||||||
|
ok = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !ok {
|
||||||
|
t.Errorf("Test %d expected %v got %v", i+1, test.expected, upstreams)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user