Skip to content

Conversation

@doxxx93
Copy link
Contributor

@doxxx93 doxxx93 commented Oct 22, 2025

Description

Fixes #1830

Resources recreated with the same name/namespace but different UIDs were incorrectly filtered out by the predicate filter. This PR fixes the issue by tracking UIDs in the cache to distinguish between different resource instances.

Problem

When using predicate filters (e.g., predicates::generation), if a resource is:

  1. Created (generation=1, uid=A)
  2. Deleted
  3. Recreated (generation=1, uid=B)

The recreated resource would be incorrectly filtered out because the cache only used ObjectRef as the key, which deliberately excludes UID from its Hash implementation.

This caused controllers to miss reconciliation of recreated resources, breaking scenarios where resources need to be managed via owner references and garbage collection.

Solution

Created a new PredicateCacheKey struct that includes UID in the cache key:

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct PredicateCacheKey {
    name: String,
    namespace: Option<String>,
    uid: Option<String>,
}

Modified PredicateFilter cache from:

HashMap<ObjectRef<K>, u64>

to:

HashMap<PredicateCacheKey, u64>

This ensures different resource instances (with different UIDs) are treated as separate cache entries.

Changes

  • Added PredicateCacheKey struct that includes UID in equality/hashing
  • Modified PredicateFilter cache to use PredicateCacheKey instead of ObjectRef
  • Updated poll_next to create cache keys that include UID
  • Added regression test predicate_filtering_should_handle_recreated_resources_with_same_generation

Testing

All existing tests pass (67 passed), including new regression test that verifies:

  • First resource with UID "uid-1" passes through
  • Recreated resource with same generation but different UID "uid-2" also passes through (previously would be filtered out)
cargo test --package kube-runtime --lib

Checklist

Fixes kube-rs#1830

Resources recreated with the same name/namespace but different UIDs
were incorrectly filtered out by the predicate filter. The cache now
tracks both ObjectRef and UID to distinguish between different resource
instances.

The bug occurred because ObjectRef deliberately excludes UID from its
Hash implementation (for general-purpose use), but PredicateFilter
needs to distinguish between different resource instances.

Changes:
- Modified PredicateFilter cache to HashMap<ObjectRef<K>, (Option<String>, u64)>
- Updated poll_next to check both UID and predicate value changes
- Added regression test for recreated resources with same generation

The fix ensures that when a resource is deleted and recreated with the
same name/namespace but a different UID (and same generation), the new
resource is properly reconciled by controllers using predicate filters.

Signed-off-by: doxxx93 <[email protected]>
@doxxx93
Copy link
Contributor Author

doxxx93 commented Oct 22, 2025

i will fix lint soon

Copy link
Member

@clux clux left a comment

Choose a reason for hiding this comment

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

thanks a lot for this!

i have one comment initially because it solves it in a way i did not expect, but it's all private implementation details. i just want to make it as simple as possible 😄

@codecov
Copy link

codecov bot commented Oct 22, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.6%. Comparing base (98bfbe3) to head (8b1b7c7).
⚠️ Report is 7 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##            main   #1836     +/-   ##
=======================================
+ Coverage   74.5%   74.6%   +0.1%     
=======================================
  Files         84      84             
  Lines       7856    7877     +21     
=======================================
+ Hits        5849    5871     +22     
+ Misses      2007    2006      -1     
Files with missing lines Coverage Δ
kube-runtime/src/utils/predicate.rs 77.8% <100.0%> (+7.3%) ⬆️

... and 5 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@doxxx93 doxxx93 force-pushed the fix/predicate-filter-uid-tracking branch from 2b84017 to b683a2e Compare October 23, 2025 10:00
@doxxx93
Copy link
Contributor Author

doxxx93 commented Oct 23, 2025

hum.. sorry for lint

Reorder imports alphabetically to match nightly rustfmt requirements.

Signed-off-by: doxxx93 <[email protected]>
Copy link
Member

@clux clux left a comment

Choose a reason for hiding this comment

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

I think this looks great. I would like one extra thing in the tests ideally, and have a comment suggestion, but tiny things. Thanks a lot. Looks very clean.

@doxxx93 doxxx93 force-pushed the fix/predicate-filter-uid-tracking branch from 7e79179 to eaa580e Compare October 23, 2025 11:51
@clux clux merged commit 7c63f56 into kube-rs:main Oct 23, 2025
17 checks passed
@doxxx93 doxxx93 deleted the fix/predicate-filter-uid-tracking branch October 23, 2025 14:41
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.

Recreated objects do not pass through predicate filters

2 participants