Skip to content

Commit 14b8a58

Browse files
hemeimeiclaude
andcommitted
portmap: make DNAT entries last-write-wins
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Signed-off-by: hemeimei <hemeimei@baidu.com>
1 parent 33cc6bd commit 14b8a58

2 files changed

Lines changed: 19 additions & 12 deletions

File tree

plugins/meta/portmap/portmap_iptables.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,15 @@ func genToplevelDnatChain() chain {
164164

165165
// genDnatChain creates the per-container chain.
166166
// Conditions are any static entry conditions for the chain.
167+
// Entry rules are prepended within CNI-HOSTPORT-DNAT so that the most recent
168+
// container using a host port wins without moving CNI-HOSTPORT-DNAT ahead of
169+
// Kubernetes service rules in PREROUTING/OUTPUT.
167170
func genDnatChain(netName, containerID string) chain {
168171
return chain{
169-
table: "nat",
170-
name: utils.MustFormatChainNameWithPrefix(netName, containerID, "DN-"),
171-
entryChains: []string{TopLevelDNATChainName},
172+
table: "nat",
173+
name: utils.MustFormatChainNameWithPrefix(netName, containerID, "DN-"),
174+
entryChains: []string{TopLevelDNATChainName},
175+
prependEntry: true,
172176
}
173177
}
174178

plugins/meta/portmap/portmap_iptables_test.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ var _ = Describe("portmapping configuration (iptables)", func() {
3838
ch := genDnatChain(netName, containerID)
3939

4040
Expect(ch).To(Equal(chain{
41-
table: "nat",
42-
name: "CNI-DN-bfd599665540dd91d5d28",
43-
entryChains: []string{TopLevelDNATChainName},
41+
table: "nat",
42+
name: "CNI-DN-bfd599665540dd91d5d28",
43+
entryChains: []string{TopLevelDNATChainName},
44+
prependEntry: true,
4445
}))
4546
configBytes := []byte(fmt.Sprintf(`{
4647
"name": "test",
@@ -69,9 +70,10 @@ var _ = Describe("portmapping configuration (iptables)", func() {
6970

7071
ch = genDnatChain(conf.Name, containerID)
7172
Expect(ch).To(Equal(chain{
72-
table: "nat",
73-
name: "CNI-DN-67e92b96e692a494b6b85",
74-
entryChains: []string{"CNI-HOSTPORT-DNAT"},
73+
table: "nat",
74+
name: "CNI-DN-67e92b96e692a494b6b85",
75+
entryChains: []string{"CNI-HOSTPORT-DNAT"},
76+
prependEntry: true,
7577
}))
7678

7779
n, err := types.ParseCIDR("10.0.0.2/24")
@@ -171,9 +173,10 @@ var _ = Describe("portmapping configuration (iptables)", func() {
171173
ch := genDnatChain(netName, containerID)
172174

173175
Expect(ch).To(Equal(chain{
174-
table: "nat",
175-
name: "CNI-DN-bfd599665540dd91d5d28",
176-
entryChains: []string{TopLevelDNATChainName},
176+
table: "nat",
177+
name: "CNI-DN-bfd599665540dd91d5d28",
178+
entryChains: []string{TopLevelDNATChainName},
179+
prependEntry: true,
177180
}))
178181
configBytes := []byte(fmt.Sprintf(`{
179182
"name": "test",

0 commit comments

Comments
 (0)