Skip to content

Commit

Permalink
Move duplicate logic to iptRule.Exists method (code health)
Browse files Browse the repository at this point in the history
Signed-off-by: Richard Hansen <rhansen@rhansen.org>
  • Loading branch information
rhansen committed Oct 14, 2023
1 parent 14d2535 commit e260808
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
7 changes: 6 additions & 1 deletion libnetwork/drivers/bridge/setup_ip_tables_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ type iptRule struct {
args []string
}

// Exists returns true if the rule exists in the kernel.
func (r iptRule) Exists() bool {
return iptables.GetIptable(r.ipv).Exists(r.table, r.chain, r.args...)
}

func setupIPTablesInternal(ipVer iptables.IPVersion, config *networkConfiguration, addr *net.IPNet, hairpin, enable bool) error {
var (
address = addr.String()
Expand Down Expand Up @@ -258,7 +263,7 @@ func programChainRule(rule iptRule, ruleDescr string, insert bool) error {
var (
operation string
condition bool
doesExist = iptable.Exists(rule.table, rule.chain, rule.args...)
doesExist = rule.Exists()
)

args := []string{"-t", string(rule.table)}
Expand Down
5 changes: 2 additions & 3 deletions libnetwork/drivers/bridge/setup_ip_tables_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,15 @@ func assertIPTableChainProgramming(rule iptRule, descr string, t *testing.T) {
t.Fatalf("Failed to program iptable rule %s: %s", descr, err.Error())
}

iptable := iptables.GetIptable(rule.ipv)
if iptable.Exists(rule.table, rule.chain, rule.args...) == false {
if !rule.Exists() {
t.Fatalf("Failed to effectively program iptable rule: %s", descr)
}

// Remove
if err := programChainRule(rule, descr, false); err != nil {
t.Fatalf("Failed to remove iptable rule %s: %s", descr, err.Error())
}
if iptable.Exists(rule.table, rule.chain, rule.args...) == true {
if rule.Exists() {
t.Fatalf("Failed to effectively remove iptable rule: %s", descr)
}
}
Expand Down

0 comments on commit e260808

Please sign in to comment.