[management] Optimize affected posture checks and add logs#6522
Conversation
…trace-update-logs
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
✅ Files skipped from review due to trivial changes (1)
📝 WalkthroughWalkthroughThe PR updates peer location persistence, centralizes ChangesPeer geo, update gating, and controller tracing
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Release artifactsBuilt for PR head
GHCR images (amd64)No GHCR images were pushed. This comment is updated by the Release workflow. Artifact links expire according to the workflow retention policy. |
There was a problem hiding this comment.
Actionable comments posted: 3
🧹 Nitpick comments (1)
management/internals/controllers/network_map/controller/controller.go (1)
181-183: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick winGuard
debug.Stack()behind trace-level checks.
debug.Stack()allocates/captures even when trace logging is off. Add a trace-level guard so stack collection only happens when it can actually be emitted.💡 Proposed fix
- if reason.Operation == types.UpdateOperationUpdate && reason.Resource == types.UpdateResourceUser { + if reason.Operation == types.UpdateOperationUpdate && + reason.Resource == types.UpdateResourceUser && + log.IsLevelEnabled(log.TraceLevel) { log.WithContext(ctx).Tracef("got an user update, stack: %s", debug.Stack()) } @@ - if reason.Operation == types.UpdateOperationUpdate && reason.Resource == types.UpdateResourceUser { + if reason.Operation == types.UpdateOperationUpdate && + reason.Resource == types.UpdateResourceUser && + log.IsLevelEnabled(log.TraceLevel) { log.WithContext(ctx).Tracef("got an user update, stack: %s", debug.Stack()) }Also applies to: 506-510
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@management/internals/controllers/network_map/controller/controller.go` around lines 181 - 183, The debug.Stack() function is being called unconditionally in the trace log statement, which allocates memory and captures the stack even when trace logging is disabled. Add a trace-level check before calling debug.Stack() to ensure the expensive stack collection only occurs when trace logging is actually enabled. Guard the log statement containing debug.Stack() with a conditional check that verifies trace level logging is enabled (such as log.IsLevelEnabled() or similar), and apply this same pattern to all similar instances mentioned in the comment (around lines 506-510).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@management/server/peer.go`:
- Around line 1058-1060: Remove the redundant initial assignment of the reason
variable in the requiresPeerUpdate function. The reason variable is declared and
assigned an empty string on line 1059, but this value is never used because all
switch cases that reference reason overwrite it before use, and the default case
returns early. Delete the line `reason := ""` entirely since the variable will
be properly initialized in each switch case that needs it.
In `@management/server/posture/affects_test.go`:
- Around line 38-122: Add a new test case to the tests slice that verifies the
function properly evaluates multiple checks in a single slice without
short-circuiting. Create a test case where the checks slice contains a public
range check followed by another check type (such as a process check or geo
check) with a diff that should NOT affect the public range check but SHOULD
affect the second check. Set the expected result to true to ensure the function
continues evaluating all checks in the slice rather than returning early based
on the first check's result.
In `@management/server/posture/checks.go`:
- Around line 81-93: The issue is that the function returns false after checking
the PeerNetworkRangeCheck ranges when all ranges are public (line 92), which
prevents subsequent posture checks from being evaluated. Instead of returning
false immediately when no private ranges are found in the loop within the
PeerNetworkRangeCheck block, the code should continue to the next posture check.
Remove the return false statement after the range loop so that the function
continues to evaluate other posture check conditions, only returning false after
all applicable checks have been examined.
---
Nitpick comments:
In `@management/internals/controllers/network_map/controller/controller.go`:
- Around line 181-183: The debug.Stack() function is being called
unconditionally in the trace log statement, which allocates memory and captures
the stack even when trace logging is disabled. Add a trace-level check before
calling debug.Stack() to ensure the expensive stack collection only occurs when
trace logging is actually enabled. Guard the log statement containing
debug.Stack() with a conditional check that verifies trace level logging is
enabled (such as log.IsLevelEnabled() or similar), and apply this same pattern
to all similar instances mentioned in the comment (around lines 506-510).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: f40da02d-40f3-4f61-9cbe-1aaefcffed30
📒 Files selected for processing (5)
management/internals/controllers/network_map/controller/controller.gomanagement/server/peer.gomanagement/server/peer_test.gomanagement/server/posture/affects_test.gomanagement/server/posture/checks.go
|



Describe your changes
Issue ticket number and link
Stack
Checklist
Documentation
Select exactly one:
Docs PR URL (required if "docs added" is checked)
Paste the PR link from https://github.com/netbirdio/docs here:
https://github.com/netbirdio/docs/pull/__
Summary by CodeRabbit