libnetwork: refactor rule management to use Ensure method for Append and Insert operations

Signed-off-by: Andrey Epifanov <aepifanov@mirantis.com>
This commit is contained in:
Andrey Epifanov
2025-05-29 02:15:48 -07:00
committed by Cory Snider
parent 19a8083866
commit 262c32565b

View File

@@ -467,22 +467,25 @@ func (r Rule) WithChain(chain string) Rule {
return wc
}
// Append appends the rule to the end of the chain. If the rule already exists anywhere in the
// ensure appends/insert the rule to the end of the chain. If the rule already exists anywhere in the
// chain, this is a no-op.
func (r Rule) Append() error {
func (r Rule) ensure(op Action) error {
if r.Exists() {
return nil
}
return r.exec(Append)
return r.exec(op)
}
// Append appends the rule to the end of the chain. If the rule already exists anywhere in the
// chain, this is a no-op.
func (r Rule) Append() error {
return r.ensure(Append)
}
// Insert inserts the rule at the head of the chain. If the rule already exists anywhere in the
// chain, this is a no-op.
func (r Rule) Insert() error {
if r.Exists() {
return nil
}
return r.exec(Insert)
return r.ensure(Insert)
}
// Delete deletes the rule from the kernel. If the rule does not exist, this is a no-op.