Skip to content

Commit b016bf0

Browse files
committed
wip
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent 32823e0 commit b016bf0

File tree

2 files changed

+81
-31
lines changed

2 files changed

+81
-31
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,90 @@
11
package io.javaoperatorsdk.operator.api.reconciler;
22

33
import java.util.function.BiFunction;
4+
import java.util.function.UnaryOperator;
45

56
import org.slf4j.Logger;
67
import org.slf4j.LoggerFactory;
78

89
import io.fabric8.kubernetes.api.model.HasMetadata;
910
import io.fabric8.kubernetes.client.KubernetesClient;
11+
import io.fabric8.kubernetes.client.dsl.base.PatchContext;
12+
import io.fabric8.kubernetes.client.dsl.base.PatchType;
1013
import io.javaoperatorsdk.operator.api.reconciler.support.UserPrimaryResourceCache;
1114
import io.javaoperatorsdk.operator.processing.event.ResourceID;
1215

1316
public class PrimaryUpdateAndCacheUtils {
1417

18+
private PrimaryUpdateAndCacheUtils() {}
19+
1520
private static final Logger log = LoggerFactory.getLogger(PrimaryUpdateAndCacheUtils.class);
1621

1722
public static <P extends HasMetadata> P updateAndCacheStatus(P primary, Context<P> context) {
18-
if (primary.getMetadata().getResourceVersion() == null) {
19-
throw new IllegalStateException(
20-
"Primary resource version is null, it is expected to set resource version for updates"
21-
+ " with for cache");
22-
}
23-
var updatedResource = context.getClient().resource(primary).updateStatus();
23+
return patchAndCacheStatusWithLock(
24+
primary, context, (p, c) -> c.resource(primary).updateStatus());
25+
}
26+
27+
public static <P extends HasMetadata> P patchAndCacheStatusWithLock(
28+
P primary, Context<P> context) {
29+
return patchAndCacheStatusWithLock(
30+
primary, context, (p, c) -> c.resource(primary).patchStatus());
31+
}
32+
33+
public static <P extends HasMetadata> P editAndCacheStatusWithLock(
34+
P primary, Context<P> context, UnaryOperator<P> operation) {
35+
return patchAndCacheStatusWithLock(
36+
primary, context, (p, c) -> c.resource(primary).editStatus(operation));
37+
}
38+
39+
public static <P extends HasMetadata> P patchAndCacheStatusWithLock(
40+
P primary, Context<P> context, BiFunction<P, KubernetesClient, P> patch) {
41+
checkResourceVersionPresent(primary);
42+
var updatedResource = patch.apply(primary, context.getClient());
2443
context
2544
.eventSourceRetriever()
2645
.getControllerEventSource()
2746
.handleRecentResourceUpdate(ResourceID.fromResource(primary), updatedResource, primary);
28-
return updatedResource;
47+
return null;
2948
}
3049

31-
public static <P extends HasMetadata> P patchAndCacheStatus(
32-
P primary, Context<P> context, UserPrimaryResourceCache<P> cache) {
50+
public static <P extends HasMetadata> P ssaPatchAndCacheStatusWithLock(
51+
P primary, P freshResourceWithStatus, Context<P> context) {
52+
checkResourceVersionPresent(freshResourceWithStatus);
53+
var res =
54+
context
55+
.getClient()
56+
.resource(freshResourceWithStatus)
57+
.subresource("status")
58+
.patch(
59+
new PatchContext.Builder()
60+
.withForce(true)
61+
.withFieldManager(context.getControllerConfiguration().fieldManager())
62+
.withPatchType(PatchType.SERVER_SIDE_APPLY)
63+
.build());
3364

65+
context
66+
.eventSourceRetriever()
67+
.getControllerEventSource()
68+
.handleRecentResourceUpdate(ResourceID.fromResource(primary), res, primary);
69+
return res;
70+
}
71+
72+
public static <P extends HasMetadata> P ssaPatchAndCacheStatus(
73+
P primary, P freshResource, Context<P> context, UserPrimaryResourceCache<P> cache) {
74+
logWarnIfResourceVersionPresent(freshResource);
3475
return patchAndCacheStatus(
3576
primary,
3677
context.getClient(),
3778
cache,
38-
(P p, KubernetesClient c) -> {
39-
if (context
40-
.getControllerConfiguration()
41-
.getConfigurationService()
42-
.useSSAToPatchPrimaryResource()) {
43-
return c.resource(p).serverSideApply();
44-
} else {
45-
return c.resource(p).patchStatus();
46-
}
47-
});
79+
(P p, KubernetesClient c) ->
80+
c.resource(freshResource)
81+
.subresource("status")
82+
.patch(
83+
new PatchContext.Builder()
84+
.withForce(true)
85+
.withFieldManager(context.getControllerConfiguration().fieldManager())
86+
.withPatchType(PatchType.SERVER_SIDE_APPLY)
87+
.build()));
4888
}
4989

5090
public static <P extends HasMetadata> P patchAndCacheStatus(
@@ -56,4 +96,22 @@ public static <P extends HasMetadata> P patchAndCacheStatus(
5696
cache.cacheResource(primary, updatedResource);
5797
return updatedResource;
5898
}
99+
100+
private static <P extends HasMetadata> void checkResourceVersionPresent(P primary) {
101+
if (primary.getMetadata().getResourceVersion() == null) {
102+
throw new IllegalStateException(
103+
"Primary resource version is null, it is expected to set resource version for updates for caching. Name: %s namespace: %s"
104+
.formatted(primary.getMetadata().getName(), primary.getMetadata().getNamespace()));
105+
}
106+
}
107+
108+
private static <P extends HasMetadata> void logWarnIfResourceVersionPresent(P primary) {
109+
if (primary.getMetadata().getResourceVersion() != null) {
110+
log.warn(
111+
"Primary resource version is NOT null, for caching with optimistic locking use"
112+
+ " alternative methods. Name: {} namespace: {}",
113+
primary.getMetadata().getName(),
114+
primary.getMetadata().getNamespace());
115+
}
116+
}
59117
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/support/UserPrimaryResourceCache.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public P getFreshResource(P newVersion) {
4343
}
4444
}
4545

46+
public void cleanup(P resource) {
47+
cache.remove(ResourceID.fromResource(resource));
48+
}
49+
4650
public record Pair<T extends HasMetadata>(T beforeUpdate, T afterUpdate) {}
4751

4852
public static class ResourceVersionParsingEvictionPredicate<T extends HasMetadata>
@@ -53,16 +57,4 @@ public boolean test(Pair<T> updatePair, T newVersion) {
5357
<= Long.parseLong(newVersion.getMetadata().getResourceVersion());
5458
}
5559
}
56-
57-
public static class EqualityPredicateForOptimisticUpdate<T extends HasMetadata>
58-
implements BiPredicate<Pair<T>, T> {
59-
@Override
60-
public boolean test(Pair<T> updatePair, T newVersion) {
61-
return !updatePair
62-
.beforeUpdate()
63-
.getMetadata()
64-
.getResourceVersion()
65-
.equals(newVersion.getMetadata().getResourceVersion());
66-
}
67-
}
6860
}

0 commit comments

Comments
 (0)