Browse code

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

Signed-off-by: Andrey Epifanov <aepifanov@mirantis.com>

Andrey Epifanov authored on 2025/05/29 18:15:48
Showing 1 changed files
... ...
@@ -467,22 +467,25 @@ func (r Rule) WithChain(chain string) Rule {
467 467
 	return wc
468 468
 }
469 469
 
470
-// Append appends the rule to the end of the chain. If the rule already exists anywhere in the
470
+// ensure appends/insert the rule to the end of the chain. If the rule already exists anywhere in the
471 471
 // chain, this is a no-op.
472
-func (r Rule) Append() error {
472
+func (r Rule) ensure(op Action) error {
473 473
 	if r.Exists() {
474 474
 		return nil
475 475
 	}
476
-	return r.exec(Append)
476
+	return r.exec(op)
477
+}
478
+
479
+// Append appends the rule to the end of the chain. If the rule already exists anywhere in the
480
+// chain, this is a no-op.
481
+func (r Rule) Append() error {
482
+	return r.ensure(Append)
477 483
 }
478 484
 
479 485
 // Insert inserts the rule at the head of the chain. If the rule already exists anywhere in the
480 486
 // chain, this is a no-op.
481 487
 func (r Rule) Insert() error {
482
-	if r.Exists() {
483
-		return nil
484
-	}
485
-	return r.exec(Insert)
488
+	return r.ensure(Insert)
486 489
 }
487 490
 
488 491
 // Delete deletes the rule from the kernel. If the rule does not exist, this is a no-op.