feat(controllers): Enable multiple controllers with different label selectors for the same entity type #911
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.
Overview
This PR introduces support for registering multiple controllers for the same Kubernetes entity type, each with different label selectors. This enhancement allows operators to handle different subsets of resources using specialized controllers based on label-based filtering.
Fixes #909
Key Changes
🔧 Core Interface Updates
IEntityLabelSelector<TEntity>
toIEntityLabelSelector<TEntity, TSelf>
to enable type-safe registration of multiple selectorsIOperatorBuilder.AddController<TImplementation, TEntity, TLabelSelector>()
method signature to match the new interface constraint🏗️ Service Registration Improvements
OperatorBuilder
to properly register multiple label selectors as distinct servicesAddController<TImplementation, TEntity>()
method delegate to the selector-aware version withDefaultEntityLabelSelector<TEntity>
🔍 Resource Watcher Enhancements
ResourceWatcher<TEntity>
toResourceWatcher<TEntity, TSelector>
for selector-specific watchingLeaderAwareResourceWatcher<TEntity>
with the same pattern for leader election scenarios✅ Comprehensive Test Coverage
OperatorBuilder.Test.cs
to verify multiple selector registrationTestLabelSelector2
to demonstrate multiple selector scenariosBenefits
Backward Compatibility
✅ Fully backward compatible - existing code using single controllers continues to work without changes. The parameterless
AddController<TImplementation, TEntity>()
method automatically uses the default label selector.Example Usage
This enhancement significantly improves the flexibility of the KubeOps operator framework while maintaining full backward compatibility with existing implementations.