Skip to content

Commit 32823e0

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

File tree

3 files changed

+44
-16
lines changed

3 files changed

+44
-16
lines changed

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
package io.javaoperatorsdk.operator.api.reconciler;
22

3+
import java.util.function.BiFunction;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
38
import io.fabric8.kubernetes.api.model.HasMetadata;
49
import io.fabric8.kubernetes.client.KubernetesClient;
510
import io.javaoperatorsdk.operator.api.reconciler.support.UserPrimaryResourceCache;
611
import io.javaoperatorsdk.operator.processing.event.ResourceID;
712

813
public class PrimaryUpdateAndCacheUtils {
914

15+
private static final Logger log = LoggerFactory.getLogger(PrimaryUpdateAndCacheUtils.class);
16+
1017
public static <P extends HasMetadata> P updateAndCacheStatus(P primary, Context<P> context) {
1118
if (primary.getMetadata().getResourceVersion() == null) {
1219
throw new IllegalStateException(
@@ -24,12 +31,29 @@ public static <P extends HasMetadata> P updateAndCacheStatus(P primary, Context<
2431
public static <P extends HasMetadata> P patchAndCacheStatus(
2532
P primary, Context<P> context, UserPrimaryResourceCache<P> cache) {
2633

27-
return null;
34+
return patchAndCacheStatus(
35+
primary,
36+
context.getClient(),
37+
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+
});
2848
}
2949

3050
public static <P extends HasMetadata> P patchAndCacheStatus(
31-
P primary, KubernetesClient client, UserPrimaryResourceCache<P> cache) {
32-
33-
return null;
51+
P primary,
52+
KubernetesClient client,
53+
UserPrimaryResourceCache<P> cache,
54+
BiFunction<P, KubernetesClient, P> patch) {
55+
var updatedResource = patch.apply(primary, client);
56+
cache.cacheResource(primary, updatedResource);
57+
return updatedResource;
3458
}
3559
}

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
public class UserPrimaryResourceCache<P extends HasMetadata> {
1010

11-
private BiPredicate<Pair<P>, P> evictionCondition;
12-
private ConcurrentHashMap<ResourceID, Pair<P>> cache = new ConcurrentHashMap<>();
11+
private final BiPredicate<Pair<P>, P> evictionPredicate;
12+
private final ConcurrentHashMap<ResourceID, Pair<P>> cache = new ConcurrentHashMap<>();
1313

14-
public UserPrimaryResourceCache(BiPredicate<Pair<P>, P> evictionCondition) {
15-
this.evictionCondition = evictionCondition;
14+
public UserPrimaryResourceCache(BiPredicate<Pair<P>, P> evictionPredicate) {
15+
this.evictionPredicate = evictionPredicate;
1616
}
1717

1818
public void cacheResource(P afterUpdate) {
@@ -35,7 +35,7 @@ public P getFreshResource(P newVersion) {
3535
cache.remove(resourceId);
3636
return newVersion;
3737
}
38-
if (evictionCondition.test(pair, newVersion)) {
38+
if (evictionPredicate.test(pair, newVersion)) {
3939
cache.remove(resourceId);
4040
return newVersion;
4141
} else {
@@ -45,7 +45,7 @@ public P getFreshResource(P newVersion) {
4545

4646
public record Pair<T extends HasMetadata>(T beforeUpdate, T afterUpdate) {}
4747

48-
public static class ResourceVersionComparePredicate<T extends HasMetadata>
48+
public static class ResourceVersionParsingEvictionPredicate<T extends HasMetadata>
4949
implements BiPredicate<Pair<T>, T> {
5050
@Override
5151
public boolean test(Pair<T> updatePair, T newVersion) {
@@ -54,11 +54,15 @@ public boolean test(Pair<T> updatePair, T newVersion) {
5454
}
5555
}
5656

57-
public static class ResourceVersionEqualityPredicate<T extends HasMetadata>
57+
public static class EqualityPredicateForOptimisticUpdate<T extends HasMetadata>
5858
implements BiPredicate<Pair<T>, T> {
5959
@Override
6060
public boolean test(Pair<T> updatePair, T newVersion) {
61-
return !updatePair.beforeUpdate().equals(newVersion.getMetadata().getResourceVersion());
61+
return !updatePair
62+
.beforeUpdate()
63+
.getMetadata()
64+
.getResourceVersion()
65+
.equals(newVersion.getMetadata().getResourceVersion());
6266
}
6367
}
6468
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/TemporaryResourceCache.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import org.slf4j.LoggerFactory;
1010

1111
import io.fabric8.kubernetes.api.model.HasMetadata;
12-
import io.javaoperatorsdk.operator.api.config.informer.InformerEventSourceConfiguration;
12+
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
1313
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResource;
1414
import io.javaoperatorsdk.operator.processing.event.ResourceID;
1515

@@ -167,9 +167,9 @@ public synchronized boolean isKnownResourceVersion(T resource) {
167167
}
168168

169169
/**
170-
* @return true if {@link InformerEventSourceConfiguration#parseResourceVersions()} is enabled and
171-
* the resourceVersion of newResource is numerically greater than cachedResource, otherwise
172-
* false
170+
* @return true if {@link ConfigurationService#parseResourceVersionsForEventFilteringAndCaching()}
171+
* is enabled and the resourceVersion of newResource is numerically greater than
172+
* cachedResource, otherwise false
173173
*/
174174
private boolean isLaterResourceVersion(ResourceID resourceId, T newResource, T cachedResource) {
175175
try {

0 commit comments

Comments
 (0)