Skip to content

Optional eventsource on dependent resources #1479

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

Merged
merged 65 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
6e42e53
feat: improvements on caching and dependent resources
csviri Jul 27, 2022
7e3119c
wip
csviri Jul 27, 2022
ffc150b
fix
csviri Jul 27, 2022
7b16504
kubernetes dependent resource configuration
csviri Jul 28, 2022
7b442bc
IT fix
csviri Jul 28, 2022
f5fd957
fixed ITs
csviri Jul 29, 2022
d9d4a95
index based discriminator
csviri Jul 29, 2022
924f162
IT fix
csviri Jul 29, 2022
7eb4eb7
wip
csviri Jul 29, 2022
1f6b8b9
fixes from rebase from next
csviri Aug 26, 2022
c359557
fix after rebase
csviri Sep 5, 2022
e9c8c7e
event source provider to context
csviri Sep 6, 2022
2042fd8
todo fixes
csviri Sep 16, 2022
8f04146
remove void discriminator
csviri Sep 27, 2022
bdd2d96
rebase on next
csviri Sep 27, 2022
378f463
fix: bulk creation of dependent resource directly in abstract resource
csviri Sep 8, 2022
210034a
wip
csviri Sep 9, 2022
7a56e1c
wip
csviri Sep 9, 2022
bc4709c
wip to start IT
csviri Sep 9, 2022
2e96cd9
fixes, progress
csviri Sep 9, 2022
6892077
wp
csviri Sep 12, 2022
9d8055b
matcher
csviri Sep 12, 2022
e222735
test passes
csviri Sep 12, 2022
e55592d
bulk dependent resource to an interface
csviri Sep 13, 2022
b0a7227
wip
csviri Sep 14, 2022
88f0053
test improvement
csviri Sep 14, 2022
fe33446
note
csviri Sep 14, 2022
65705d4
wip
csviri Sep 15, 2022
58c4096
rebase on next
csviri Sep 20, 2022
6a4109b
increates test timeout
csviri Sep 20, 2022
5a3ac2d
comment
csviri Sep 20, 2022
50c6a0f
fix format
csviri Sep 20, 2022
b955396
wip
csviri Sep 20, 2022
069290d
delete, other improvements
csviri Sep 21, 2022
018e8c5
manage tests, refactored ITs
csviri Sep 21, 2022
a596b00
additionl IT
csviri Sep 21, 2022
bd6b9e7
external resource
csviri Sep 21, 2022
50a50b9
external resource IT
csviri Sep 22, 2022
3d07b2e
docs
csviri Sep 22, 2022
fe3bd1c
feat: optional event source from dependent resources
csviri Sep 16, 2022
efd2324
fix test
csviri Sep 22, 2022
ab1e980
wip
csviri Sep 23, 2022
34850d1
wip
csviri Sep 23, 2022
fc618d7
wip
csviri Sep 26, 2022
72ef5b6
wip
csviri Sep 26, 2022
5927d96
format
csviri Sep 27, 2022
6c87c40
fix
csviri Sep 27, 2022
b910139
IT skeleton
csviri Sep 27, 2022
ef67469
IT managed dependent resourc
csviri Sep 27, 2022
d80feed
IT improvement
csviri Sep 27, 2022
a6bee16
wip external resource handling
csviri Sep 27, 2022
94dff83
fix shortname
csviri Sep 27, 2022
5f6356e
IT
csviri Sep 27, 2022
a488572
feat: decouple event source from cache + list discriminator (#1378)
csviri Sep 28, 2022
7c66c05
bulk dependent resources (#1448)
csviri Sep 29, 2022
adef026
Merge branch 'next' into optional-eventsource-on-dr
csviri Sep 29, 2022
66882bc
fixes after merge
csviri Sep 29, 2022
d403291
Merge branch 'next' into optional-eventsource-on-dr
csviri Oct 3, 2022
ba64ed8
fixes after merge
csviri Oct 3, 2022
5a912a5
controller fix
csviri Oct 3, 2022
4576677
merge fixes
csviri Oct 3, 2022
55c3796
automatic event source selection
csviri Oct 3, 2022
964cf5a
fix
csviri Oct 3, 2022
9bad894
proper setting event source
csviri Oct 3, 2022
b86b6f7
fix: avoid NPE if no dependent resources are passed
metacosm Oct 3, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ public List<DependentResourceSpec> getDependentResources() {
Set.of(dependent.dependsOn()),
instantiateIfNotDefault(dependent.readyPostcondition(), Condition.class, context),
instantiateIfNotDefault(dependent.reconcilePrecondition(), Condition.class, context),
instantiateIfNotDefault(dependent.deletePostcondition(), Condition.class, context));
instantiateIfNotDefault(dependent.deletePostcondition(), Condition.class, context),
dependent.provideEventSource());
specsMap.put(name, spec);
}

Expand Down Expand Up @@ -286,13 +287,13 @@ private Object createKubernetesResourceConfig(Class<? extends DependentResource>
OnDeleteFilter<? extends HasMetadata> onDeleteFilter = null;
GenericFilter<? extends HasMetadata> genericFilter = null;
ResourceDiscriminator<?, ? extends HasMetadata> resourceDiscriminator = null;
String eventSourceNameToUse = null;
if (kubeDependent != null) {
if (!Arrays.equals(KubernetesDependent.DEFAULT_NAMESPACES,
kubeDependent.namespaces())) {
namespaces = Set.of(kubeDependent.namespaces());
configuredNS = true;
}

final var fromAnnotation = kubeDependent.labelSelector();
labelSelector = Constants.NO_VALUE_SET.equals(fromAnnotation) ? null : fromAnnotation;

Expand All @@ -313,12 +314,14 @@ private Object createKubernetesResourceConfig(Class<? extends DependentResource>
resourceDiscriminator =
instantiateIfNotDefault(kubeDependent.resourceDiscriminator(),
ResourceDiscriminator.class, context);
eventSourceNameToUse = Constants.NO_VALUE_SET.equals(kubeDependent.eventSourceToUse()) ? null
: kubeDependent.eventSourceToUse();
}

config =
new KubernetesDependentResourceConfig(namespaces, labelSelector, configuredNS,
resourceDiscriminator, onAddFilter,
onUpdateFilter, onDeleteFilter, genericFilter);
onUpdateFilter, onDeleteFilter, genericFilter, eventSourceNameToUse);

return config;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ private void replaceConfig(String name, Object newConfig, DependentResourceSpec<
namedDependentResourceSpecs.put(name,
new DependentResourceSpec<>(current.getDependentResourceClass(), newConfig, name,
current.getDependsOn(), current.getReadyCondition(), current.getReconcileCondition(),
current.getDeletePostCondition()));
current.getDeletePostCondition(), current.provideEventSource()));
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -220,7 +220,7 @@ public ControllerConfiguration<R> build() {
KubernetesDependentResourceConfig c) {
return new DependentResourceSpec(spec.getDependentResourceClass(),
c.setNamespaces(namespaces), name, spec.getDependsOn(), spec.getReadyCondition(),
spec.getReconcileCondition(), spec.getDeletePostCondition());
spec.getReconcileCondition(), spec.getDeletePostCondition(), spec.provideEventSource());
}

public static <R extends HasMetadata> ControllerConfigurationOverrider<R> override(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,20 @@ public class DependentResourceSpec<T extends DependentResource<?, ?>, C> {

private final Condition<?, ?> deletePostCondition;

private final boolean provideEventSource;

public DependentResourceSpec(Class<T> dependentResourceClass, C dependentResourceConfig,
String name, Set<String> dependsOn, Condition<?, ?> readyCondition,
Condition<?, ?> reconcileCondition, Condition<?, ?> deletePostCondition) {
Condition<?, ?> reconcileCondition, Condition<?, ?> deletePostCondition,
boolean provideEventSource) {
this.dependentResourceClass = dependentResourceClass;
this.dependentResourceConfig = dependentResourceConfig;
this.name = name;
this.dependsOn = dependsOn;
this.readyCondition = readyCondition;
this.reconcileCondition = reconcileCondition;
this.deletePostCondition = deletePostCondition;
this.provideEventSource = provideEventSource;
}

public Class<T> getDependentResourceClass() {
Expand Down Expand Up @@ -89,4 +93,8 @@ public Condition getReconcileCondition() {
public Condition getDeletePostCondition() {
return deletePostCondition;
}

public boolean provideEventSource() {
return provideEventSource;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package io.javaoperatorsdk.operator.api.reconciler;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
import io.javaoperatorsdk.operator.processing.event.source.EventSource;
import io.javaoperatorsdk.operator.processing.event.source.ResourceEventSource;

/**
* An interface that a {@link Reconciler} can implement to have the SDK register the provided
Expand Down Expand Up @@ -39,6 +43,22 @@ static Map<String, EventSource> nameEventSources(EventSource... eventSources) {
return eventSourceMap;
}

@SuppressWarnings("unchecked,rawtypes")
static <K extends HasMetadata> Map<String, EventSource> nameEventSourcesFromDependentResource(
EventSourceContext<K> context, DependentResource... dependentResources) {

if (dependentResources != null) {
Map<String, EventSource> eventSourceMap = new HashMap<>(dependentResources.length);
for (DependentResource dependentResource : dependentResources) {
Optional<ResourceEventSource> es = dependentResource.eventSource(context);
es.ifPresent(e -> eventSourceMap.put(generateNameFor(e), e));
}
return eventSourceMap;
} else {
return Collections.emptyMap();
}
}

/**
* This is for the use case when the event sources are not access explicitly by name in the
* reconciler.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
public interface ResourceDiscriminator<R, P extends HasMetadata> {

Optional<R> distinguish(Class<R> resource, P primary, Context<P> context);

}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,13 @@
* one can be
*/
String[] dependsOn() default {};

/**
* Setting this to false means that the event source provided by the dependent resource won't be
* used. This is helpful if more dependent resources created for the same type, and want to share
* a common event source. In that case an event source needs to be explicitly registered.
*
* @return if the event source (if any) provided by the dependent resource should be used or not.
*/
boolean provideEventSource() default true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
import io.javaoperatorsdk.operator.processing.event.source.ResourceEventSource;

/**
* An interface to implement and provide dependent resource support.
Expand All @@ -29,6 +31,31 @@ public interface DependentResource<R, P extends HasMetadata> {
*/
Class<R> resourceType();

/**
* Dependent resources are designed to by default provide event sources. There are cases where it
* might not:
* <ul>
* <li>If an event source is shared between multiple dependent resources. In this case only one or
* none of the dependent resources sharing the event source should provide one.</li>
* <li>Some special implementation of an event source. That just execute some action might not
* provide one.</li>
* </ul>
*
* @param eventSourceContext context of event source initialization
* @return an optional event source
*/
default Optional<ResourceEventSource<R, P>> eventSource(
EventSourceContext<P> eventSourceContext) {
return Optional.empty();
}

/**
* Calling this method, instructs the implementation to not provide an event source, even if it
* normally does.
*/
void doNotProvideEventSource();


default Optional<R> getSecondaryResource(P primary, Context<P> context) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.javaoperatorsdk.operator.api.reconciler.dependent;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.processing.event.EventSourceRetriever;

public interface EventSourceAware<P extends HasMetadata> {

void selectEventSources(EventSourceRetriever<P> eventSourceRetriever);

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
import io.javaoperatorsdk.operator.processing.event.source.EventSource;

/**
* @deprecated now event source related methods are directly on {@link DependentResource}
* @param <P> primary resource
*/
@Deprecated(forRemoval = true)
public interface EventSourceProvider<P extends HasMetadata> {
/**
* @param context - event source context where the event source is initialized
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@
import io.javaoperatorsdk.operator.api.reconciler.Ignore;
import io.javaoperatorsdk.operator.api.reconciler.Reconciler;
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
import io.javaoperatorsdk.operator.api.reconciler.dependent.EventSourceAware;
import io.javaoperatorsdk.operator.api.reconciler.dependent.EventSourceProvider;
import io.javaoperatorsdk.operator.api.reconciler.dependent.managed.DefaultManagedDependentResourceContext;
import io.javaoperatorsdk.operator.processing.dependent.workflow.ManagedWorkflow;
import io.javaoperatorsdk.operator.processing.dependent.workflow.WorkflowCleanupResult;
import io.javaoperatorsdk.operator.processing.event.EventProcessor;
import io.javaoperatorsdk.operator.processing.event.EventSourceManager;
import io.javaoperatorsdk.operator.processing.event.ResourceID;
import io.javaoperatorsdk.operator.processing.event.source.ResourceEventSource;

import static io.javaoperatorsdk.operator.api.reconciler.Constants.WATCH_CURRENT_NAMESPACE;

Expand Down Expand Up @@ -207,21 +209,30 @@ private void initContextIfNeeded(P resource, Context<P> context) {
}

public void initAndRegisterEventSources(EventSourceContext<P> context) {
managedWorkflow
.getDependentResourcesByName().entrySet().stream()
.filter(drEntry -> drEntry.getValue() instanceof EventSourceProvider)
.forEach(drEntry -> {
final var provider = (EventSourceProvider) drEntry.getValue();
final var source = provider.initEventSource(context);
eventSourceManager.registerEventSource(drEntry.getKey(), source);
});

// add manually defined event sources
if (reconciler instanceof EventSourceInitializer) {
final var provider = (EventSourceInitializer<P>) this.reconciler;
final var ownSources = provider.prepareEventSources(context);
ownSources.forEach(eventSourceManager::registerEventSource);
}
managedWorkflow
.getDependentResourcesByName().entrySet().stream()
.forEach(drEntry -> {
if (drEntry.getValue() instanceof EventSourceProvider) {
final var provider = (EventSourceProvider) drEntry.getValue();
final var source = provider.initEventSource(context);
eventSourceManager.registerEventSource(drEntry.getKey(), source);
} else {
Optional<ResourceEventSource> eventSource =
drEntry.getValue().eventSource(context);
eventSource.ifPresent(es -> {
eventSourceManager.registerEventSource(drEntry.getKey(), es);
});
}
});
managedWorkflow.getDependentResourcesByName().entrySet().stream().map(Map.Entry::getValue)
.filter(EventSourceAware.class::isInstance)
.forEach(dr -> ((EventSourceAware) dr)
.selectEventSources(eventSourceManager));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
import io.javaoperatorsdk.operator.api.reconciler.Ignore;
import io.javaoperatorsdk.operator.api.reconciler.ResourceDiscriminator;
import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource;
import io.javaoperatorsdk.operator.api.reconciler.dependent.ReconcileResult;
import io.javaoperatorsdk.operator.processing.event.ResourceID;
import io.javaoperatorsdk.operator.processing.event.source.ResourceEventSource;

@Ignore
public abstract class AbstractDependentResource<R, P extends HasMetadata>
Expand All @@ -27,8 +29,9 @@ public abstract class AbstractDependentResource<R, P extends HasMetadata>
protected Creator<R, P> creator;
protected Updater<R, P> updater;
protected BulkDependentResource<R, P> bulkDependentResource;
private boolean returnEventSource = true;

private final List<ResourceDiscriminator<R, P>> resourceDiscriminator = new ArrayList<>(1);
protected List<ResourceDiscriminator<R, P>> resourceDiscriminator = new ArrayList<>(1);

@SuppressWarnings("unchecked")
public AbstractDependentResource() {
Expand All @@ -38,6 +41,23 @@ public AbstractDependentResource() {
bulkDependentResource = bulk ? (BulkDependentResource<R, P>) this : null;
}

@Override
public void doNotProvideEventSource() {
this.returnEventSource = false;
}

@Override
public Optional<ResourceEventSource<R, P>> eventSource(EventSourceContext<P> eventSourceContext) {
if (!returnEventSource) {
return Optional.empty();
} else {
return Optional.of(provideEventSource(eventSourceContext));
}
}

protected abstract ResourceEventSource<R, P> provideEventSource(
EventSourceContext<P> eventSourceContext);

@Override
public ReconcileResult<R> reconcile(P primary, Context<P> context) {
if (bulk) {
Expand Down Expand Up @@ -172,7 +192,7 @@ protected R handleCreate(R desired, P primary, Context<P> context) {
protected abstract void onCreated(ResourceID primaryResourceId, R created);

/**
* Allows sub-classes to perform additional processing on the updated resource if needed.
* Allows subclasses to perform additional processing on the updated resource if needed.
*
* @param primaryResourceId the {@link ResourceID} of the primary resource associated with the
* newly updated resource
Expand Down Expand Up @@ -219,4 +239,7 @@ protected int lastKnownBulkSize() {
return resourceDiscriminator.size();
}

protected boolean getReturnEventSource() {
return returnEventSource;
}
}
Loading