Skip to content

Commit 1992acd

Browse files
committed
wip
Signed-off-by: Attila Mészáros <[email protected]>
1 parent 6e5d46e commit 1992acd

File tree

1 file changed

+42
-32
lines changed

1 file changed

+42
-32
lines changed

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

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
/**
1818
* Utility methods to patch the primary resource state and store it to the related cache, to make
1919
* sure that fresh resource is present for the next reconciliation. The main use case for such
20-
* updates is to store state is resource status. Use of optimistic locking is not desired for such
21-
* updates, since we don't want to patch fail and lose information that we want to store.
20+
* updates is to store state is resource status. We aim here for completeness and provide you all
21+
* various options, where all of them have pros and cons.
2222
*/
2323
public class PrimaryUpdateAndCacheUtils {
2424

@@ -39,7 +39,7 @@ private PrimaryUpdateAndCacheUtils() {}
3939
*/
4040
public static <P extends HasMetadata> P updateStatusAndCacheResource(
4141
P primary, Context<P> context) {
42-
logWarnIfResourceVersionPresent(primary);
42+
checkResourceVersionNotPresentAndParseConfiguration(primary, context);
4343
return patchStatusAndCacheResource(
4444
primary, context, () -> context.getClient().resource(primary).updateStatus());
4545
}
@@ -64,7 +64,7 @@ public static <P extends HasMetadata> P updateStatusAndCacheResourceWithLock(
6464
*/
6565
public static <P extends HasMetadata> P patchStatusAndCacheResource(
6666
P primary, Context<P> context) {
67-
logWarnIfResourceVersionPresent(primary);
67+
checkResourceVersionNotPresentAndParseConfiguration(primary, context);
6868
return patchStatusAndCacheResource(
6969
primary, context, () -> context.getClient().resource(primary).patchStatus());
7070
}
@@ -86,7 +86,7 @@ public static <P extends HasMetadata> P patchStatusAndCacheResourceWithLock(
8686
*/
8787
public static <P extends HasMetadata> P editStatusAndCacheResource(
8888
P primary, Context<P> context, UnaryOperator<P> operation) {
89-
logWarnIfResourceVersionPresent(primary);
89+
checkResourceVersionNotPresentAndParseConfiguration(primary, context);
9090
return patchStatusAndCacheResource(
9191
primary, context, () -> context.getClient().resource(primary).editStatus(operation));
9292
}
@@ -132,24 +132,21 @@ public static <P extends HasMetadata> P patchStatusAndCacheResource(
132132
*/
133133
public static <P extends HasMetadata> P ssaPatchStatusAndCacheResource(
134134
P primary, P freshResourceWithStatus, Context<P> context) {
135-
logWarnIfResourceVersionPresent(freshResourceWithStatus);
136-
var res =
137-
context
138-
.getClient()
139-
.resource(freshResourceWithStatus)
140-
.subresource("status")
141-
.patch(
142-
new PatchContext.Builder()
143-
.withForce(true)
144-
.withFieldManager(context.getControllerConfiguration().fieldManager())
145-
.withPatchType(PatchType.SERVER_SIDE_APPLY)
146-
.build());
147-
148-
context
149-
.eventSourceRetriever()
150-
.getControllerEventSource()
151-
.handleRecentResourceUpdate(ResourceID.fromResource(primary), res, primary);
152-
return res;
135+
checkResourceVersionNotPresentAndParseConfiguration(freshResourceWithStatus, context);
136+
return patchStatusAndCacheResource(
137+
primary,
138+
context,
139+
() ->
140+
context
141+
.getClient()
142+
.resource(freshResourceWithStatus)
143+
.subresource("status")
144+
.patch(
145+
new PatchContext.Builder()
146+
.withForce(true)
147+
.withFieldManager(context.getControllerConfiguration().fieldManager())
148+
.withPatchType(PatchType.SERVER_SIDE_APPLY)
149+
.build()));
153150
}
154151

155152
public static <P extends HasMetadata> P ssaPatchStatusAndCacheResourceWithLock(
@@ -184,7 +181,7 @@ public static <P extends HasMetadata> P ssaPatchStatusAndCacheResourceWithLock(
184181
*/
185182
public static <P extends HasMetadata> P ssaPatchStatusAndCacheResource(
186183
P primary, P freshResourceWithStatus, Context<P> context, PrimaryResourceCache<P> cache) {
187-
logWarnIfResourceVersionPresent(freshResourceWithStatus);
184+
checkResourceVersionIsNotPresent(freshResourceWithStatus);
188185
return patchStatusAndCacheResource(
189186
primary,
190187
cache,
@@ -213,7 +210,7 @@ public static <P extends HasMetadata> P ssaPatchStatusAndCacheResource(
213210
*/
214211
public static <P extends HasMetadata> P editStatusAndCacheResource(
215212
P primary, Context<P> context, PrimaryResourceCache<P> cache, UnaryOperator<P> operation) {
216-
logWarnIfResourceVersionPresent(primary);
213+
checkResourceVersionIsNotPresent(primary);
217214
return patchStatusAndCacheResource(
218215
primary, cache, () -> context.getClient().resource(primary).editStatus(operation));
219216
}
@@ -230,7 +227,7 @@ public static <P extends HasMetadata> P editStatusAndCacheResource(
230227
*/
231228
public static <P extends HasMetadata> P patchStatusAndCacheResource(
232229
P primary, Context<P> context, PrimaryResourceCache<P> cache) {
233-
logWarnIfResourceVersionPresent(primary);
230+
checkResourceVersionIsNotPresent(primary);
234231
return patchStatusAndCacheResource(
235232
primary, cache, () -> context.getClient().resource(primary).patchStatus());
236233
}
@@ -246,7 +243,7 @@ public static <P extends HasMetadata> P patchStatusAndCacheResource(
246243
*/
247244
public static <P extends HasMetadata> P updateStatusAndCacheResource(
248245
P primary, Context<P> context, PrimaryResourceCache<P> cache) {
249-
logWarnIfResourceVersionPresent(primary);
246+
checkResourceVersionIsNotPresent(primary);
250247
return patchStatusAndCacheResource(
251248
primary, cache, () -> context.getClient().resource(primary).updateStatus());
252249
}
@@ -268,11 +265,22 @@ public static <P extends HasMetadata> P patchStatusAndCacheResource(
268265
return updatedResource;
269266
}
270267

271-
private static <P extends HasMetadata> void logWarnIfResourceVersionPresent(P primary) {
268+
private static <P extends HasMetadata> void checkResourceVersionIsNotPresent(P primary) {
272269
if (primary.getMetadata().getResourceVersion() != null) {
273-
log.warn(
274-
"The metadata.resourceVersion of primary resource is NOT null, "
275-
+ "using optimistic locking is discouraged for this purpose. ");
270+
throw new IllegalArgumentException("Resource version is present");
271+
}
272+
}
273+
274+
private static <P extends HasMetadata> void checkResourceVersionNotPresentAndParseConfiguration(
275+
P primary, Context<P> context) {
276+
checkResourceVersionIsNotPresent(primary);
277+
if (!context
278+
.getControllerConfiguration()
279+
.getConfigurationService()
280+
.parseResourceVersionsForEventFilteringAndCaching()) {
281+
throw new OperatorException(
282+
"For internal primary resource caching 'parseResourceVersionsForEventFilteringAndCaching'"
283+
+ " must be allowed.");
276284
}
277285
}
278286

@@ -296,10 +304,11 @@ public static <P extends HasMetadata> P updateAndCacheResourceWithLock(
296304
if (log.isDebugEnabled()) {
297305
log.debug("Conflict retrying update for: {}", ResourceID.fromResource(primary));
298306
}
307+
P modified = null;
299308
int retryIndex = 0;
300309
while (true) {
301310
try {
302-
var modified = modificationFunction.apply(primary);
311+
modified = modificationFunction.apply(primary);
303312
modified.getMetadata().setResourceVersion(primary.getMetadata().getResourceVersion());
304313
var updated = updateMethod.apply(modified);
305314
context
@@ -320,6 +329,7 @@ public static <P extends HasMetadata> P updateAndCacheResourceWithLock(
320329
throw e;
321330
}
322331
if (retryIndex >= maxRetry) {
332+
log.warn("Retry exhausted, last desired resource: {}", modified);
323333
throw new OperatorException(
324334
"Exceeded maximum ("
325335
+ maxRetry

0 commit comments

Comments
 (0)