chore(be): Refactoring for cleanup of the Detect interfaces#19431
Draft
chore(be): Refactoring for cleanup of the Detect interfaces#19431
Conversation
Contributor
There was a problem hiding this comment.
Hey - I've found 3 issues
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="pkg/detection/deploytime/detector_impl.go" line_range="21-26" />
<code_context>
// Detect runs detection on a deployment, returning any generated alerts.
-func (d *detectorImpl) Detect(goctx context.Context, ctx DetectionContext, enhancedDeployment booleanpolicy.EnhancedDeployment, filters ...detection.FilterOption) ([]*storage.Alert, error) {
+func (d *detectorImpl) Detect(ctx context.Context, enhancedDeployment booleanpolicy.EnhancedDeployment, opts ...DetectOption) ([]*storage.Alert, error) {
+ var cfg detectionConfig
+ for _, o := range opts {
+ o(&cfg)
+ }
</code_context>
<issue_to_address>
**suggestion (bug_risk):** Guard against nil DetectOptions to avoid potential panics when options are built dynamically
With the variadic `DetectOption` signature, callers can easily pass `nil` (e.g., from conditional appends), which would cause a panic when invoked. Please update the loop to skip nil options:
```go
for _, o := range opts {
if o == nil {
continue
}
o(&cfg)
}
```
```suggestion
// Detect runs detection on a deployment, returning any generated alerts.
func (d *detectorImpl) Detect(ctx context.Context, enhancedDeployment booleanpolicy.EnhancedDeployment, opts ...DetectOption) ([]*storage.Alert, error) {
var cfg detectionConfig
for _, o := range opts {
if o == nil {
continue
}
o(&cfg)
}
```
</issue_to_address>
### Comment 2
<location path="central/detection/deploytime/default_policies_bench_test.go" line_range="34" />
<code_context>
for b.Loop() {
- _, err := detection.Detect(context.Background(), deploytime.DetectionContext{}, booleanpolicy.EnhancedDeployment{
+ _, err := detection.Detect(context.Background(), booleanpolicy.EnhancedDeployment{
Deployment: dep,
Images: images,
</code_context>
<issue_to_address>
**issue (testing):** Add focused unit tests for the new deploytime.Detect options API (enforcement-only and policy filters).
The benchmark now uses the new `Detect` signature, but there are no unit tests that cover `DetectOption` behavior (`WithEnforcementOnly`, `WithPolicyFilters`) or confirm parity with the old `DetectionContext` + filters. Please add tests in the deploytime detection package (and/or central detector) that:
- Confirm `WithEnforcementOnly()` suppresses alerts for `UNSET_ENFORCEMENT` policies while the default (no option) still emits them, matching previous `EnforcementOnly=false` behavior.
- Validate `WithPolicyFilters()` applies all filters correctly, including order and short-circuit behavior.
- Verify central and sensor callers (admission-control, unified detector, etc.) still produce the same alerts as before, particularly for enforcement-only flows.
This will ensure the new options-based API is properly covered and that detection semantics remain unchanged by the refactor.
</issue_to_address>
### Comment 3
<location path="pkg/detection/deploytime/detector.go" line_range="37" />
<code_context>
PolicySet() detection.PolicySet
- Detect(goctx context.Context, ctx DetectionContext, enhancedDeployment booleanpolicy.EnhancedDeployment, policyFilters ...detection.FilterOption) ([]*storage.Alert, error)
+ Detect(ctx context.Context, enhancedDeployment booleanpolicy.EnhancedDeployment, opts ...DetectOption) ([]*storage.Alert, error)
}
</code_context>
<issue_to_address>
**issue (complexity):** Consider simplifying the new Detect API changes by minimizing configuration surface and keeping the function signature straightforward.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
469ca5a to
4c2f35f
Compare
Contributor
Author
|
/test all |
Contributor
|
Images are ready for the commit at 4c2f35f. To use with deploy scripts, first |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #19431 +/- ##
=======================================
Coverage 49.69% 49.69%
=======================================
Files 2702 2702
Lines 203538 203535 -3
=======================================
+ Hits 101155 101156 +1
+ Misses 94856 94853 -3
+ Partials 7527 7526 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
A refactor to clean up the
Detectinterface to no longer requiring two confusing "contexts".User-facing documentation
Testing and quality
Automated testing
How I validated my change
CI only (verified that all 3 detection paths - central, sensor and admission controller are covered).