mirror of
https://github.com/moby/moby.git
synced 2026-08-03 22:51:03 +00:00
d/libn/i/nftables: add fluent chain builder API
With most of the dynamism of nftables rulesets being powered by named maps and sets, the rules of a chain are often initialized when the chain is added, and never touched again. Add a fluent API for adding the creation of a chain and all its rules to a modifier without having to repeat the chain name for each rule. Signed-off-by: Cory Snider <csnider@mirantis.com>
This commit is contained in:
30
daemon/libnetwork/internal/nftables/fluentapi_linux.go
Normal file
30
daemon/libnetwork/internal/nftables/fluentapi_linux.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package nftables
|
||||
|
||||
type chainBuilder struct {
|
||||
chain string
|
||||
tm *Modifier
|
||||
}
|
||||
|
||||
func (b BaseChain) Builder() chainBuilder {
|
||||
tm := &Modifier{}
|
||||
tm.create(b, 1)
|
||||
return chainBuilder{chain: b.Name, tm: tm}
|
||||
}
|
||||
|
||||
func (c Chain) Builder() chainBuilder {
|
||||
tm := &Modifier{}
|
||||
tm.create(c, 1)
|
||||
return chainBuilder{chain: c.Name, tm: tm}
|
||||
}
|
||||
|
||||
func (b chainBuilder) Rule(rule ...string) chainBuilder {
|
||||
b.tm.create(Rule{
|
||||
Chain: b.chain,
|
||||
Rule: rule,
|
||||
}, 1)
|
||||
return b
|
||||
}
|
||||
|
||||
func (b chainBuilder) Create(tm *Modifier) {
|
||||
tm.cmds = append(tm.cmds, b.tm.cmds...)
|
||||
}
|
||||
@@ -490,7 +490,11 @@ type Modifier struct {
|
||||
|
||||
// Create enqueues creation of object o, to be applied by tm.Apply.
|
||||
func (tm *Modifier) Create(o Obj) {
|
||||
_, f, l, _ := runtime.Caller(1)
|
||||
tm.create(o, 1)
|
||||
}
|
||||
|
||||
func (tm *Modifier) create(o Obj, skipFrames int) {
|
||||
_, f, l, _ := runtime.Caller(skipFrames + 1)
|
||||
tm.cmds = append(tm.cmds, command{
|
||||
obj: o,
|
||||
callerFile: f,
|
||||
|
||||
Reference in New Issue
Block a user