coredns/plugin/view/setup.go
Chris O'Haver b56b080a7c
plugin/view: Advanced routing interface and new 'view' plugin (#5538)
* introduce new interface "dnsserver.Viewer", that allows a plugin implementing it to decide if a query should be routed into its server block.
* add new plugin "view", that uses the new interface to enable a user to define expression based conditions that must be met for a query to be routed to its server block.

Signed-off-by: Chris O'Haver <cohaver@infoblox.com>
2022-09-08 14:56:27 -04:00

65 lines
1.2 KiB
Go

package view
import (
"context"
"strings"
"github.com/coredns/caddy"
"github.com/coredns/coredns/core/dnsserver"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/pkg/expression"
"github.com/antonmedv/expr"
)
func init() { plugin.Register("view", setup) }
func setup(c *caddy.Controller) error {
cond, err := parse(c)
if err != nil {
return plugin.Error("view", err)
}
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
cond.Next = next
return cond
})
return nil
}
func parse(c *caddy.Controller) (*View, error) {
v := new(View)
i := 0
for c.Next() {
i++
if i > 1 {
return nil, plugin.ErrOnce
}
args := c.RemainingArgs()
if len(args) != 1 {
return nil, c.ArgErr()
}
v.viewName = args[0]
for c.NextBlock() {
switch c.Val() {
case "expr":
args := c.RemainingArgs()
prog, err := expr.Compile(strings.Join(args, " "), expr.Env(expression.DefaultEnv(context.Background(), nil)))
if err != nil {
return v, err
}
v.progs = append(v.progs, prog)
if err != nil {
return nil, err
}
continue
default:
return nil, c.Errf("unknown property '%s'", c.Val())
}
}
}
return v, nil
}