diff options
author | 2018-01-27 17:25:39 -0500 | |
---|---|---|
committer | 2018-01-27 17:25:39 -0500 | |
commit | d27be86e3e5baaa8e63c84e320207156f58b30cf (patch) | |
tree | 1fe5d8c770d7960892d86bee7deb4757669c005a /plugin/proxy | |
parent | 3b4235a7c62dc05a04eb5097c50c8a83b937ae8e (diff) | |
download | coredns-d27be86e3e5baaa8e63c84e320207156f58b30cf.tar.gz coredns-d27be86e3e5baaa8e63c84e320207156f58b30cf.tar.zst coredns-d27be86e3e5baaa8e63c84e320207156f58b30cf.zip |
Minor refactor of proxy parsing to make upstreams re-usable in other plugins (#1426)
Diffstat (limited to 'plugin/proxy')
-rw-r--r-- | plugin/proxy/upstream.go | 88 |
1 files changed, 49 insertions, 39 deletions
diff --git a/plugin/proxy/upstream.go b/plugin/proxy/upstream.go index bcb921973..421163b83 100644 --- a/plugin/proxy/upstream.go +++ b/plugin/proxy/upstream.go @@ -28,56 +28,66 @@ type staticUpstream struct { func NewStaticUpstreams(c *caddyfile.Dispenser) ([]Upstream, error) { var upstreams []Upstream for c.Next() { - upstream := &staticUpstream{ - from: ".", - HealthCheck: healthcheck.HealthCheck{ - FailTimeout: 5 * time.Second, - MaxFails: 3, - }, - ex: newDNSEx(), + u, err := NewStaticUpstream(c) + if err != nil { + return upstreams, err } + upstreams = append(upstreams, u) + } + return upstreams, nil +} - if !c.Args(&upstream.from) { - return upstreams, c.ArgErr() - } - upstream.from = plugin.Host(upstream.from).Normalize() +// NewStaticUpstream parses the configuration of a single upstream +// starting from the FROM +func NewStaticUpstream(c *caddyfile.Dispenser) (Upstream, error) { + upstream := &staticUpstream{ + from: ".", + HealthCheck: healthcheck.HealthCheck{ + FailTimeout: 5 * time.Second, + MaxFails: 3, + }, + ex: newDNSEx(), + } - to := c.RemainingArgs() - if len(to) == 0 { - return upstreams, c.ArgErr() - } + if !c.Args(&upstream.from) { + return upstream, c.ArgErr() + } + upstream.from = plugin.Host(upstream.from).Normalize() - // process the host list, substituting in any nameservers in files - toHosts, err := dnsutil.ParseHostPortOrFile(to...) - if err != nil { - return upstreams, err - } + to := c.RemainingArgs() + if len(to) == 0 { + return upstream, c.ArgErr() + } - if len(toHosts) > max { - return upstreams, fmt.Errorf("more than %d TOs configured: %d", max, len(toHosts)) - } + // process the host list, substituting in any nameservers in files + toHosts, err := dnsutil.ParseHostPortOrFile(to...) + if err != nil { + return upstream, err + } - for c.NextBlock() { - if err := parseBlock(c, upstream); err != nil { - return upstreams, err - } + if len(toHosts) > max { + return upstream, fmt.Errorf("more than %d TOs configured: %d", max, len(toHosts)) + } + + for c.NextBlock() { + if err := parseBlock(c, upstream); err != nil { + return upstream, err } + } - upstream.Hosts = make([]*healthcheck.UpstreamHost, len(toHosts)) + upstream.Hosts = make([]*healthcheck.UpstreamHost, len(toHosts)) - for i, host := range toHosts { - uh := &healthcheck.UpstreamHost{ - Name: host, - FailTimeout: upstream.FailTimeout, - CheckDown: checkDownFunc(upstream), - } - upstream.Hosts[i] = uh + for i, host := range toHosts { + uh := &healthcheck.UpstreamHost{ + Name: host, + FailTimeout: upstream.FailTimeout, + CheckDown: checkDownFunc(upstream), } - upstream.Start() - - upstreams = append(upstreams, upstream) + upstream.Hosts[i] = uh } - return upstreams, nil + upstream.Start() + + return upstream, nil } func parseBlock(c *caddyfile.Dispenser, u *staticUpstream) error { |