-
Notifications
You must be signed in to change notification settings - Fork 220
refactor: isolate bulk dependent resource handling more #1530
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
6 commits
Select commit
Hold shift + click to select a range
8cf411b
refactor: isolate bulk dependent resource handling more
metacosm bfe3e67
refactor: adapt after rebase
metacosm bf96682
fix: iterate over desired instead of actual, handle updatable case
metacosm abd9186
fix: format
metacosm 5f0407c
docs: add more documentation / comments
metacosm 7d92eaa
fix: format
metacosm 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
135 changes: 135 additions & 0 deletions
135
...ava/io/javaoperatorsdk/operator/processing/dependent/BulkDependentResourceReconciler.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,135 @@ | ||
package io.javaoperatorsdk.operator.processing.dependent; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
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> { | ||
|
||
private final BulkDependentResource<R, P> bulkDependentResource; | ||
|
||
BulkDependentResourceReconciler(BulkDependentResource<R, P> bulkDependentResource) { | ||
this.bulkDependentResource = bulkDependentResource; | ||
} | ||
|
||
@Override | ||
public ReconcileResult<R> reconcile(P primary, Context<P> context) { | ||
final var desiredResources = bulkDependentResource.desiredResources(primary, context); | ||
Map<String, R> actualResources = bulkDependentResource.getSecondaryResources(primary, context); | ||
|
||
// remove existing resources that are not needed anymore according to the primary state | ||
deleteBulkResourcesIfRequired(desiredResources.keySet(), actualResources, primary, context); | ||
|
||
final List<ReconcileResult<R>> results = new ArrayList<>(desiredResources.size()); | ||
final var updatable = bulkDependentResource instanceof Updater; | ||
desiredResources.forEach((key, value) -> { | ||
final var instance = | ||
updatable ? new UpdatableBulkDependentResourceInstance<>(bulkDependentResource, value) | ||
: new BulkDependentResourceInstance<>(bulkDependentResource, value); | ||
results.add(instance.reconcile(primary, actualResources.get(key), context)); | ||
}); | ||
|
||
return ReconcileResult.aggregatedResult(results); | ||
} | ||
|
||
@Override | ||
public void delete(P primary, Context<P> context) { | ||
var actualResources = bulkDependentResource.getSecondaryResources(primary, context); | ||
deleteBulkResourcesIfRequired(Collections.emptySet(), actualResources, primary, context); | ||
} | ||
|
||
private void deleteBulkResourcesIfRequired(Set<String> expectedKeys, | ||
Map<String, R> actualResources, P primary, Context<P> context) { | ||
actualResources.forEach((key, value) -> { | ||
if (!expectedKeys.contains(key)) { | ||
bulkDependentResource.deleteBulkResource(primary, value, key, context); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Exposes a dynamically-created instance of the bulk dependent resource precursor as an | ||
* AbstractDependentResource so that we can reuse its reconciliation logic. | ||
* | ||
* @param <R> | ||
* @param <P> | ||
*/ | ||
private static class BulkDependentResourceInstance<R, P extends HasMetadata> | ||
extends AbstractDependentResource<R, P> | ||
implements Creator<R, P>, Deleter<P> { | ||
private final BulkDependentResource<R, P> bulkDependentResource; | ||
private final R desired; | ||
|
||
private BulkDependentResourceInstance(BulkDependentResource<R, P> bulkDependentResource, | ||
R desired) { | ||
this.bulkDependentResource = bulkDependentResource; | ||
this.desired = desired; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private AbstractDependentResource<R, P> asAbstractDependentResource() { | ||
return (AbstractDependentResource<R, P>) bulkDependentResource; | ||
} | ||
|
||
@Override | ||
protected R desired(P primary, Context<P> context) { | ||
return desired; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
public R update(R actual, R desired, P primary, Context<P> context) { | ||
return ((Updater<R, P>) bulkDependentResource).update(actual, desired, primary, context); | ||
} | ||
|
||
@Override | ||
public Result<R> match(R resource, P primary, Context<P> context) { | ||
return bulkDependentResource.match(resource, desired, primary, context); | ||
} | ||
|
||
@Override | ||
protected void onCreated(ResourceID primaryResourceId, R created) { | ||
asAbstractDependentResource().onCreated(primaryResourceId, created); | ||
} | ||
|
||
@Override | ||
protected void onUpdated(ResourceID primaryResourceId, R updated, R actual) { | ||
asAbstractDependentResource().onUpdated(primaryResourceId, updated, actual); | ||
} | ||
|
||
@Override | ||
public Class<R> resourceType() { | ||
return asAbstractDependentResource().resourceType(); | ||
} | ||
|
||
@Override | ||
public R create(R desired, P primary, Context<P> context) { | ||
return bulkDependentResource.create(desired, primary, context); | ||
} | ||
} | ||
|
||
/** | ||
* Makes sure that the instance implements Updater if its precursor does as well. | ||
* | ||
* @param <R> | ||
* @param <P> | ||
*/ | ||
private static class UpdatableBulkDependentResourceInstance<R, P extends HasMetadata> | ||
extends BulkDependentResourceInstance<R, P> implements Updater<R, P> { | ||
|
||
private UpdatableBulkDependentResourceInstance( | ||
BulkDependentResource<R, P> bulkDependentResource, | ||
R desired) { | ||
super(bulkDependentResource, desired); | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...in/java/io/javaoperatorsdk/operator/processing/dependent/DependentResourceReconciler.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,20 @@ | ||
package io.javaoperatorsdk.operator.processing.dependent; | ||
|
||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
import io.javaoperatorsdk.operator.api.reconciler.dependent.ReconcileResult; | ||
|
||
/** | ||
* An internal interface that abstracts away how to reconcile dependent resources, in particular | ||
* when they can be dynamically created based on the state provided by the primary resource (e.g. | ||
* the primary resource dictates which/how many secondary resources need to be created). | ||
* | ||
* @param <R> the type of the secondary resource to be reconciled | ||
* @param <P> the primary resource type | ||
*/ | ||
interface DependentResourceReconciler<R, P extends HasMetadata> { | ||
|
||
ReconcileResult<R> reconcile(P primary, Context<P> context); | ||
|
||
void delete(P primary, Context<P> context); | ||
} |
26 changes: 26 additions & 0 deletions
26
...a/io/javaoperatorsdk/operator/processing/dependent/SingleDependentResourceReconciler.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,26 @@ | ||
package io.javaoperatorsdk.operator.processing.dependent; | ||
|
||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.javaoperatorsdk.operator.api.reconciler.Context; | ||
import io.javaoperatorsdk.operator.api.reconciler.dependent.ReconcileResult; | ||
|
||
class SingleDependentResourceReconciler<R, P extends HasMetadata> | ||
implements DependentResourceReconciler<R, P> { | ||
|
||
private final AbstractDependentResource<R, P> instance; | ||
|
||
SingleDependentResourceReconciler(AbstractDependentResource<R, P> dependentResource) { | ||
this.instance = dependentResource; | ||
} | ||
|
||
@Override | ||
public ReconcileResult<R> reconcile(P primary, Context<P> context) { | ||
final var maybeActual = instance.getSecondaryResource(primary, context); | ||
return instance.reconcile(primary, maybeActual.orElse(null), context); | ||
} | ||
|
||
@Override | ||
public void delete(P primary, Context<P> context) { | ||
instance.handleDelete(primary, context); | ||
} | ||
} |
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.
I don't get why this is needed.
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.
This is needed so that we can have the exact same interface for a "single" or bulk dependent in
AbstractDependentResource
.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.
More specifically, this is needed to be able to do this: https://github.com/java-operator-sdk/java-operator-sdk/pull/1530/files#diff-0d27f73ffef40826943dbbef3e9a2a1e74e2a10341627042ffa5a792d03c3638R39. In order to do it this way, the objects we iterate over need to be
AbstractDependentResources
.