-
Notifications
You must be signed in to change notification settings - Fork 795
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
portmap: fix nftables backend #1116
Merged
squeed
merged 1 commit into
containernetworking:main
from
champtar:portmap-nftables-prerouting-hook
Nov 18, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import ( | |
"context" | ||
"fmt" | ||
"net" | ||
"strconv" | ||
|
||
"sigs.k8s.io/knftables" | ||
) | ||
|
@@ -110,23 +111,23 @@ func (pmNFT *portMapperNFTables) forwardPorts(config *PortMapConf, containerNet | |
}) | ||
|
||
tx.Add(&knftables.Chain{ | ||
Name: "input", | ||
Name: "prerouting", | ||
Type: knftables.PtrTo(knftables.NATType), | ||
Hook: knftables.PtrTo(knftables.InputHook), | ||
Hook: knftables.PtrTo(knftables.PreroutingHook), | ||
Priority: knftables.PtrTo(knftables.DNATPriority), | ||
}) | ||
tx.Flush(&knftables.Chain{ | ||
Name: "input", | ||
Name: "prerouting", | ||
}) | ||
tx.Add(&knftables.Rule{ | ||
Chain: "input", | ||
Chain: "prerouting", | ||
Rule: knftables.Concat( | ||
conditions, | ||
"jump", hostIPHostPortsChain, | ||
), | ||
}) | ||
tx.Add(&knftables.Rule{ | ||
Chain: "input", | ||
Chain: "prerouting", | ||
Rule: knftables.Concat( | ||
conditions, | ||
"jump", hostPortsChain, | ||
|
@@ -187,19 +188,17 @@ func (pmNFT *portMapperNFTables) forwardPorts(config *PortMapConf, containerNet | |
Chain: hostIPHostPortsChain, | ||
Rule: knftables.Concat( | ||
ipX, "daddr", e.HostIP, | ||
ipX, "protocol", e.Protocol, | ||
"th dport", e.HostPort, | ||
"dnat", ipX, "addr . port", "to", containerNet.IP, ".", e.ContainerPort, | ||
e.Protocol, "dport", e.HostPort, | ||
"dnat to", net.JoinHostPort(containerNet.IP.String(), strconv.Itoa(e.ContainerPort)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (yeah, this rule was complicated because it's derived from a kube-proxy rule where we look up the "addr . protocol . port" in a map rather than just having a static rule) |
||
), | ||
Comment: &config.ContainerID, | ||
}) | ||
} else { | ||
tx.Add(&knftables.Rule{ | ||
Chain: hostPortsChain, | ||
Rule: knftables.Concat( | ||
ipX, "protocol", e.Protocol, | ||
"th dport", e.HostPort, | ||
"dnat", ipX, "addr . port", "to", containerNet.IP, ".", e.ContainerPort, | ||
e.Protocol, "dport", e.HostPort, | ||
"dnat to", net.JoinHostPort(containerNet.IP.String(), strconv.Itoa(e.ContainerPort)), | ||
), | ||
Comment: &config.ContainerID, | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I think I know what I was confused about: in
prerouting
it hasn't yet decided where it's going to route the packet to, so you can't useoifname
. I was thinking that meant you couldn't usefib daddr type
either, but that's wrong;fib daddr type
answers "what does the routing table say we should do with this packet?", not "what are we actually going to do with this packet?", so it doesn't depend on the routing decision.