-
Notifications
You must be signed in to change notification settings - Fork 344
fix(envd): self-heal MMDS routing on /init lookup failure #2701
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
37c4de7
fix(envd): self-heal MMDS routing on /init MMDS lookup failure
ValentaTomas b425dcf
use direct PREROUTING/OUTPUT RETURN rules for MMDS pin
ValentaTomas f3796b6
add -w to iptables for xtables lock contention
ValentaTomas b139b81
bump envd to 0.5.24 + correct self-heal comment
ValentaTomas 361598d
surface iptables -I failures from MMDS pin
ValentaTomas f7256d4
rate-limit MMDS pin failure warn to 1/10s
ValentaTomas bd2a127
reuse shared rate limiter; bound MMDS poller per-tick
ValentaTomas c6ee5e0
serialize MMDS pin to avoid parallel iptables runs
ValentaTomas 9232385
use semaphore.Weighted for MMDS pin coalescing
ValentaTomas 5afc45e
Merge remote-tracking branch 'origin/main' into fix/envd-mmds-route-pin
ValentaTomas 6589bfb
chore(envd): bump version to 0.5.25
arkamar 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| //go:build linux | ||
|
|
||
| package host | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os/exec" | ||
|
|
||
| "golang.org/x/sync/semaphore" | ||
| ) | ||
|
|
||
| // pinMMDSSem serializes self-heal calls so concurrent /init retries don't | ||
| // run iptables in parallel against the same nat table. | ||
| var pinMMDSSem = semaphore.NewWeighted(1) | ||
|
|
||
| // PinMMDSRoute pins a RETURN rule for MMDS traffic (169.254.169.254:80) at | ||
| // position 1 of nat PREROUTING and OUTPUT. Idempotent: each run deletes any | ||
| // existing copy of the rule first, then re-inserts at position 1, so user | ||
| // rules added above ours get pushed down. | ||
| // | ||
| // Intended for the self-heal path: only called when a real MMDS lookup | ||
| // fails. Concurrent callers are coalesced via a semaphore — only one runs | ||
| // at a time, the rest return nil immediately. Returns the first -I failure | ||
| // (if any); -D failures are expected (rule absent on first run) and | ||
| // silently swallowed. | ||
| func PinMMDSRoute(ctx context.Context) error { | ||
| if !pinMMDSSem.TryAcquire(1) { | ||
| return nil | ||
| } | ||
| defer pinMMDSSem.Release(1) | ||
|
|
||
| rule := []string{"-d", "169.254.169.254", "-p", "tcp", "--dport", "80", "-j", "RETURN"} | ||
| for _, chain := range []string{"PREROUTING", "OUTPUT"} { | ||
| // -D fails when the rule is absent (exit 1, expected on first run); | ||
| // nothing actionable to log. | ||
| _ = iptables(ctx, append([]string{"-D", chain}, rule...)...) | ||
| if err := iptables(ctx, append([]string{"-I", chain, "1"}, rule...)...); err != nil { | ||
| return fmt.Errorf("iptables -I nat %s: %w", chain, err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // iptables runs `iptables -w 5 -t nat ...`. -w waits up to 5s for the | ||
| // xtables lock (a user iptables process may race us). | ||
| func iptables(ctx context.Context, args ...string) error { | ||
| full := append([]string{"-w", "5", "-t", "nat"}, args...) | ||
| out, err := exec.CommandContext(ctx, "iptables", full...).CombinedOutput() | ||
| if err != nil { | ||
| return fmt.Errorf("%w: %s", err, out) | ||
| } | ||
|
|
||
| return nil | ||
| } |
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,7 @@ | ||
| //go:build !linux | ||
|
|
||
| package host | ||
|
|
||
| import "context" | ||
|
|
||
| func PinMMDSRoute(_ context.Context) error { return nil } |
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,39 @@ | ||
| package ratelimit | ||
|
|
||
| import ( | ||
| "sync/atomic" | ||
| "time" | ||
| ) | ||
|
|
||
| // Limiter gates a recurring log to at most one emit per `floor`, counting | ||
| // suppressed attempts in between. The caller decides how to format/emit; | ||
| // this type only owns the gating decision. | ||
| type Limiter struct { | ||
| floor time.Duration | ||
| lastLogged atomic.Pointer[time.Time] | ||
| suppressed atomic.Int64 | ||
| } | ||
|
|
||
| func New(floor time.Duration) *Limiter { | ||
| return &Limiter{floor: floor} | ||
| } | ||
|
|
||
| // Allow returns (true, suppressedSinceLast) when the caller should emit a | ||
| // log line; false otherwise. On true the caller should include | ||
| // `suppressedSinceLast` in the emitted message. | ||
| func (r *Limiter) Allow() (bool, int64) { | ||
| last := r.lastLogged.Load() | ||
| if last != nil && time.Since(*last) <= r.floor { | ||
| r.suppressed.Add(1) | ||
|
|
||
| return false, 0 | ||
| } | ||
| now := time.Now() | ||
| if !r.lastLogged.CompareAndSwap(last, &now) { | ||
| r.suppressed.Add(1) | ||
|
|
||
| return false, 0 | ||
| } | ||
|
|
||
| return true, r.suppressed.Swap(0) | ||
| } |
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 |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| package pkg | ||
|
|
||
| const Version = "0.5.24" | ||
| const Version = "0.5.25" |
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.
Uh oh!
There was an error while loading. Please reload this page.