-
Notifications
You must be signed in to change notification settings - Fork 219
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
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 dcd3718
feat: bulk dependent resources (#1448)
csviri fa7fa28
feat: optional eventsource on dependent resources (#1479)
csviri d8da98b
refactor: simplify handling of reused event sources (#1518)
csviri bf7a79d
refactor: isolate index handling to BulkDependentResource interface (…
metacosm 3cd36d1
feat: key based bulk resource creation (#1521)
csviri 0092f0a
fix: fixes after rebase
csviri 7442012
refactor: improve bulk dependent resource api (#1525)
csviri 92ec34e
refactor: isolate bulk dependent resource handling more (#1530)
metacosm bbf4d80
feat: external resource with generated id support (#1527)
csviri 5a0cb3a
refactor: deleteBulkResource -> deleteTargetResource (#1544)
metacosm a456441
feat: external state management for dependent resources
csviri e1a89c7
wip
csviri 0bc7f92
wip
csviri 50ebb9b
wip
csviri beacbb6
wip
csviri 4e45517
wip
csviri 02c6b4a
Integration test
csviri aff1fcc
test fix
csviri a8d9c9f
bulk + external state
csviri 7b9e6c7
IT refactor
csviri c270875
format
csviri 80d9ea0
fix
csviri 73d84d0
docs
csviri b1e6dcb
refactors
csviri a147d4f
fix: put back owner reference
csviri 27cfa03
fix format
csviri cc45a36
feat: decouple event source from cache + list discriminator (#1378)
csviri 98fbb4f
feat: bulk dependent resources (#1448)
csviri 400e51a
feat: optional eventsource on dependent resources (#1479)
csviri 1aee7c7
refactor: simplify handling of reused event sources (#1518)
csviri 59e1663
refactor: isolate index handling to BulkDependentResource interface (…
metacosm aaea1a5
feat: key based bulk resource creation (#1521)
csviri 919f61c
fix: fixes after rebase
csviri e0776a4
refactor: improve bulk dependent resource api (#1525)
csviri 3c29576
refactor: isolate bulk dependent resource handling more (#1530)
metacosm e0f6110
feat: external resource with generated id support (#1527)
csviri 15e30f1
refactor: deleteBulkResource -> deleteTargetResource (#1544)
metacosm 6a132c8
Merge branch 'next' into persistent-state-dependent-resources
csviri 522ffb7
fixes after merge
csviri 6ad757d
fix: exception wording
metacosm 4c5a52d
format
csviri 9fa7236
feat: decouple event source from cache + list discriminator (#1378)
csviri 37ca487
feat: bulk dependent resources (#1448)
csviri db4951d
feat: optional eventsource on dependent resources (#1479)
csviri a80b367
refactor: simplify handling of reused event sources (#1518)
csviri ce9fc7d
refactor: isolate index handling to BulkDependentResource interface (…
metacosm c61c6bc
feat: key based bulk resource creation (#1521)
csviri 1757b29
fix: fixes after rebase
csviri 30d084e
refactor: improve bulk dependent resource api (#1525)
csviri 0a84412
refactor: isolate bulk dependent resource handling more (#1530)
metacosm ba69350
feat: external resource with generated id support (#1527)
csviri 31502ce
refactor: deleteBulkResource -> deleteTargetResource (#1544)
metacosm 6b286f8
Merge branch 'next' into persistent-state-dependent-resources
csviri e37736e
merge from next
csviri 8c521b1
Merge branch 'next' into persistent-state-dependent-resources
csviri 3dbdde6
rename
csviri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
120 changes: 120 additions & 0 deletions
120
...a/io/javaoperatorsdk/operator/processing/dependent/AbstractExternalDependentResource.java
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
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 = | ||
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; | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
.../io/javaoperatorsdk/operator/processing/dependent/DependentResourceWithExplicitState.java
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
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); | ||
|
||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is a polling dependent resource necessarily external? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
?There was a problem hiding this comment.
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 implementDependentResourceWithExplicitState
? Could you detail a use case where someone would extend this class and not want an explicit external state with it?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.