-
Notifications
You must be signed in to change notification settings - Fork 218
feat: primary resource caching for followup reconciliation(s) #2761
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
Open
csviri
wants to merge
23
commits into
main
Choose a base branch
from
support-reconciler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
3aa6d17
feat: resource cache
csviri 0125d66
wip
csviri 8870c14
wip
csviri bff907c
wip
csviri 32823e0
wip
csviri b016bf0
wip
csviri 00fd9e6
wip
csviri 3b99f78
Integration tests
csviri 1812851
wip
csviri e09472a
fix
csviri 608fb09
Update operator-framework-core/src/main/java/io/javaoperatorsdk/opera…
csviri 21b2ef5
fix
csviri 870db57
additional test
csviri 9c58fd4
doc
csviri e481342
Update operator-framework-core/src/main/java/io/javaoperatorsdk/opera…
csviri 51f1ca0
Update operator-framework/src/test/java/io/javaoperatorsdk/operator/b…
csviri 3409053
Update operator-framework-core/src/main/java/io/javaoperatorsdk/opera…
csviri 68ca625
remove with lock versions
csviri 84eec7b
remove not used code
csviri 42b9ead
Revert "remove not used code"
csviri d51f0e3
Revert "remove with lock versions"
csviri 14c63bb
wip
csviri e9bcfbe
fix: typos and start improving javadoc
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
232 changes: 232 additions & 0 deletions
232
.../src/main/java/io/javaoperatorsdk/operator/api/reconciler/PrimaryUpdateAndCacheUtils.java
This file contains 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,232 @@ | ||
package io.javaoperatorsdk.operator.api.reconciler; | ||
|
||
import java.util.function.BiFunction; | ||
import java.util.function.UnaryOperator; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.fabric8.kubernetes.client.dsl.base.PatchContext; | ||
import io.fabric8.kubernetes.client.dsl.base.PatchType; | ||
import io.javaoperatorsdk.operator.api.reconciler.support.PrimaryResourceCache; | ||
import io.javaoperatorsdk.operator.processing.event.ResourceID; | ||
|
||
public class PrimaryUpdateAndCacheUtils { | ||
|
||
private PrimaryUpdateAndCacheUtils() {} | ||
|
||
private static final Logger log = LoggerFactory.getLogger(PrimaryUpdateAndCacheUtils.class); | ||
|
||
/** | ||
* Makes sure that the up-to-date primary resource will be present during the next reconciliation. | ||
* Using update (PUT) method. | ||
* | ||
* @param primary resource | ||
* @param context of reconciliation | ||
* @return updated resource | ||
* @param <P> primary resource type | ||
*/ | ||
public static <P extends HasMetadata> P updateAndCacheStatusWith(P primary, Context<P> context) { | ||
return patchAndCacheStatusWith(primary, context, (p, c) -> c.resource(primary).updateStatus()); | ||
} | ||
|
||
/** | ||
* Makes sure that the up-to-date primary resource will be present during the next reconciliation. | ||
* Using JSON Merge patch. | ||
* | ||
* @param primary resource | ||
* @param context of reconciliation | ||
* @return updated resource | ||
* @param <P> primary resource type | ||
*/ | ||
public static <P extends HasMetadata> P patchAndCacheStatusWith(P primary, Context<P> context) { | ||
return patchAndCacheStatusWith(primary, context, (p, c) -> c.resource(primary).patchStatus()); | ||
} | ||
|
||
/** | ||
* Makes sure that the up-to-date primary resource will be present during the next reconciliation. | ||
* Using JSON Patch. | ||
* | ||
* @param primary resource | ||
* @param context of reconciliation | ||
* @return updated resource | ||
* @param <P> primary resource type | ||
*/ | ||
public static <P extends HasMetadata> P editAndCacheStatusWith( | ||
P primary, Context<P> context, UnaryOperator<P> operation) { | ||
return patchAndCacheStatusWith( | ||
primary, context, (p, c) -> c.resource(primary).editStatus(operation)); | ||
} | ||
|
||
/** | ||
* Makes sure that the up-to-date primary resource will be present during the next reconciliation. | ||
* | ||
* @param primary resource | ||
* @param context of reconciliation | ||
* @param patch free implementation of cache - make sure you use optimistic locking during the | ||
* update | ||
* @return the updated resource. | ||
* @param <P> primary resource type | ||
*/ | ||
public static <P extends HasMetadata> P patchAndCacheStatusWith( | ||
P primary, Context<P> context, BiFunction<P, KubernetesClient, P> patch) { | ||
checkResourceVersionPresent(primary); | ||
var updatedResource = patch.apply(primary, context.getClient()); | ||
context | ||
.eventSourceRetriever() | ||
.getControllerEventSource() | ||
.handleRecentResourceUpdate(ResourceID.fromResource(primary), updatedResource, primary); | ||
return updatedResource; | ||
} | ||
|
||
/** | ||
* Makes sure that the up-to-date primary resource will be present during the next reconciliation. | ||
* Using Server Side Apply. | ||
* | ||
* @param primary resource | ||
* @param freshResourceWithStatus - fresh resource with target state | ||
* @param context of reconciliation | ||
* @return the updated resource. | ||
* @param <P> primary resource type | ||
*/ | ||
public static <P extends HasMetadata> P ssaPatchAndCacheStatusWith( | ||
P primary, P freshResourceWithStatus, Context<P> context) { | ||
checkResourceVersionPresent(freshResourceWithStatus); | ||
var res = | ||
context | ||
.getClient() | ||
.resource(freshResourceWithStatus) | ||
.subresource("status") | ||
.patch( | ||
new PatchContext.Builder() | ||
.withForce(true) | ||
.withFieldManager(context.getControllerConfiguration().fieldManager()) | ||
.withPatchType(PatchType.SERVER_SIDE_APPLY) | ||
.build()); | ||
|
||
context | ||
.eventSourceRetriever() | ||
.getControllerEventSource() | ||
.handleRecentResourceUpdate(ResourceID.fromResource(primary), res, primary); | ||
return res; | ||
} | ||
|
||
/** | ||
* Patches the resource and adds it to the {@link PrimaryResourceCache} provided. Optimistic | ||
* locking is not required. | ||
* | ||
* @param primary resource | ||
* @param freshResourceWithStatus - fresh resource with target state | ||
* @param context of reconciliation | ||
* @param cache - resource cache managed by user | ||
* @return the updated resource. | ||
* @param <P> primary resource type | ||
*/ | ||
public static <P extends HasMetadata> P ssaPatchAndCacheStatus( | ||
P primary, P freshResourceWithStatus, Context<P> context, PrimaryResourceCache<P> cache) { | ||
logWarnIfResourceVersionPresent(freshResourceWithStatus); | ||
return patchAndCacheStatus( | ||
primary, | ||
context.getClient(), | ||
cache, | ||
(P p, KubernetesClient c) -> | ||
c.resource(freshResourceWithStatus) | ||
.subresource("status") | ||
.patch( | ||
new PatchContext.Builder() | ||
.withForce(true) | ||
.withFieldManager(context.getControllerConfiguration().fieldManager()) | ||
.withPatchType(PatchType.SERVER_SIDE_APPLY) | ||
.build())); | ||
} | ||
|
||
/** | ||
* Patches the resource with JSON Patch and adds it to the {@link PrimaryResourceCache} provided. | ||
* Optimistic locking is not required. | ||
* | ||
* @param primary resource* | ||
* @param context of reconciliation | ||
* @param cache - resource cache managed by user | ||
* @return the updated resource. | ||
* @param <P> primary resource type | ||
*/ | ||
public static <P extends HasMetadata> P editAndCacheStatus( | ||
P primary, Context<P> context, PrimaryResourceCache<P> cache, UnaryOperator<P> operation) { | ||
logWarnIfResourceVersionPresent(primary); | ||
return patchAndCacheStatus( | ||
primary, | ||
context.getClient(), | ||
cache, | ||
(P p, KubernetesClient c) -> c.resource(primary).editStatus(operation)); | ||
} | ||
|
||
/** | ||
* Patches the resource with JSON Merge patch and adds it to the {@link PrimaryResourceCache} | ||
* provided. Optimistic locking is not required. | ||
* | ||
* @param primary resource* | ||
* @param context of reconciliation | ||
* @param cache - resource cache managed by user | ||
* @return the updated resource. | ||
* @param <P> primary resource type | ||
*/ | ||
public static <P extends HasMetadata> P patchAndCacheStatus( | ||
P primary, Context<P> context, PrimaryResourceCache<P> cache) { | ||
logWarnIfResourceVersionPresent(primary); | ||
return patchAndCacheStatus( | ||
primary, | ||
context.getClient(), | ||
cache, | ||
(P p, KubernetesClient c) -> c.resource(primary).patchStatus()); | ||
} | ||
|
||
/** | ||
* Updates the resource and adds it to the {@link PrimaryResourceCache} provided. Optimistic | ||
* locking is not required. | ||
* | ||
* @param primary resource* | ||
* @param context of reconciliation | ||
* @param cache - resource cache managed by user | ||
* @return the updated resource. | ||
* @param <P> primary resource type | ||
*/ | ||
public static <P extends HasMetadata> P updateAndCacheStatus( | ||
P primary, Context<P> context, PrimaryResourceCache<P> cache) { | ||
logWarnIfResourceVersionPresent(primary); | ||
return patchAndCacheStatus( | ||
primary, | ||
context.getClient(), | ||
cache, | ||
(P p, KubernetesClient c) -> c.resource(primary).updateStatus()); | ||
} | ||
|
||
public static <P extends HasMetadata> P patchAndCacheStatus( | ||
P primary, | ||
KubernetesClient client, | ||
PrimaryResourceCache<P> cache, | ||
BiFunction<P, KubernetesClient, P> patch) { | ||
var updatedResource = patch.apply(primary, client); | ||
cache.cacheResource(primary, updatedResource); | ||
return updatedResource; | ||
} | ||
|
||
private static <P extends HasMetadata> void checkResourceVersionPresent(P primary) { | ||
if (primary.getMetadata().getResourceVersion() == null) { | ||
throw new IllegalStateException( | ||
"Primary resource version is null, it is expected to set resource version for updates for caching. Name: %s namespace: %s" | ||
.formatted(primary.getMetadata().getName(), primary.getMetadata().getNamespace())); | ||
} | ||
} | ||
|
||
private static <P extends HasMetadata> void logWarnIfResourceVersionPresent(P primary) { | ||
if (primary.getMetadata().getResourceVersion() != null) { | ||
log.warn( | ||
"Primary resource version is NOT null, for caching with optimistic locking use" | ||
+ " alternative methods. Name: {} namespace: {}", | ||
primary.getMetadata().getName(), | ||
primary.getMetadata().getNamespace()); | ||
} | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
...rc/main/java/io/javaoperatorsdk/operator/api/reconciler/support/PrimaryResourceCache.java
This file contains 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,65 @@ | ||
package io.javaoperatorsdk.operator.api.reconciler.support; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.function.BiPredicate; | ||
|
||
import io.fabric8.kubernetes.api.model.HasMetadata; | ||
import io.javaoperatorsdk.operator.processing.event.ResourceID; | ||
|
||
public class PrimaryResourceCache<P extends HasMetadata> { | ||
|
||
private final BiPredicate<Pair<P>, P> evictionPredicate; | ||
private final ConcurrentHashMap<ResourceID, Pair<P>> cache = new ConcurrentHashMap<>(); | ||
|
||
public PrimaryResourceCache(BiPredicate<Pair<P>, P> evictionPredicate) { | ||
this.evictionPredicate = evictionPredicate; | ||
} | ||
|
||
public PrimaryResourceCache() { | ||
this(new ResourceVersionParsingEvictionPredicate<>()); | ||
} | ||
|
||
public void cacheResource(P afterUpdate) { | ||
var resourceId = ResourceID.fromResource(afterUpdate); | ||
cache.put(resourceId, new Pair<>(null, afterUpdate)); | ||
} | ||
|
||
public void cacheResource(P beforeUpdate, P afterUpdate) { | ||
var resourceId = ResourceID.fromResource(beforeUpdate); | ||
cache.put(resourceId, new Pair<>(beforeUpdate, afterUpdate)); | ||
} | ||
|
||
public P getFreshResource(P newVersion) { | ||
var resourceId = ResourceID.fromResource(newVersion); | ||
var pair = cache.get(resourceId); | ||
if (pair == null) { | ||
return newVersion; | ||
} | ||
if (!newVersion.getMetadata().getUid().equals(pair.afterUpdate().getMetadata().getUid())) { | ||
cache.remove(resourceId); | ||
return newVersion; | ||
} | ||
if (evictionPredicate.test(pair, newVersion)) { | ||
cache.remove(resourceId); | ||
return newVersion; | ||
} else { | ||
return pair.afterUpdate(); | ||
} | ||
} | ||
|
||
public void cleanup(P resource) { | ||
cache.remove(ResourceID.fromResource(resource)); | ||
} | ||
|
||
public record Pair<T extends HasMetadata>(T beforeUpdate, T afterUpdate) {} | ||
|
||
/** This works in general, but it does not strictly follow the contract with k8s API */ | ||
public static class ResourceVersionParsingEvictionPredicate<T extends HasMetadata> | ||
implements BiPredicate<Pair<T>, T> { | ||
@Override | ||
public boolean test(Pair<T> updatePair, T newVersion) { | ||
return Long.parseLong(updatePair.afterUpdate().getMetadata().getResourceVersion()) | ||
<= Long.parseLong(newVersion.getMetadata().getResourceVersion()); | ||
} | ||
} | ||
} |
This file contains 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 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.
Can we add small JavaDoc for this exposed method as well?