Skip to content

fix(kubernetes): apply namespace selector filtering to List operations#8312

Merged
cnvergence merged 10 commits intoenvoyproxy:mainfrom
shahar-h:fix-8305
Feb 25, 2026
Merged

fix(kubernetes): apply namespace selector filtering to List operations#8312
cnvergence merged 10 commits intoenvoyproxy:mainfrom
shahar-h:fix-8305

Conversation

@shahar-h
Copy link
Copy Markdown
Contributor

@shahar-h shahar-h commented Feb 19, 2026

What this PR does / why we need it:
When using the Kubernetes provider with NamespaceSelector watch mode, namespace filtering after client.List operations was implemented for some resources (Gateway, xRoute, ReferenceGrant) but was missing for xPolicy resources (SecurityPolicy, BackendTrafficPolicy, ClientTrafficPolicy, etc.). This caused xPolicy resources from all namespaces to be processed during reconciliation, even when those namespaces didn't match the configured selector.

This PR centralizes the namespace filtering logic by introducing a namespaceSelectorClient wrapper that automatically filters List results. This approach:

  • Eliminates scattered namespace filtering checks throughout the codebase
  • Ensures consistent filtering for all resource types including xPolicies
  • Excludes cluster-scoped resources (e.g., GatewayClass) from filtering since they have no namespace

Note: Watch predicates in watchResources still have their own namespace filtering checks for filtering incoming events.

Which issue(s) this PR fixes:
Fixes #8305

Release Notes: Yes

Signed-off-by: Shahar Harari <shahar.harari@sap.com>
Signed-off-by: Shahar Harari <shahar.harari@sap.com>
Signed-off-by: Shahar Harari <shahar.harari@sap.com>
Signed-off-by: Shahar Harari <shahar.harari@sap.com>
Signed-off-by: Shahar Harari <shahar.harari@sap.com>
@shahar-h shahar-h requested a review from a team as a code owner February 19, 2026 13:46
@netlify
Copy link
Copy Markdown

netlify Bot commented Feb 19, 2026

Deploy Preview for cerulean-figolla-1f9435 ready!

Name Link
🔨 Latest commit d6a7202
🔍 Latest deploy log https://app.netlify.com/projects/cerulean-figolla-1f9435/deploys/699db292b7e186000863e1d0
😎 Deploy Preview https://deploy-preview-8312--cerulean-figolla-1f9435.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 19, 2026

Codecov Report

❌ Patch coverage is 85.45455% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.83%. Comparing base (26fa4e6) to head (d6a7202).

Files with missing lines Patch % Lines
...l/provider/kubernetes/namespace_selector_client.go 81.81% 4 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #8312      +/-   ##
==========================================
+ Coverage   73.73%   73.83%   +0.10%     
==========================================
  Files         241      242       +1     
  Lines       37077    37046      -31     
==========================================
+ Hits        27337    27353      +16     
+ Misses       7796     7761      -35     
+ Partials     1944     1932      -12     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Signed-off-by: Shahar Harari <shahar.harari@sap.com>
Signed-off-by: Shahar Harari <shahar.harari@sap.com>
@shahar-h
Copy link
Copy Markdown
Contributor Author

/retest

@zhaohuabing zhaohuabing added this to the v1.8.0-rc.1 Release milestone Feb 20, 2026
Comment thread internal/provider/kubernetes/predicates.go Outdated
Comment thread internal/provider/kubernetes/predicates.go
Comment thread internal/provider/kubernetes/predicates.go
Comment thread internal/provider/kubernetes/namespace_selector_client.go Outdated
}

// Set the filtered items back to the list
if err := meta.SetList(list, filtered); err != nil {
Copy link
Copy Markdown
Member

@zhaohuabing zhaohuabing Feb 20, 2026

Choose a reason for hiding this comment

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

Nit: this can be skipped if the length of the resulting list is the same as the original one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

// Filter items based on namespace labels
var filtered []runtime.Object
for _, item := range items {
obj, ok := item.(metav1.Object)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: Could we cache namespace match results in a map (keyed by namespace) and check that first, to avoid repeatedly calling checkObjectNamespaceLabels for objects in the same namespace?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

@zhaohuabing
Copy link
Copy Markdown
Member

Hi @shahar-h thanks for the fix! This looks good overall. I left a few minor nits for follow-up.

@zhaohuabing zhaohuabing requested a review from a team February 20, 2026 03:51
Signed-off-by: Shahar Harari <shahar.harari@sap.com>
@shahar-h
Copy link
Copy Markdown
Contributor Author

/retest

kkk777-7
kkk777-7 previously approved these changes Feb 22, 2026
@kkk777-7
Copy link
Copy Markdown
Member

LGTM, thanks!

@arkodg arkodg requested a review from zhaohuabing February 22, 2026 19:55
@zhaohuabing
Copy link
Copy Markdown
Member

Hi @shahar-h — once the conflict is resolved, we should be good to go.

Signed-off-by: Shahar Harari <shahar.harari@sap.com>
@shahar-h
Copy link
Copy Markdown
Contributor Author

Hi @shahar-h — once the conflict is resolved, we should be good to go.

Done

zhaohuabing
zhaohuabing previously approved these changes Feb 23, 2026
Copy link
Copy Markdown
Member

@zhaohuabing zhaohuabing left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for fixing this!

Signed-off-by: shahar-h <shahar.harari@sap.com>
@shahar-h
Copy link
Copy Markdown
Contributor Author

/retest

@shahar-h
Copy link
Copy Markdown
Contributor Author

/retest

1 similar comment
@shahar-h
Copy link
Copy Markdown
Contributor Author

/retest

Copy link
Copy Markdown
Member

@cnvergence cnvergence left a comment

Choose a reason for hiding this comment

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

thanks!

@shahar-h
Copy link
Copy Markdown
Contributor Author

Can this be merged if there are no further comments?

@cnvergence cnvergence merged commit 73503ed into envoyproxy:main Feb 25, 2026
98 of 105 checks passed
@shahar-h shahar-h deleted the fix-8305 branch February 25, 2026 12:56
antonio-mazzini pushed a commit to antonio-mazzini/gateway that referenced this pull request Mar 5, 2026
envoyproxy#8312)

* fix(kubernetes): apply namespace selector filtering to List operations

Signed-off-by: Shahar Harari <shahar.harari@sap.com>

* disable codecov

Signed-off-by: Shahar Harari <shahar.harari@sap.com>

* cleanup

Signed-off-by: Shahar Harari <shahar.harari@sap.com>

* fix(kubernetes): apply namespace selector filtering to List operations

Signed-off-by: Shahar Harari <shahar.harari@sap.com>

* Update release notes

Signed-off-by: Shahar Harari <shahar.harari@sap.com>

* Improve coverage

Signed-off-by: Shahar Harari <shahar.harari@sap.com>

* Fix lint error

Signed-off-by: Shahar Harari <shahar.harari@sap.com>

* cr fixes

Signed-off-by: Shahar Harari <shahar.harari@sap.com>

---------

Signed-off-by: Shahar Harari <shahar.harari@sap.com>
Signed-off-by: shahar-h <shahar.harari@sap.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

xPolicy resources are processed from all namespaces when NamespaceSelector watch is configured

4 participants