feat(controllers): Enable multiple controllers with different label selectors for the same entity type#911
Closed
stevefan1999-personal wants to merge 4 commits intodotnet:mainfrom
Conversation
Collaborator
|
I like the idea :) |
Contributor
Author
Just a little poke, anything missing? |
Collaborator
|
I reviewed parts of the code and I have some thoughts / questions:
So in my head, I'd like to use something like: interface IEntityController<T> {
ILabelSelector? LabelFilter {get;} = null;
// rest...
}
// ResourceWatcher{TEntity}.cs
private async Task ReconcileDeletionAsync(TEntity entity, CancellationToken cancellationToken)
{
requeue.Remove(entity);
await _entityCache.RemoveAsync(entity.Uid(), token: cancellationToken);
await using var scope = provider.CreateAsyncScope();
// CHANGE HERE: use a list of entity controllers and then select the one that fits best.
// same change goes for the reconcile Modification func
var controller = scope.ServiceProvider.GetRequiredService<IEntityController<TEntity>>();
await controller.DeletedAsync(entity, cancellationToken);
}What do you think about this? As far as I see, this would preserve the default behavior for everybody without a breaking change and allow you to overwrite the behavior if you need it. And you can register multiple controllers for the same entity type. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
OperatorBuilderto 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.csto verify multiple selector registrationTestLabelSelector2to 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.