-
Notifications
You must be signed in to change notification settings - Fork 219
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 10 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,236 @@ | ||
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 updateAndCacheStatusWithLock( | ||
P primary, Context<P> context) { | ||
return patchAndCacheStatusWithLock( | ||
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 patchAndCacheStatusWithLock( | ||
P primary, Context<P> context) { | ||
return patchAndCacheStatusWithLock( | ||
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 editAndCacheStatusWithLock( | ||
P primary, Context<P> context, UnaryOperator<P> operation) { | ||
return patchAndCacheStatusWithLock( | ||
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 patchAndCacheStatusWithLock( | ||
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 null; | ||
csviri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* 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 ssaPatchAndCacheStatusWithLock( | ||
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); | ||
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, | ||
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* | ||
csviri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @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 edithAndCacheStatus( | ||
csviri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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, | ||
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* | ||
csviri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @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, | ||
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* | ||
csviri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @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( | ||
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, | ||
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" | ||
csviri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
+ " alternative methods. Name: {} namespace: {}", | ||
primary.getMetadata().getName(), | ||
primary.getMetadata().getNamespace()); | ||
} | ||
} | ||
} |
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()); | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.