Skip to content

Commit c5c9e8f

Browse files
committed
IT fixes
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent f245dd1 commit c5c9e8f

5 files changed

Lines changed: 67 additions & 11 deletions

File tree

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

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -995,7 +995,33 @@ <R extends HasMetadata> R resourcePatch(
995995
* @see #addFinalizer(String)
996996
*/
997997
public P addFinalizer() {
998-
return addFinalizer(context.getControllerConfiguration().getFinalizerName());
998+
return addFinalizer(context.getControllerConfiguration().getFinalizerName(), false);
999+
}
1000+
1001+
/**
1002+
* Adds the default finalizer (from controller configuration) to the primary resource. This is a
1003+
* convenience method that calls {@link #addFinalizer(String, boolean)} with the configured
1004+
* finalizer name.
1005+
*
1006+
* @param cacheOnly if {@code true} the finalizer patch does not filter its own event (see {@link
1007+
* #addFinalizer(String, boolean)})
1008+
* @return updated resource from the server response
1009+
* @see #addFinalizer(String, boolean)
1010+
*/
1011+
public P addFinalizer(boolean cacheOnly) {
1012+
return addFinalizer(context.getControllerConfiguration().getFinalizerName(), cacheOnly);
1013+
}
1014+
1015+
/**
1016+
* Adds the given finalizer to the primary resource, filtering the own event (equivalent to {@link
1017+
* #addFinalizer(String, boolean)} with {@code cacheOnly = false}).
1018+
*
1019+
* @param finalizerName name of the finalizer to add
1020+
* @return updated resource from the server response
1021+
* @see #addFinalizer(String, boolean)
1022+
*/
1023+
public P addFinalizer(String finalizerName) {
1024+
return addFinalizer(finalizerName, false);
9991025
}
10001026

10011027
/**
@@ -1005,9 +1031,13 @@ public P addFinalizer() {
10051031
* "Trigger reconciliation on all event" mode is on.
10061032
*
10071033
* @param finalizerName name of the finalizer to add
1034+
* @param cacheOnly if {@code true} the finalizer patch only caches the response and does
1035+
* <em>not</em> filter the resulting own event, so a subsequent reconciliation is triggered by
1036+
* the finalizer addition; if {@code false} the default JSON Patch matcher is used and the own
1037+
* event is filtered
10081038
* @return updated resource from the server response
10091039
*/
1010-
public P addFinalizer(String finalizerName) {
1040+
public P addFinalizer(String finalizerName, boolean cacheOnly) {
10111041
var resource = context.getPrimaryResource();
10121042
if (resource.isMarkedForDeletion() || resource.hasFinalizer(finalizerName)) {
10131043
return resource;
@@ -1017,7 +1047,8 @@ public P addFinalizer(String finalizerName) {
10171047
r.addFinalizer(finalizerName);
10181048
return r;
10191049
},
1020-
r -> !r.hasFinalizer(finalizerName));
1050+
r -> !r.hasFinalizer(finalizerName),
1051+
cacheOnly);
10211052
}
10221053

10231054
/**
@@ -1061,6 +1092,22 @@ public P removeFinalizer(String finalizerName) {
10611092
});
10621093
}
10631094

1095+
/**
1096+
* Patches the primary resource using JSON Patch, retrying on conflict, filtering the own event
1097+
* via the default JSON Patch matcher (equivalent to {@link
1098+
* #conflictRetryingPatchPrimary(UnaryOperator, Predicate, boolean)} with {@code cacheOnly =
1099+
* false}).
1100+
*
1101+
* @param resourceChangesOperator changes to be done on the resource before update
1102+
* @param preCondition condition to check if the patch operation still needs to be performed or
1103+
* not
1104+
* @return updated resource from the server or unchanged if the precondition does not hold
1105+
*/
1106+
public P conflictRetryingPatchPrimary(
1107+
UnaryOperator<P> resourceChangesOperator, Predicate<P> preCondition) {
1108+
return conflictRetryingPatchPrimary(resourceChangesOperator, preCondition, false);
1109+
}
1110+
10641111
/**
10651112
* Patches the primary resource using JSON Patch, retrying on conflict. The {@code
10661113
* resourceChangesOperator} is applied to the current resource before each attempt; if the server
@@ -1071,12 +1118,15 @@ public P removeFinalizer(String finalizerName) {
10711118
* @param resourceChangesOperator changes to be done on the resource before update
10721119
* @param preCondition condition to check if the patch operation still needs to be performed or
10731120
* not; evaluated against the (possibly re-fetched) resource before each attempt
1121+
* @param cacheOnly if {@code true} the patch only caches the response and does <em>not</em>
1122+
* filter the resulting own event; if {@code false} the default JSON Patch matcher is used and
1123+
* the own event is filtered
10741124
* @return updated resource from the server or unchanged if the precondition does not hold
10751125
* @throws OperatorException if the maximum number of retry attempts is exceeded
10761126
*/
10771127
@SuppressWarnings("unchecked")
10781128
public P conflictRetryingPatchPrimary(
1079-
UnaryOperator<P> resourceChangesOperator, Predicate<P> preCondition) {
1129+
UnaryOperator<P> resourceChangesOperator, Predicate<P> preCondition, boolean cacheOnly) {
10801130
var resource = context.getPrimaryResource();
10811131
var client = context.getClient();
10821132
if (log.isDebugEnabled()) {
@@ -1088,7 +1138,12 @@ public P conflictRetryingPatchPrimary(
10881138
if (!preCondition.test(resource)) {
10891139
return resource;
10901140
}
1091-
return jsonPatchPrimary(resource, resourceChangesOperator);
1141+
return jsonPatchPrimary(
1142+
resource,
1143+
resourceChangesOperator,
1144+
cacheOnly
1145+
? Options.cacheOnly()
1146+
: Options.matchAndFilterWithDefaultMatcher(UpdateType.JSON_PATCH));
10921147
} catch (KubernetesClientException e) {
10931148
log.trace("Exception during patch for resource: {}", resource);
10941149
retryIndex++;

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/ReconciliationDispatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private PostExecutionControl<P> handleReconcile(
137137
if (useSSA) {
138138
updatedResource = context.resourceOperations().addFinalizerWithSSA();
139139
} else {
140-
updatedResource = context.resourceOperations().addFinalizer();
140+
updatedResource = context.resourceOperations().addFinalizer(true);
141141
}
142142
return PostExecutionControl.onlyFinalizerAdded(updatedResource);
143143
} else {

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/event/ReconciliationDispatcherTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ void addFinalizerOnNewResourceWithoutSSA() throws Exception {
169169
dispatcher.handleDispatch(executionScopeWithCREvent(testCustomResource), createTestContext());
170170

171171
verify(reconciler, never()).reconcile(ArgumentMatchers.eq(testCustomResource), any());
172-
verify(mockResourceOperations, times(1)).addFinalizer();
172+
verify(mockResourceOperations, times(1)).addFinalizer(true);
173173
}
174174

175175
@Test

operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/readcacheafterwrite/filterpatchevent/FilterPatchEventTestReconciler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,12 @@ public UpdateControl<FilterPatchEventTestCustomResource> reconcile(
4242
resource.setStatus(new FilterPatchEventTestCustomResourceStatus());
4343
resource.getStatus().setValue(UPDATED);
4444

45-
var uc = UpdateControl.patchStatus(resource);
45+
context.resourceOperations().jsonMergePatchPrimaryStatus(resource);
4646
if (!filterPatchEvent.get()) {
47-
uc = uc.reschedule();
47+
return UpdateControl.<FilterPatchEventTestCustomResource>noUpdate().reschedule();
48+
} else {
49+
return UpdateControl.noUpdate();
4850
}
49-
return uc;
5051
}
5152

5253
public int getNumberOfExecutions() {

operator-framework/src/test/java/io/javaoperatorsdk/operator/baseapi/simple/ReconcilerExecutorIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void configMapGetsCreatedForTestCustomResource() {
5252

5353
awaitResourcesCreatedOrUpdated();
5454
awaitStatusUpdated();
55-
assertThat(TestUtils.getNumberOfExecutions(operator)).isEqualTo(1);
55+
assertThat(TestUtils.getNumberOfExecutions(operator)).isEqualTo(2);
5656
}
5757

5858
@Test

0 commit comments

Comments
 (0)