Move duplicate logic to iptRule.Exists method (code health)

Signed-off-by: Richard Hansen <rhansen@rhansen.org>
This commit is contained in:
Richard Hansen 2023-09-11 17:52:08 -04:00
parent 14d2535f13
commit e260808a57
2 changed files with 8 additions and 4 deletions

View file

@ -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()
@ -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)}

View file

@ -107,8 +107,7 @@ 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)
}
@ -116,7 +115,7 @@ func assertIPTableChainProgramming(rule iptRule, descr string, t *testing.T) {
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)
}
}