coredns/plugin/loadbalance/setup.go
Miek Gieben 004c5fca9d
all: simply registering plugins (#3287)
Abstract the caddy call and make it simpler.

See #3261 for some part of the discussion.

Go from:

~~~ go
func init() {
       caddy.RegisterPlugin("any", caddy.Plugin{
               ServerType: "dns",
               Action:     setup,
       })
}
~~~

To:

~~~ go
func init() { plugin.Register("any", setup) }
~~~

This requires some external documents in coredns.io to be updated as
well; the old way still works, so it's backwards compatible.

Signed-off-by: Miek Gieben <miek@miek.nl>
2019-09-20 08:02:30 +01:00

45 lines
828 B
Go

package loadbalance
import (
"fmt"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/caddyserver/caddy"
)
var log = clog.NewWithPlugin("loadbalance")
func init() { plugin.Register("loadbalance", setup) }
func setup(c *caddy.Controller) error {
err := parse(c)
if err != nil {
return plugin.Error("loadbalance", err)
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
return RoundRobin{Next: next}
})
return nil
}
func parse(c *caddy.Controller) error {
for c.Next() {
args := c.RemainingArgs()
switch len(args) {
case 0:
return nil
case 1:
if args[0] != "round_robin" {
return fmt.Errorf("unknown policy: %s", args[0])
}
return nil
}
}
return c.ArgErr()
}