Skip to content

Persistent state dependent resources #1543

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 57 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
3ff673f
feat: decouple event source from cache + list discriminator (#1378)
csviri Sep 28, 2022
dcd3718
feat: bulk dependent resources (#1448)
csviri Sep 29, 2022
fa7fa28
feat: optional eventsource on dependent resources (#1479)
csviri Oct 4, 2022
d8da98b
refactor: simplify handling of reused event sources (#1518)
csviri Oct 5, 2022
bf7a79d
refactor: isolate index handling to BulkDependentResource interface (…
metacosm Oct 5, 2022
3cd36d1
feat: key based bulk resource creation (#1521)
csviri Oct 6, 2022
0092f0a
fix: fixes after rebase
csviri Oct 6, 2022
7442012
refactor: improve bulk dependent resource api (#1525)
csviri Oct 7, 2022
92ec34e
refactor: isolate bulk dependent resource handling more (#1530)
metacosm Oct 12, 2022
bbf4d80
feat: external resource with generated id support (#1527)
csviri Oct 17, 2022
5a0cb3a
refactor: deleteBulkResource -> deleteTargetResource (#1544)
metacosm Oct 17, 2022
a456441
feat: external state management for dependent resources
csviri Oct 12, 2022
e1a89c7
wip
csviri Oct 13, 2022
0bc7f92
wip
csviri Oct 13, 2022
50ebb9b
wip
csviri Oct 13, 2022
beacbb6
wip
csviri Oct 14, 2022
4e45517
wip
csviri Oct 14, 2022
02c6b4a
Integration test
csviri Oct 17, 2022
aff1fcc
test fix
csviri Oct 18, 2022
a8d9c9f
bulk + external state
csviri Oct 18, 2022
7b9e6c7
IT refactor
csviri Oct 18, 2022
c270875
format
csviri Oct 18, 2022
80d9ea0
fix
csviri Oct 18, 2022
73d84d0
docs
csviri Oct 18, 2022
b1e6dcb
refactors
csviri Oct 18, 2022
a147d4f
fix: put back owner reference
csviri Oct 18, 2022
27cfa03
fix format
csviri Oct 18, 2022
cc45a36
feat: decouple event source from cache + list discriminator (#1378)
csviri Sep 28, 2022
98fbb4f
feat: bulk dependent resources (#1448)
csviri Sep 29, 2022
400e51a
feat: optional eventsource on dependent resources (#1479)
csviri Oct 4, 2022
1aee7c7
refactor: simplify handling of reused event sources (#1518)
csviri Oct 5, 2022
59e1663
refactor: isolate index handling to BulkDependentResource interface (…
metacosm Oct 5, 2022
aaea1a5
feat: key based bulk resource creation (#1521)
csviri Oct 6, 2022
919f61c
fix: fixes after rebase
csviri Oct 6, 2022
e0776a4
refactor: improve bulk dependent resource api (#1525)
csviri Oct 7, 2022
3c29576
refactor: isolate bulk dependent resource handling more (#1530)
metacosm Oct 12, 2022
e0f6110
feat: external resource with generated id support (#1527)
csviri Oct 17, 2022
15e30f1
refactor: deleteBulkResource -> deleteTargetResource (#1544)
metacosm Oct 17, 2022
6a132c8
Merge branch 'next' into persistent-state-dependent-resources
csviri Oct 19, 2022
522ffb7
fixes after merge
csviri Oct 19, 2022
6ad757d
fix: exception wording
metacosm Oct 20, 2022
4c5a52d
format
csviri Oct 21, 2022
9fa7236
feat: decouple event source from cache + list discriminator (#1378)
csviri Sep 28, 2022
37ca487
feat: bulk dependent resources (#1448)
csviri Sep 29, 2022
db4951d
feat: optional eventsource on dependent resources (#1479)
csviri Oct 4, 2022
a80b367
refactor: simplify handling of reused event sources (#1518)
csviri Oct 5, 2022
ce9fc7d
refactor: isolate index handling to BulkDependentResource interface (…
metacosm Oct 5, 2022
c61c6bc
feat: key based bulk resource creation (#1521)
csviri Oct 6, 2022
1757b29
fix: fixes after rebase
csviri Oct 6, 2022
30d084e
refactor: improve bulk dependent resource api (#1525)
csviri Oct 7, 2022
0a84412
refactor: isolate bulk dependent resource handling more (#1530)
metacosm Oct 12, 2022
ba69350
feat: external resource with generated id support (#1527)
csviri Oct 17, 2022
31502ce
refactor: deleteBulkResource -> deleteTargetResource (#1544)
metacosm Oct 17, 2022
6b286f8
Merge branch 'next' into persistent-state-dependent-resources
csviri Oct 24, 2022
e37736e
merge from next
csviri Oct 24, 2022
8c521b1
Merge branch 'next' into persistent-state-dependent-resources
csviri Oct 25, 2022
3dbdde6
rename
csviri Oct 25, 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 @@ -110,38 +110,38 @@ private void logForOperation(String operation, P primary, R desired) {
}

protected R handleCreate(R desired, P primary, Context<P> context) {
ResourceID resourceID = ResourceID.fromResource(primary);
R created = creator.create(desired, primary, context);
throwIfNull(created, primary, "Created resource");
onCreated(resourceID, created);
onCreated(primary, created, context);
return created;
}

/**
* Allows subclasses to perform additional processing (e.g. caching) on the created resource if
* needed.
*
* @param primaryResourceId the {@link ResourceID} of the primary resource associated with the
* newly created resource
* @param primary the {@link ResourceID} of the primary resource associated with the newly created
* resource
* @param created the newly created resource
* @param context
*/
protected abstract void onCreated(ResourceID primaryResourceId, R created);
protected abstract void onCreated(P primary, R created, Context<P> context);

/**
* 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
* @param primary the {@link ResourceID} of the primary resource associated with the newly updated
* resource
* @param updated the updated resource
* @param actual the resource as it was before the update
* @param context
*/
protected abstract void onUpdated(ResourceID primaryResourceId, R updated, R actual);
protected abstract void onUpdated(P primary, R updated, R actual, Context<P> context);

protected R handleUpdate(R actual, R desired, P primary, Context<P> context) {
ResourceID resourceID = ResourceID.fromResource(primary);
R updated = updater.update(actual, desired, primary, context);
throwIfNull(updated, primary, "Updated resource");
onUpdated(resourceID, updated, actual);
onUpdated(primary, updated, actual, context);
return updated;
}

Expand All @@ -154,8 +154,9 @@ public void delete(P primary, Context<P> context) {
dependentResourceReconciler.delete(primary, context);
}

protected void handleDelete(P primary, Context<P> context) {
throw new IllegalStateException("delete method be implemented if Deleter trait is supported");
protected void handleDelete(P primary, R secondary, Context<P> context) {
throw new IllegalStateException(
"handleDelete method must be implemented if Deleter trait is supported");
}

public void setResourceDiscriminator(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Optional;

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.dependent.EventSourceNotFoundException;
Expand Down Expand Up @@ -94,15 +95,17 @@ public Optional<ResourceEventSource<R, P>> eventSource() {
return Optional.ofNullable(eventSource);
}

protected void onCreated(ResourceID primaryResourceId, R created) {
protected void onCreated(P primary, R created, Context<P> context) {
if (isCacheFillerEventSource) {
recentOperationCacheFiller().handleRecentResourceCreate(primaryResourceId, created);
recentOperationCacheFiller().handleRecentResourceCreate(ResourceID.fromResource(primary),
created);
}
}

protected void onUpdated(ResourceID primaryResourceId, R updated, R actual) {
protected void onUpdated(P primary, R updated, R actual, Context<P> context) {
if (isCacheFillerEventSource) {
recentOperationCacheFiller().handleRecentResourceUpdate(primaryResourceId, updated, actual);
recentOperationCacheFiller().handleRecentResourceUpdate(ResourceID.fromResource(primary),
updated, actual);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package io.javaoperatorsdk.operator.processing.dependent;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.api.reconciler.dependent.RecentOperationCacheFiller;
import io.javaoperatorsdk.operator.processing.event.EventSourceRetriever;
import io.javaoperatorsdk.operator.processing.event.ResourceID;
import io.javaoperatorsdk.operator.processing.event.source.ResourceEventSource;
import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource;

public abstract class AbstractExternalDependentResource<R, P extends HasMetadata, T extends ResourceEventSource<R, P>>
extends AbstractEventSourceHolderDependentResource<R, P, T> {

private final boolean isDependentResourceWithExplicitState =
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would someone use this class and not implement DependentResourceWithExplicitState? If yes, why?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The thing is that the "batteries included" externals should support easily external resources with and without the state. So it should not implement DependentResourceWithExplicitState by default.
But you got a point, that if some just want to imlpement an external resource event source without state, might just extend AbstractEventSourceHolderDependentResource.

Maybe we should rename it to ExternalWithStateSupportDependentResource ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure renaming the class will fix the problem… Why would someone extend AbstractExternalDependentResource and not implement DependentResourceWithExplicitState? Could you detail a use case where someone would extend this class and not want an explicit external state with it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yep, so we have already samples for that (like the mysql sample):
If somebody want a PollingDependentResource, that does not need an external state.

Note that not every external resource needs an explicit state, for example if it can be addressed only by the information in the custom resource. However some might need one, like if a github PR is managed. Therefore the current implementation allows to a dependent resource to extend PollingDependentResource (and others) and to decide if it needs an external state or not implementing the related DependentResourceWithExplicitState.

I thing that we might address in a separate PR and could have an impact on this is this issue:
#1511
(but not necessarily)

However I recognize that Polling and PerResourcePolling DRs are quite a thin layer, that basically just creates an instance of a EventSource on top of a standard functionality. (althoug might be extended later for example with an improved configuration). So the structure might change in the future if we for example make it less explic? "Thus just an external event source that provides an event source" abstraction.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems, though, that (most of) the functionality that this class provides is only available if you implement DependentResourceWithExplicitState so I'm wondering about the benefit of having this class plus a separate interface when most (if not all of the behavior) is specific to that one use case.

Copy link
Collaborator Author

@csviri csviri Oct 25, 2022

Choose a reason for hiding this comment

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

Yep, but don't see how we would cover otherwise just optionally having this external state functionality for Polling EventSources.

this instanceof DependentResourceWithExplicitState;
private final boolean isBulkDependentResource = this instanceof BulkDependentResource;
@SuppressWarnings("rawtypes")
private DependentResourceWithExplicitState dependentResourceWithExplicitState;
private InformerEventSource<?, P> externalStateEventSource;
private KubernetesClient kubernetesClient;

@SuppressWarnings("unchecked")
protected AbstractExternalDependentResource(Class<R> resourceType) {
super(resourceType);
if (isDependentResourceWithExplicitState) {
dependentResourceWithExplicitState = (DependentResourceWithExplicitState<R, P, ?>) this;
}
}

@Override
@SuppressWarnings("unchecked")
public void resolveEventSource(EventSourceRetriever<P> eventSourceRetriever) {
super.resolveEventSource(eventSourceRetriever);
if (isDependentResourceWithExplicitState) {
externalStateEventSource = (InformerEventSource<?, P>) dependentResourceWithExplicitState
.eventSourceName()
.map(n -> eventSourceRetriever
.getResourceEventSourceFor(dependentResourceWithExplicitState.stateResourceClass(),
(String) n))
.orElseGet(() -> eventSourceRetriever
.getResourceEventSourceFor(
(Class<R>) dependentResourceWithExplicitState.stateResourceClass()));
}

}

@Override
protected void onCreated(P primary, R created, Context<P> context) {
super.onCreated(primary, created, context);
if (this instanceof DependentResourceWithExplicitState) {
handleExplicitStateCreation(primary, created, context);
}
}

@Override
public void delete(P primary, Context<P> context) {
if (isDependentResourceWithExplicitState && !isBulkDependentResource) {
var secondary = getSecondaryResource(primary, context);
super.delete(primary, context);
// deletes the state after the resource is deleted
handleExplicitStateDelete(primary, secondary.orElse(null), context);
} else {
super.delete(primary, context);
}
}

@SuppressWarnings("unchecked")
private void handleExplicitStateDelete(P primary, R secondary, Context<P> context) {
var res = dependentResourceWithExplicitState.stateResource(primary, secondary);
dependentResourceWithExplicitState.getKubernetesClient().resource(res).delete();
}

@SuppressWarnings({"rawtypes", "unchecked"})
protected void handleExplicitStateCreation(P primary, R created, Context<P> context) {
var resource = dependentResourceWithExplicitState.stateResource(primary, created);
var stateResource =
dependentResourceWithExplicitState.getKubernetesClient().resource(resource).create();
if (externalStateEventSource != null) {
((RecentOperationCacheFiller) externalStateEventSource)
.handleRecentResourceCreate(ResourceID.fromResource(primary), stateResource);
}
}


@SuppressWarnings("unchecked")
public void deleteTargetResource(P primary, R resource, String key,
Context<P> context) {
if (isDependentResourceWithExplicitState) {
getKubernetesClient()
.resource(dependentResourceWithExplicitState.stateResource(primary, resource))
.delete();
}
handleDeleteTargetResource(primary, resource, key, context);
}

public void handleDeleteTargetResource(P primary, R resource, String key,
Context<P> context) {
throw new IllegalStateException("Override this method in case you manage an bulk resource");
}

@SuppressWarnings("rawtypes")
protected InformerEventSource getExternalStateEventSource() {
return externalStateEventSource;
}

/**
* It's here just to manage the explicit state resource in case the dependent resource implements
* {@link RecentOperationCacheFiller}.
*
* @return kubernetes client.
*/
public KubernetesClient getKubernetesClient() {
return kubernetesClient;
}

public void setKubernetesClient(KubernetesClient kubernetesClient) {
this.kubernetesClient = kubernetesClient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import io.javaoperatorsdk.operator.api.reconciler.dependent.Deleter;
import io.javaoperatorsdk.operator.api.reconciler.dependent.ReconcileResult;
import io.javaoperatorsdk.operator.processing.dependent.Matcher.Result;
import io.javaoperatorsdk.operator.processing.event.ResourceID;

class BulkDependentResourceReconciler<R, P extends HasMetadata>
implements DependentResourceReconciler<R, P> {
Expand Down Expand Up @@ -97,13 +96,13 @@ public Result<R> match(R resource, P primary, Context<P> context) {
}

@Override
protected void onCreated(ResourceID primaryResourceId, R created) {
asAbstractDependentResource().onCreated(primaryResourceId, created);
protected void onCreated(P primary, R created, Context<P> context) {
asAbstractDependentResource().onCreated(primary, created, context);
}

@Override
protected void onUpdated(ResourceID primaryResourceId, R updated, R actual) {
asAbstractDependentResource().onUpdated(primaryResourceId, updated, actual);
protected void onUpdated(P primary, R updated, R actual, Context<P> context) {
asAbstractDependentResource().onUpdated(primary, updated, actual, context);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.javaoperatorsdk.operator.processing.dependent;

import java.util.Optional;

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.api.reconciler.dependent.Deleter;
import io.javaoperatorsdk.operator.api.reconciler.dependent.managed.KubernetesClientAware;

/**
* Handles external resources where in order to address the resource additional information or
* persistent state (usually the ID of the resource) is needed to access the current state. These
* are non Kubernetes resources which when created their ID is generated, so cannot be determined
* based only on primary resources. In order to manage such dependent resource use this interface
* for a resource that extends {@link AbstractExternalDependentResource}.
*/
public interface DependentResourceWithExplicitState<R, P extends HasMetadata, S extends HasMetadata>
extends Creator<R, P>, Deleter<P>, KubernetesClientAware {

/**
* Only needs to be implemented if multiple event sources are present for the target resource
* class.
*
* @return name of the event source to access the state resources.
*/
default Optional<String> eventSourceName() {
return Optional.empty();
}

/**
* Class of the state resource.
*/
Class<S> stateResourceClass();

/** State resource which contains the target state. Usually an ID to address the resource */
S stateResource(P primary, R resource);

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public ReconcileResult<R> reconcile(P primary, Context<P> context) {

@Override
public void delete(P primary, Context<P> context) {
instance.handleDelete(primary, context);
var secondary = instance.getSecondaryResource(primary, context);
instance.handleDelete(primary, secondary.orElse(null), context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.javaoperatorsdk.operator.api.reconciler.Ignore;
import io.javaoperatorsdk.operator.processing.dependent.AbstractEventSourceHolderDependentResource;
import io.javaoperatorsdk.operator.processing.dependent.AbstractExternalDependentResource;
import io.javaoperatorsdk.operator.processing.event.source.CacheKeyMapper;
import io.javaoperatorsdk.operator.processing.event.source.ExternalResourceCachingEventSource;

@Ignore
public abstract class AbstractPollingDependentResource<R, P extends HasMetadata>
extends
AbstractEventSourceHolderDependentResource<R, P, ExternalResourceCachingEventSource<R, P>>
extends AbstractExternalDependentResource<R, P, ExternalResourceCachingEventSource<R, P>>
implements CacheKeyMapper<R> {

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is a polling dependent resource necessarily external?

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, we don't poll kubernetes API.

public static final int DEFAULT_POLLING_PERIOD = 5000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,10 @@ public Result<R> match(R actualResource, R desired, P primary, Context<P> contex
return GenericKubernetesResourceMatcher.match(desired, actualResource, false);
}

protected void handleDelete(P primary, Context<P> context) {
var resource = getSecondaryResource(primary, context);
resource.ifPresent(r -> client.resource(r).delete());
protected void handleDelete(P primary, R secondary, Context<P> context) {
if (secondary != null) {
client.resource(secondary).delete();
}
}

@SuppressWarnings("unused")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.processing.event.ResourceID;
import io.javaoperatorsdk.operator.sample.simple.TestCustomResource;

import static org.junit.jupiter.api.Assertions.*;
Expand Down Expand Up @@ -85,10 +84,12 @@ public Optional<ConfigMap> getSecondaryResource(TestCustomResource primary,
}

@Override
protected void onCreated(ResourceID primaryResourceId, ConfigMap created) {}
protected void onCreated(TestCustomResource primary, ConfigMap created,
Context<TestCustomResource> context) {}

@Override
protected void onUpdated(ResourceID primaryResourceId, ConfigMap updated, ConfigMap actual) {}
protected void onUpdated(TestCustomResource primary, ConfigMap updated, ConfigMap actual,
Context<TestCustomResource> context) {}

@Override
protected ConfigMap desired(TestCustomResource primary, Context<TestCustomResource> context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.awaitility.Awaitility.await;

class EventSourceIT {

@RegisterExtension
LocallyRunOperatorExtension operator =
LocallyRunOperatorExtension.builder().withReconciler(EventSourceTestCustomReconciler.class)
Expand Down
Loading