-
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
Changes from all commits
3aa6d17
0125d66
8870c14
bff907c
32823e0
b016bf0
00fd9e6
3b99f78
1812851
e09472a
608fb09
21b2ef5
870db57
9c58fd4
e481342
51f1ca0
3409053
68ca625
84eec7b
42b9ead
d51f0e3
14c63bb
e9bcfbe
e8ede1a
a71eafe
eff1ccb
217629f
a8e7efc
a1d303d
7b58dca
a656160
1f1e1d0
95a9f2e
720a4c5
a510cc7
0a79dd7
f12955b
717933b
966aee7
68471f7
fb090a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
package io.javaoperatorsdk.operator.api.reconciler; | ||
|
||
import java.util.function.Supplier; | ||
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.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; | ||
|
||
/** | ||
* Utility methods to patch the primary resource state and store it to the related cache, to make | ||
* sure that fresh resource is present for the next reconciliation. The main use case for such | ||
* updates is to store state is resource status. Use of optimistic locking is not desired for such | ||
* updates, since we don't want to patch fail and lose information that we want to store. | ||
*/ | ||
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. | ||
csviri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* 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 updateAndCacheStatus(P primary, Context<P> context) { | ||
logWarnIfResourceVersionPresent(primary); | ||
return patchAndCacheStatus( | ||
primary, context, () -> context.getClient().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 patchAndCacheStatus(P primary, Context<P> context) { | ||
logWarnIfResourceVersionPresent(primary); | ||
return patchAndCacheStatus( | ||
primary, context, () -> context.getClient().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 editAndCacheStatus( | ||
P primary, Context<P> context, UnaryOperator<P> operation) { | ||
logWarnIfResourceVersionPresent(primary); | ||
return patchAndCacheStatus( | ||
primary, context, () -> context.getClient().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 | ||
* @return the updated resource. | ||
* @param <P> primary resource type | ||
*/ | ||
public static <P extends HasMetadata> P patchAndCacheStatus( | ||
P primary, Context<P> context, Supplier<P> patch) { | ||
var updatedResource = patch.get(); | ||
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 ssaPatchAndCacheStatus( | ||
P primary, P freshResourceWithStatus, Context<P> context) { | ||
logWarnIfResourceVersionPresent(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}. | ||
* | ||
* @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, | ||
cache, | ||
() -> | ||
context | ||
.getClient() | ||
.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}. | ||
* | ||
* @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); | ||
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. If optimistic locking is not required, why do we need to log a warning? |
||
return patchAndCacheStatus( | ||
primary, cache, () -> context.getClient().resource(primary).editStatus(operation)); | ||
} | ||
|
||
/** | ||
* Patches the resource with JSON Merge patch and adds it to the {@link PrimaryResourceCache} | ||
* provided. | ||
* | ||
* @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); | ||
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. If optimistic locking is not required, why do we need to log a warning? |
||
return patchAndCacheStatus( | ||
primary, cache, () -> context.getClient().resource(primary).patchStatus()); | ||
} | ||
|
||
/** | ||
* Updates the resource and adds it to the {@link PrimaryResourceCache}. | ||
* | ||
* @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, cache, () -> context.getClient().resource(primary).updateStatus()); | ||
} | ||
|
||
/** | ||
* Updates the resource using the user provided implementation anc caches the result. | ||
* | ||
* @param primary resource | ||
* @param cache resource cache managed by user | ||
* @param patch implementation of resource update* | ||
* @return the updated resource. | ||
* @param <P> primary resource type | ||
*/ | ||
public static <P extends HasMetadata> P patchAndCacheStatus( | ||
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. Can we add small JavaDoc for this exposed method as well? 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. Still would be nice to have a JavaDoc for this public method. |
||
P primary, PrimaryResourceCache<P> cache, Supplier<P> patch) { | ||
var updatedResource = patch.get(); | ||
cache.cacheResource(primary, updatedResource); | ||
return updatedResource; | ||
} | ||
|
||
private static <P extends HasMetadata> void logWarnIfResourceVersionPresent(P primary) { | ||
if (primary.getMetadata().getResourceVersion() != null) { | ||
log.warn( | ||
"The metadata.resourceVersion of primary resource is NOT null, " | ||
+ "using optimistic locking is discouraged for this purpose. "); | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.