-
Notifications
You must be signed in to change notification settings - Fork 25
feat(ipv6): add --disable-ipv6 flag for IPv4-only operation #142
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3c3489f
feat(ipv6): add --disable-ipv6 flag for IPv4-only operation
stevensbkang 346a879
fix(networking): own pod egress masquerade to fix agent connectivity …
stevensbkang ab62b03
feat(config): add --full flag and strip redundant upstream defaults (…
stevensbkang b723d84
chore: v1.1.4 bump (#144)
stevensbkang 506c067
preserve registry config (#147)
xiezhuojin 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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| package network | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "strings" | ||
|
|
||
| "github.com/portainer/kubesolo/types" | ||
| "github.com/rs/zerolog/log" | ||
| ) | ||
|
|
||
| const masqueradeComment = "kubesolo: pod masquerade" | ||
|
|
||
| // EnsurePodMasquerade programs a SNAT/masquerade rule for pod egress traffic so | ||
| // pods can reach external IPs. It is idempotent and mode-aware: iptables on | ||
| // systems that have the ip_tables kernel module, nftables otherwise. | ||
| // | ||
| // This must be called before kubelet starts (so no pod ever starts without SNAT | ||
| // in place) and after flushNftablesNat (to restore the rule after the nat table | ||
| // is flushed for kube-proxy compatibility). | ||
| func EnsurePodMasquerade(podCIDR string) error { | ||
| if _, err := os.Stat("/proc/net/ip_tables_names"); err == nil { | ||
| return ensureIPTablesMasquerade(podCIDR) | ||
| } | ||
| return ensureNftablesMasquerade(podCIDR) | ||
| } | ||
|
|
||
| func ensureIPTablesMasquerade(podCIDR string) error { | ||
| // -w 5: wait up to 5 s for the xtables lock. This function is called during | ||
| // kube-proxy startup when other processes may also be modifying iptables; | ||
| // without -w, concurrent access causes "Resource temporarily unavailable". | ||
| args := []string{ | ||
| "-w", "5", | ||
| "-t", "nat", "-C", "POSTROUTING", | ||
| "-s", podCIDR, "!", "-d", podCIDR, | ||
| "-m", "comment", "--comment", masqueradeComment, | ||
| "-j", "MASQUERADE", | ||
| } | ||
|
|
||
| if err := exec.Command("iptables", args...).Run(); err == nil { | ||
| log.Debug().Str("component", "network").Msg("pod masquerade rule already present (iptables)") | ||
| return nil | ||
| } | ||
|
|
||
| args[4] = "-A" | ||
| if out, err := exec.Command("iptables", args...).CombinedOutput(); err != nil { | ||
| return fmt.Errorf("iptables: failed to add pod masquerade rule: %v (output: %s)", err, out) | ||
| } | ||
|
|
||
| log.Info().Str("component", "network"). | ||
| Str("cidr", podCIDR). | ||
| Msg("added pod masquerade rule (iptables)") | ||
| return nil | ||
| } | ||
|
|
||
| func nftCombinedOutput(args ...string) ([]byte, error) { | ||
| return exec.Command("nft", args...).CombinedOutput() | ||
| } | ||
|
|
||
| func nftAlreadyExists(out []byte) bool { | ||
| msg := strings.ToLower(string(out)) | ||
| return strings.Contains(msg, "file exists") || strings.Contains(msg, "already exists") | ||
| } | ||
|
|
||
| // ensureNftObject ensures an nftables object (table or chain) exists. It tries | ||
| // listArgs first; if the object is absent it runs addArgs, tolerating a | ||
| // concurrent creation racing us to it. | ||
| func ensureNftObject(listArgs, addArgs []string) error { | ||
| if _, err := nftCombinedOutput(listArgs...); err == nil { | ||
| return nil | ||
| } | ||
| if out, err := nftCombinedOutput(addArgs...); err != nil && !nftAlreadyExists(out) { | ||
| return fmt.Errorf("nft %s: %v (output: %s)", strings.Join(addArgs, " "), err, out) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func ensureNftablesMasquerade(podCIDR string) error { | ||
| if err := ensureNftObject( | ||
| []string{"list", "table", "ip", types.DefaultNftMasqTable}, | ||
| []string{"add", "table", "ip", types.DefaultNftMasqTable}, | ||
| ); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := ensureNftObject( | ||
| []string{"list", "chain", "ip", types.DefaultNftMasqTable, "postrouting"}, | ||
| []string{"add", "chain", "ip", types.DefaultNftMasqTable, "postrouting", | ||
| "{ type nat hook postrouting priority srcnat; policy accept; }"}, | ||
| ); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| out, err := nftCombinedOutput("list", "chain", "ip", types.DefaultNftMasqTable, "postrouting") | ||
| if err != nil { | ||
| return fmt.Errorf("nft list chain ip %s postrouting: %v (output: %s)", types.DefaultNftMasqTable, err, out) | ||
| } | ||
| if strings.Contains(string(out), masqueradeComment) { | ||
| log.Debug().Str("component", "network").Msg("pod masquerade rule already present (nftables)") | ||
| return nil | ||
| } | ||
|
|
||
| args := []string{ | ||
| "add", "rule", "ip", types.DefaultNftMasqTable, "postrouting", | ||
| "ip", "saddr", podCIDR, "ip", "daddr", "!=", podCIDR, | ||
| "masquerade", "comment", `"` + masqueradeComment + `"`, | ||
| } | ||
| if out, err := nftCombinedOutput(args...); err != nil { | ||
| return fmt.Errorf("nft %s: %v (output: %s)", strings.Join(args, " "), err, out) | ||
| } | ||
|
|
||
| log.Info().Str("component", "network"). | ||
| Str("cidr", podCIDR). | ||
| Str("table", types.DefaultNftMasqTable). | ||
| Msg("added pod masquerade rule (nftables)") | ||
| return nil | ||
| } |
Oops, something went wrong.
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.
The flag help text says kubelet will only register with an explicit IPv4 node address when this flag is set, but the current kubelet args unconditionally set
--node-ip. Either make kubelet’s--node-ipconditional onDisableIPv6, or adjust this description to match the actual behavior.