Skip to content

Commit

Permalink
portmap: fix iptables conditions detection
Browse files Browse the repository at this point in the history
As show in the docs, iptables conditions can also start with '!'

Fixes 01a94e1

Signed-off-by: Etienne Champetier <[email protected]>
  • Loading branch information
champtar committed Nov 21, 2024
1 parent 6de8a98 commit ccac6de
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
9 changes: 5 additions & 4 deletions plugins/meta/portmap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,10 +349,11 @@ func detectBackendOfConditions(conditions *[]string) string {
return ""
}

// The first token of any iptables condition would start with a hyphen (e.g. "-d",
// "--sport", "-m"). No nftables condition would start that way. (An nftables
// condition might include a negative number, but not as the first token.)
if (*conditions)[0][0] == '-' {
// The first character of any iptables condition would either be an hyphen
// (e.g. "-d", "--sport", "-m") or an exclamation mark.
// No nftables condition would start that way. (An nftables condition might
// include a negative number, but not as the first token.)
if (*conditions)[0][0] == '-' || (*conditions)[0][0] == '!' {
return iptablesBackend
}
return nftablesBackend
Expand Down
4 changes: 2 additions & 2 deletions plugins/meta/portmap/portmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var _ = Describe("portmapping configuration", func() {
},
"snat": false,
"conditionsV4": ["-s", "1.2.3.4"],
"conditionsV6": ["-s", "12::34"],
"conditionsV6": ["!", "-s", "12::34"],
"prevResult": {
"interfaces": [
{"name": "host"},
Expand Down Expand Up @@ -76,7 +76,7 @@ var _ = Describe("portmapping configuration", func() {
Expect(err).NotTo(HaveOccurred())
Expect(c.CNIVersion).To(Equal(ver))
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
Expect(c.SNAT).To(Equal(&fvar))
Expect(c.Name).To(Equal("test"))
Expand Down

0 comments on commit ccac6de

Please sign in to comment.