Skip to content

Commit f166d8e

Browse files
committed
improve: throw exception if resourceVersion parsing is not turned on on primary caching
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent 63bbec5 commit f166d8e

File tree

1 file changed

+32
-26
lines changed

1 file changed

+32
-26
lines changed

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

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.fabric8.kubernetes.api.model.HasMetadata;
1010
import io.fabric8.kubernetes.client.dsl.base.PatchContext;
1111
import io.fabric8.kubernetes.client.dsl.base.PatchType;
12+
import io.javaoperatorsdk.operator.OperatorException;
1213
import io.javaoperatorsdk.operator.api.reconciler.support.PrimaryResourceCache;
1314
import io.javaoperatorsdk.operator.processing.event.ResourceID;
1415

@@ -34,7 +35,7 @@ private PrimaryUpdateAndCacheUtils() {}
3435
* @param <P> primary resource type
3536
*/
3637
public static <P extends HasMetadata> P updateAndCacheStatus(P primary, Context<P> context) {
37-
logWarnIfResourceVersionPresent(primary);
38+
sanityChecks(primary, context);
3839
return patchAndCacheStatus(
3940
primary, context, () -> context.getClient().resource(primary).updateStatus());
4041
}
@@ -49,7 +50,7 @@ public static <P extends HasMetadata> P updateAndCacheStatus(P primary, Context<
4950
* @param <P> primary resource type
5051
*/
5152
public static <P extends HasMetadata> P patchAndCacheStatus(P primary, Context<P> context) {
52-
logWarnIfResourceVersionPresent(primary);
53+
sanityChecks(primary, context);
5354
return patchAndCacheStatus(
5455
primary, context, () -> context.getClient().resource(primary).patchStatus());
5556
}
@@ -65,7 +66,7 @@ public static <P extends HasMetadata> P patchAndCacheStatus(P primary, Context<P
6566
*/
6667
public static <P extends HasMetadata> P editAndCacheStatus(
6768
P primary, Context<P> context, UnaryOperator<P> operation) {
68-
logWarnIfResourceVersionPresent(primary);
69+
sanityChecks(primary, context);
6970
return patchAndCacheStatus(
7071
primary, context, () -> context.getClient().resource(primary).editStatus(operation));
7172
}
@@ -101,24 +102,21 @@ public static <P extends HasMetadata> P patchAndCacheStatus(
101102
*/
102103
public static <P extends HasMetadata> P ssaPatchAndCacheStatus(
103104
P primary, P freshResourceWithStatus, Context<P> context) {
104-
logWarnIfResourceVersionPresent(freshResourceWithStatus);
105-
var res =
106-
context
107-
.getClient()
108-
.resource(freshResourceWithStatus)
109-
.subresource("status")
110-
.patch(
111-
new PatchContext.Builder()
112-
.withForce(true)
113-
.withFieldManager(context.getControllerConfiguration().fieldManager())
114-
.withPatchType(PatchType.SERVER_SIDE_APPLY)
115-
.build());
116-
117-
context
118-
.eventSourceRetriever()
119-
.getControllerEventSource()
120-
.handleRecentResourceUpdate(ResourceID.fromResource(primary), res, primary);
121-
return res;
105+
sanityChecks(freshResourceWithStatus, context);
106+
return patchAndCacheStatus(
107+
primary,
108+
context,
109+
() ->
110+
context
111+
.getClient()
112+
.resource(freshResourceWithStatus)
113+
.subresource("status")
114+
.patch(
115+
new PatchContext.Builder()
116+
.withForce(true)
117+
.withFieldManager(context.getControllerConfiguration().fieldManager())
118+
.withPatchType(PatchType.SERVER_SIDE_APPLY)
119+
.build()));
122120
}
123121

124122
/**
@@ -133,7 +131,7 @@ public static <P extends HasMetadata> P ssaPatchAndCacheStatus(
133131
*/
134132
public static <P extends HasMetadata> P ssaPatchAndCacheStatus(
135133
P primary, P freshResourceWithStatus, Context<P> context, PrimaryResourceCache<P> cache) {
136-
logWarnIfResourceVersionPresent(freshResourceWithStatus);
134+
sanityChecks(freshResourceWithStatus, context);
137135
return patchAndCacheStatus(
138136
primary,
139137
cache,
@@ -161,7 +159,7 @@ public static <P extends HasMetadata> P ssaPatchAndCacheStatus(
161159
*/
162160
public static <P extends HasMetadata> P editAndCacheStatus(
163161
P primary, Context<P> context, PrimaryResourceCache<P> cache, UnaryOperator<P> operation) {
164-
logWarnIfResourceVersionPresent(primary);
162+
sanityChecks(primary, context);
165163
return patchAndCacheStatus(
166164
primary, cache, () -> context.getClient().resource(primary).editStatus(operation));
167165
}
@@ -178,7 +176,7 @@ public static <P extends HasMetadata> P editAndCacheStatus(
178176
*/
179177
public static <P extends HasMetadata> P patchAndCacheStatus(
180178
P primary, Context<P> context, PrimaryResourceCache<P> cache) {
181-
logWarnIfResourceVersionPresent(primary);
179+
sanityChecks(primary, context);
182180
return patchAndCacheStatus(
183181
primary, cache, () -> context.getClient().resource(primary).patchStatus());
184182
}
@@ -194,7 +192,7 @@ public static <P extends HasMetadata> P patchAndCacheStatus(
194192
*/
195193
public static <P extends HasMetadata> P updateAndCacheStatus(
196194
P primary, Context<P> context, PrimaryResourceCache<P> cache) {
197-
logWarnIfResourceVersionPresent(primary);
195+
sanityChecks(primary, context);
198196
return patchAndCacheStatus(
199197
primary, cache, () -> context.getClient().resource(primary).updateStatus());
200198
}
@@ -215,11 +213,19 @@ public static <P extends HasMetadata> P patchAndCacheStatus(
215213
return updatedResource;
216214
}
217215

218-
private static <P extends HasMetadata> void logWarnIfResourceVersionPresent(P primary) {
216+
private static <P extends HasMetadata> void sanityChecks(P primary, Context<P> context) {
219217
if (primary.getMetadata().getResourceVersion() != null) {
220218
log.warn(
221219
"The metadata.resourceVersion of primary resource is NOT null, "
222220
+ "using optimistic locking is discouraged for this purpose. ");
223221
}
222+
if (!context
223+
.getControllerConfiguration()
224+
.getConfigurationService()
225+
.parseResourceVersionsForEventFilteringAndCaching()) {
226+
throw new OperatorException(
227+
"For internal primary resource caching 'parseResourceVersionsForEventFilteringAndCaching'"
228+
+ " must to be allowed.");
229+
}
224230
}
225231
}

0 commit comments

Comments
 (0)