Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@mysiki
Copy link

@mysiki mysiki commented Nov 11, 2025

closes: #1187

! honest information : The fix and the PR message was written by AI (useful sometime :D). I understand the result, build it and it work as expected. Also all tests seems to work. This fix only fix the category selector, I don't know if this should be fix also for other selectors ...

Feel free to request change, or take it yourself :)

Result log :

[2025-11-11 22:51:09,882[] asyncio              [DEBUG   ] Using selector: EpollSelector
[2025-11-11 22:51:09,883[] kopf._core.reactor.r [DEBUG   ] Starting Kopf 0.1.dev1851+g2a39916de.
[2025-11-11 22:51:09,883[] kopf.activities.star [DEBUG   ] Activity 'configure' is invoked.
[2025-11-11 22:51:09,884[] kopf.activities.star [INFO    ] Activity 'configure' succeeded.
[2025-11-11 22:51:09,884[] kopf._core.engines.a [INFO    ] Initial authentication has been initiated.
[2025-11-11 22:51:09,885[] kopf.activities.auth [DEBUG   ] Activity 'login_with_service_account' is invoked.
[2025-11-11 22:51:09,885[] kopf._core.engines.p [DEBUG   ] Serving health status at http://0.0.0.0:8080/healthz
[2025-11-11 22:51:09,885[] kopf.activities.auth [INFO    ] Activity 'login_with_service_account' succeeded.
[2025-11-11 22:51:09,886[] kopf._core.engines.a [INFO    ] Initial authentication has finished.
[2025-11-11 22:51:09,996[] kopf._cogs.clients.w [DEBUG   ] Starting the watch-stream for customresourcedefinitions.v1.apiextensions.k8s.io cluster-wide.
[2025-11-11 22:51:09,997[] kopf._kits.webhooks  [DEBUG   ] Using a provided certificate for HTTPS.
[2025-11-11 22:51:09,998[] kopf._cogs.clients.w [DEBUG   ] Starting the watch-stream for rsmultis.v1beta2.test.com cluster-wide.
[2025-11-11 22:51:09,998[] kopf._cogs.clients.w [DEBUG   ] Starting the watch-stream for rssimples.v1beta1.test.com cluster-wide.
[2025-11-11 22:51:09,998[] kopf._cogs.clients.w [DEBUG   ] Starting the watch-stream for rsmultis.v1beta1.test.com cluster-wide.
[2025-11-11 22:51:09,999[] kopf._kits.webhooks  [DEBUG   ] Listening for webhooks at https://*
[2025-11-11 22:51:09,999[] kopf._kits.webhooks  [DEBUG   ] Accessing the webhooks at https://webhook.localhost
[2025-11-11 22:51:10,000[] kopf._core.engines.a [INFO    ] Reconfiguring the mutating webhook externalname.kopf.dev.
[2025-11-11 22:51:10,004[] kopf._core.engines.a [INFO    ] Reconfiguring the validating webhook externalname.kopf.dev.

Fix category selector to include all versions, not just preferred ones

Problem

When using category selectors (e.g., category=test), only CRDs with the preferred version were being selected, even though all CRDs with that category should be included.

For example, with these CRDs:

kubectl api-resources --categories test
NAME        SHORTNAMES   APIVERSION         NAMESPACED   KIND
rsmultis                 test.com/v1beta2   true         Rsmulti
rssimples                test.com/v1beta1   true         Rssimple

Only rsmultis/v1beta2 was selected when using category=test, instead of both resources.

Root Cause

The issue was in the check method of the Selector class in kopf/_cogs/structs/references.py at line 361:

((self.version is None and resource.preferred) or self.version == resource.version)

This logic meant that when self.version was None (which it is for category selectors), only resources where resource.preferred was True would be selected. In Kubernetes, only one version per API group can be preferred, causing non-preferred versions to be filtered out.

Solution

Modified the version matching logic to specifically handle category selectors differently:

# For category-based selection, we should include all resources that match the category,
# regardless of version preference. For other selectors, preserve the original behavior.
version_matches = (
    self.version == resource.version or 
    (self.version is None and (
        resource.preferred or  # Original behavior for non-category selectors
        self.category is not None  # New: allow all versions for category selectors
    ))
)

Changes

  • Fixed category selectors: Now correctly select all resources with matching categories, regardless of version preference
  • Preserved existing behavior: All other selector types continue to work exactly as before
  • Added regression tests: Created comprehensive test cases to prevent this bug from reoccurring

Testing

  • ✅ All existing tests pass (189/189)
  • ✅ New regression tests verify the fix works correctly
  • ✅ Verified that non-category selectors still respect preferred versions appropriately

Files Changed

  • kopf/_cogs/structs/references.py: Fixed the version matching logic in Selector.check()
  • tests/references/test_category_selector_fix.py: Added comprehensive regression tests

This fix is minimal, targeted, and maintains backward compatibility while resolving the specific issue with category-based resource selection.

@mysiki mysiki requested a review from nolar as a code owner November 11, 2025 22:56
Signed-off-by: mysiki <[email protected]>
@mysiki
Copy link
Author

mysiki commented Nov 20, 2025

Hey, @nolar as this is critical for my use case just want to know if you have / will have time to check it ?

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.

Handler rules not register all resources

1 participant