mirror of
https://github.com/coredns/coredns.git
synced 2025-12-08 19:35:10 -05:00
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>
This commit is contained in:
65
plugin/view/setup.go
Normal file
65
plugin/view/setup.go
Normal file
@@ -0,0 +1,65 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user