Skip to content

refactor: simplify handling of reused event sources #1513

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 69 commits into from

Conversation

metacosm
Copy link
Collaborator

@metacosm metacosm commented Oct 3, 2022

DependentResources that provide an EventSource should implement
EventSourceAware. If they want to reuse an already created event source
instead of providing their own, they need to select the appropriate
event source identified by the name specified by the useEventSourceNamed
method.

csviri and others added 14 commits September 29, 2022 16:31
# Conflicts:
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/AnnotationControllerConfiguration.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/ResourceDiscriminator.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/ResourceIDMatcherDiscriminator.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/dependent/DependentResource.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/dependent/ReconcileResult.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/AbstractDependentResource.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/AbstractEventSourceHolderDependentResource.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/BulkDependentResource.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/external/AbstractCachingDependentResource.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/external/AbstractSimpleDependentResource.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/GenericKubernetesResourceMatcher.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependent.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependentResource.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependentResourceConfig.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/workflow/WorkflowCleanupExecutor.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/workflow/WorkflowReconcileExecutor.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/EventSourceManager.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/EventSourceRetriever.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/ResourceEventSource.java
#	operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/config/ControllerConfigurationOverriderTest.java
#	operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/dependent/AbstractDependentResourceTest.java
#	operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/dependent/external/AbstractSimpleDependentResourceTest.java
#	operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/dependent/workflow/AbstractWorkflowExecutorTest.java
#	operator-framework/src/test/java/io/javaoperatorsdk/operator/bulkdependent/BulkExternalDependentIT.java
#	operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/bulkdependent/ConfigMapDeleterBulkDependentResource.java
#	operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/bulkdependent/external/ExternalBulkDependentResource.java
#	operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/multipledependentresource/MultipleDependentResourceReconciler.java
# Conflicts:
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/config/AnnotationControllerConfiguration.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/ResourceDiscriminator.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/dependent/DependentResource.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/AbstractDependentResource.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/AbstractEventSourceHolderDependentResource.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/external/AbstractSimpleDependentResource.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependent.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependentResource.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependentResourceConfig.java
#	operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/EventSourceRetriever.java
#	operator-framework-core/src/test/java/io/javaoperatorsdk/operator/api/config/ControllerConfigurationOverriderTest.java
#	operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/dependent/AbstractDependentResourceTest.java
#	operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/dependent/external/AbstractSimpleDependentResourceTest.java
#	operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/dependent/workflow/AbstractWorkflowExecutorTest.java
#	operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/multipledependentresource/MultipleDependentResourceReconciler.java
DependentResources that provide an EventSource should implement
EventSourceAware. If they want to reuse an already created event source
instead of providing their own, they need to select the appropriate
event source identified by the name specified by the useEventSourceNamed
method.
@metacosm metacosm self-assigned this Oct 3, 2022
@metacosm metacosm requested a review from csviri October 3, 2022 19:12
@sonarqubecloud
Copy link

sonarqubecloud bot commented Oct 4, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 22 Code Smells

25.9% 25.9% Coverage
0.0% 0.0% Duplication

Copy link
Collaborator

@csviri csviri left a comment

Choose a reason for hiding this comment

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

Made comments, I have mixed feeling of some parts, especially that it is not explicit to not use event source, but this is fine for now since all our dependents work like this. Just not really future proof. (Actually will dig how this works in controller runtime)


public interface EventSourceAware<P extends HasMetadata> {
Class<R> resourceType();
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's a little smelly that we have resourceType in both places (also in dependent resource), but no big deal.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, it's not ideal but this allows to have a default implementation in the interface…

import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
import io.javaoperatorsdk.operator.processing.event.source.ResourceEventSource;

public interface EventSourceAware<R, P extends HasMetadata> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

A couple of remarks with this:

  • This is in one hand an elegant solution to couple the event source related methods into one interface
    on the other hand one of the primary reasons was to put these methods to Dependent Resource to couple it more, thus to express that a dependent resource ideally (an usually, so if not it is more of an corner case) should provide an event source. With this we are getting back to the position that basically event source is an optional thing in general.

  • We don't have parity with the @Dependent annotation, so while setting there the eventSource parameter won't have effect on the dependent if it does not implement this interface. (Not that big deal just pointing out).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, on both. This was an initial attempt to see where that could lead. We can (and probably should) refine things.

import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
import io.javaoperatorsdk.operator.processing.event.source.ResourceEventSource;

public interface EventSourceAware<R, P extends HasMetadata> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

not sure if EventSourceAware is the proper name, but cannot think anything better.


void selectEventSources(EventSourceRetriever<P> eventSourceRetriever);
void useEventSourceNamed(String eventSourceName);
Copy link
Collaborator

Choose a reason for hiding this comment

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

  • maybe useEventSourceByName ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'd rather use useEventSourceWithName then.

Copy link
Collaborator

Choose a reason for hiding this comment

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

sounds good :)


void selectEventSources(EventSourceRetriever<P> eventSourceRetriever);
void useEventSourceNamed(String eventSourceName);
Copy link
Collaborator

Choose a reason for hiding this comment

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

What little feels smelly that ok actually most of our dependent resources use event sources to fill the cache on create/update. But in this interface it is explicit to use a different event source but implicit to not provide one. While one is configurable (by setting the name on @Dependent) other can be achieved only by overriding the default Optional<ResourceEventSource<R, P>> eventSource(EventSourceContext<P> context) method. This is smelly I think.

@csviri
Copy link
Collaborator

csviri commented Oct 4, 2022

@metacosm would be good to create a separate PR on next just to see if integration test passing,
( change the CI later to run integration tests on all targets? )

@metacosm
Copy link
Collaborator Author

metacosm commented Oct 4, 2022

Let's merge #1479 with the understanding that #1504 and this PR need to be addressed after.

Base automatically changed from optional-eventsource-on-dr to next October 4, 2022 08:25
@csviri
Copy link
Collaborator

csviri commented Oct 4, 2022

Thinking about this, what if we just replace the doNotProvideEventSource, with useEventSourceByName in dependent resource? would not be little better?

regarding the comments I made, will create a PR for that, how it would look like.

@metacosm
Copy link
Collaborator Author

metacosm commented Oct 4, 2022

Superseded by #1516

@metacosm
Copy link
Collaborator Author

metacosm commented Oct 4, 2022

Thinking about this, what if we just replace the doNotProvideEventSource, with useEventSourceByName in dependent resource? would not be little better?

regarding the comments I made, will create a PR for that, how it would look like.

Yes, was thinking about removing EventSourceAware altogether and put everything on DependentResource

@metacosm metacosm closed this Oct 4, 2022
@metacosm metacosm deleted the optional-es-simplify branch October 4, 2022 08:36
*/
default Optional<ResourceEventSource<R, P>> eventSource(EventSourceContext<P> context) {
return getUsedEventSourceName().map(
name -> context.getEventSourceRetriever().getResourceEventSourceFor(resourceType(), name));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm also this is a little smelly that the EventSourceContext is now meant to be just register event sources, and support registration, not to read 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.

2 participants