coredns/middleware/file/setup.go
Miek Gieben 090d1872e9 Golint2 (#280)
* Fix linter errors

* More linting fixes

* More docs and making members private that dont need to be public

* Fix linter errors

* More linting fixes

* More docs and making members private that dont need to be public

* More lint fixes

This leaves:

~~~
middleware/kubernetes/nametemplate/nametemplate.go:64:6: exported type NameTemplate should have comment or be unexported
middleware/kubernetes/nametemplate/nametemplate.go:71:1: exported method NameTemplate.SetTemplate should have comment or be unexported
middleware/kubernetes/nametemplate/nametemplate.go:108:1: exported method NameTemplate.GetZoneFromSegmentArray should have comment or be unexported
middleware/kubernetes/nametemplate/nametemplate.go:116:1: exported method NameTemplate.GetNamespaceFromSegmentArray should have comment or be unexported
middleware/kubernetes/nametemplate/nametemplate.go:120:1: exported method NameTemplate.GetServiceFromSegmentArray should have comment or be unexported
middleware/kubernetes/nametemplate/nametemplate.go:124:1: exported method NameTemplate.GetTypeFromSegmentArray should have comment or be unexported
middleware/kubernetes/nametemplate/nametemplate.go:135:1: exported method NameTemplate.GetSymbolFromSegmentArray should have comment or be unexported
middleware/kubernetes/nametemplate/nametemplate.go:167:1: exported method NameTemplate.IsValid should have comment or be unexported
middleware/kubernetes/nametemplate/nametemplate.go:182:6: exported type NameValues should have comment or be unexported
middleware/kubernetes/util/util.go:1:1: package comment should be of the form "Package util ..."
middleware/kubernetes/util/util.go:27:2: exported const WildcardStar should have comment (or a comment on this block) or be unexported
middleware/proxy/lookup.go:66:1: exported method Proxy.Forward should have comment or be unexported
middleware/proxy/proxy.go:24:6: exported type Client should have comment or be unexported
middleware/proxy/proxy.go:107:1: exported function Clients should have comment or be unexported
middleware/proxy/reverseproxy.go:10:6: exported type ReverseProxy should have comment or be unexported
middleware/proxy/reverseproxy.go:16:1: exported method ReverseProxy.ServeDNS should have comment or be unexported
middleware/proxy/upstream.go:42:6: exported type Options should have comment or be unexported
~~~

I plan on reworking the proxy anyway, so I'll leave that be.
2016-09-23 09:14:12 +01:00

143 lines
3 KiB
Go

package file
import (
"fmt"
"net"
"os"
"github.com/miekg/coredns/core/dnsserver"
"github.com/miekg/coredns/middleware"
"github.com/mholt/caddy"
)
func init() {
caddy.RegisterPlugin("file", caddy.Plugin{
ServerType: "dns",
Action: setup,
})
}
func setup(c *caddy.Controller) error {
zones, err := fileParse(c)
if err != nil {
return middleware.Error("file", err)
}
// Add startup functions to notify the master(s).
for _, n := range zones.Names {
c.OnStartup(func() error {
zones.Z[n].StartupOnce.Do(func() {
if len(zones.Z[n].TransferTo) > 0 {
zones.Z[n].Notify()
}
zones.Z[n].Reload(nil)
})
return nil
})
}
dnsserver.GetConfig(c).AddMiddleware(func(next middleware.Handler) middleware.Handler {
return File{Next: next, Zones: zones}
})
return nil
}
func fileParse(c *caddy.Controller) (Zones, error) {
z := make(map[string]*Zone)
names := []string{}
origins := []string{}
for c.Next() {
if c.Val() == "file" {
// file db.file [zones...]
if !c.NextArg() {
return Zones{}, c.ArgErr()
}
fileName := c.Val()
origins = make([]string, len(c.ServerBlockKeys))
copy(origins, c.ServerBlockKeys)
args := c.RemainingArgs()
if len(args) > 0 {
origins = args
}
reader, err := os.Open(fileName)
if err != nil {
// bail out
return Zones{}, err
}
for i := range origins {
origins[i] = middleware.Host(origins[i]).Normalize()
zone, err := Parse(reader, origins[i], fileName)
if err == nil {
z[origins[i]] = zone
} else {
return Zones{}, err
}
names = append(names, origins[i])
}
noReload := false
for c.NextBlock() {
t, _, e := TransferParse(c)
if e != nil {
return Zones{}, e
}
switch c.Val() {
case "no_reload":
noReload = true
}
// discard from, here, maybe check and show log when we do?
for _, origin := range origins {
if t != nil {
z[origin].TransferTo = append(z[origin].TransferTo, t...)
}
z[origin].NoReload = noReload
}
}
}
}
return Zones{Z: z, Names: names}, nil
}
// TransferParse parses transfer statements: 'transfer to [address...]'.
// Exported so secondary can use this as well.
func TransferParse(c *caddy.Controller) (tos, froms []string, err error) {
what := c.Val()
if !c.NextArg() {
return nil, nil, c.ArgErr()
}
value := c.Val()
switch what {
case "transfer":
if value == "to" {
tos = c.RemainingArgs()
for i := range tos {
if tos[i] != "*" {
if x := net.ParseIP(tos[i]); x == nil {
return nil, nil, fmt.Errorf("must specify an IP addres: `%s'", tos[i])
}
tos[i] = middleware.Addr(tos[i]).Normalize()
}
}
}
if value == "from" {
froms = c.RemainingArgs()
for i := range froms {
if froms[i] != "*" {
if x := net.ParseIP(froms[i]); x == nil {
return nil, nil, fmt.Errorf("must specify an IP addres: `%s'", froms[i])
}
froms[i] = middleware.Addr(froms[i]).Normalize()
} else {
return nil, nil, fmt.Errorf("can't use '*' in transfer from")
}
}
}
}
return
}