Skip to content

chore(be): Refactoring for cleanup of the Detect interfaces#19431

Draft
clickboo wants to merge 1 commit intomasterfrom
boo-refactor-cleanup-detect-interface
Draft

chore(be): Refactoring for cleanup of the Detect interfaces#19431
clickboo wants to merge 1 commit intomasterfrom
boo-refactor-cleanup-detect-interface

Conversation

@clickboo
Copy link
Contributor

@clickboo clickboo commented Mar 16, 2026

Description

A refactor to clean up the Detect interface to no longer requiring two confusing "contexts".

User-facing documentation

Testing and quality

  • the change is production ready: the change is GA, or otherwise the functionality is gated by a feature flag
  • CI results are inspected

Automated testing

  • added unit tests
  • added e2e tests
  • added regression tests
  • added compatibility tests
  • modified existing tests

How I validated my change

CI only (verified that all 3 detection paths - central, sensor and admission controller are covered).

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@clickboo clickboo force-pushed the boo-refactor-cleanup-detect-interface branch from 469ca5a to 4c2f35f Compare March 16, 2026 09:46
@clickboo
Copy link
Contributor Author

/test all

@rhacs-bot
Copy link
Contributor

rhacs-bot commented Mar 16, 2026

Images are ready for the commit at 4c2f35f.

To use with deploy scripts, first export MAIN_IMAGE_TAG=4.11.x-326-g4c2f35f64f.

@codecov
Copy link

codecov bot commented Mar 16, 2026

Codecov Report

❌ Patch coverage is 0% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 49.69%. Comparing base (846febd) to head (4c2f35f).
⚠️ Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
central/detection/service/service_impl.go 0.00% 7 Missing ⚠️
central/detection/deploytime/detector_impl.go 0.00% 2 Missing ⚠️
...r/admission-control/manager/evaluate_deploytime.go 0.00% 2 Missing ⚠️
sensor/common/detector/detector.go 0.00% 1 Missing ⚠️
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     
Flag Coverage Δ
go-unit-tests 49.69% <0.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants