portmap: fix iptables conditions detection

As show in the docs, iptables conditions can also start with '!'

Fixes 01a94e17c77e6ff8e5019e15c42d8d92cf87194f

Signed-off-by: Etienne Champetier <e.champetier@ateme.com>
This commit is contained in:
Etienne Champetier 2024-11-05 17:24:40 -05:00 committed by Casey Callendrello
parent 3ffc42cdfd
commit 7f756b411e
2 changed files with 7 additions and 6 deletions

View File

@ -349,10 +349,11 @@ func detectBackendOfConditions(conditions *[]string) string {
return "" return ""
} }
// The first token of any iptables condition would start with a hyphen (e.g. "-d", // The first character of any iptables condition would either be an hyphen
// "--sport", "-m"). No nftables condition would start that way. (An nftables // (e.g. "-d", "--sport", "-m") or an exclamation mark.
// condition might include a negative number, but not as the first token.) // No nftables condition would start that way. (An nftables condition might
if (*conditions)[0][0] == '-' { // include a negative number, but not as the first token.)
if (*conditions)[0][0] == '-' || (*conditions)[0][0] == '!' {
return iptablesBackend return iptablesBackend
} }
return nftablesBackend return nftablesBackend

View File

@ -44,7 +44,7 @@ var _ = Describe("portmapping configuration", func() {
}, },
"snat": false, "snat": false,
"conditionsV4": ["-s", "1.2.3.4"], "conditionsV4": ["-s", "1.2.3.4"],
"conditionsV6": ["-s", "12::34"], "conditionsV6": ["!", "-s", "12::34"],
"prevResult": { "prevResult": {
"interfaces": [ "interfaces": [
{"name": "host"}, {"name": "host"},
@ -76,7 +76,7 @@ var _ = Describe("portmapping configuration", func() {
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(c.CNIVersion).To(Equal(ver)) Expect(c.CNIVersion).To(Equal(ver))
Expect(c.ConditionsV4).To(Equal(&[]string{"-s", "1.2.3.4"})) Expect(c.ConditionsV4).To(Equal(&[]string{"-s", "1.2.3.4"}))
Expect(c.ConditionsV6).To(Equal(&[]string{"-s", "12::34"})) Expect(c.ConditionsV6).To(Equal(&[]string{"!", "-s", "12::34"}))
fvar := false fvar := false
Expect(c.SNAT).To(Equal(&fvar)) Expect(c.SNAT).To(Equal(&fvar))
Expect(c.Name).To(Equal("test")) Expect(c.Name).To(Equal("test"))