Skip to content

Commit 559afa9

Browse files
authored
imrpove: replacing confusing UpdateControl method (#1762)
1 parent 55f5d0b commit 559afa9

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed

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

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,27 @@ public class UpdateControl<P extends HasMetadata> extends BaseControl<UpdateCont
88
private final P resource;
99
private final boolean updateStatus;
1010
private final boolean updateResource;
11-
private final boolean patch;
11+
private final boolean patchStatus;
1212

1313
private UpdateControl(
14-
P resource, boolean updateStatus, boolean updateResource, boolean patch) {
14+
P resource, boolean updateStatus, boolean updateResource, boolean patchStatus) {
1515
if ((updateResource || updateStatus) && resource == null) {
1616
throw new IllegalArgumentException("CustomResource cannot be null in case of update");
1717
}
1818
this.resource = resource;
1919
this.updateStatus = updateStatus;
2020
this.updateResource = updateResource;
21-
this.patch = patch;
21+
this.patchStatus = patchStatus;
2222
}
2323

2424
/**
2525
* Creates an update control instance that instructs the framework to do an update on resource
2626
* itself, not on the status. Note that usually as a results of a reconciliation should be a
2727
* status update not an update to the resource itself.
2828
*
29+
* Using this update makes sure that the resource in the next reconciliation is the updated one -
30+
* this is not guaranteed by default if you do an update on a resource by the Kubernetes client.
31+
*
2932
* @param <T> custom resource type
3033
* @param customResource customResource to use for update
3134
* @return initialized update control
@@ -73,6 +76,9 @@ public static <T extends HasMetadata> UpdateControl<T> updateStatus(T customReso
7376
* As a results of this there will be two call to K8S API. First the custom resource will be
7477
* updates then the status sub-resource.
7578
*
79+
* Using this update makes sure that the resource in the next reconciliation is the updated one -
80+
* this is not guaranteed by default if you do an update on a resource by the Kubernetes client.
81+
*
7682
* @param <T> resource type
7783
* @param customResource - custom resource to use in both API calls
7884
* @return UpdateControl instance
@@ -82,11 +88,36 @@ public static <T extends HasMetadata> UpdateControl<T> updateResourceAndStatus(
8288
return new UpdateControl<>(customResource, true, true, false);
8389
}
8490

85-
public static <T extends HasMetadata> UpdateControl<T> patchResourceAndStatus(
91+
/**
92+
* Updates the resource - with optimistic locking - and patches the status without optimistic
93+
* locking in place.
94+
*
95+
* Note that using this method, it is not guaranteed that the most recent updated resource will be
96+
* in case for next reconciliation.
97+
*
98+
* @param customResource to update
99+
* @return UpdateControl instance
100+
* @param <T> resource type
101+
*/
102+
public static <T extends HasMetadata> UpdateControl<T> updateResourceAndPatchStatus(
86103
T customResource) {
87104
return new UpdateControl<>(customResource, true, true, true);
88105
}
89106

107+
/**
108+
* Marked for removal, because of confusing name. It does not patch the resource but rather
109+
* updates it.
110+
*
111+
* @deprecated use {@link UpdateControl#updateResourceAndPatchStatus(HasMetadata)}
112+
*
113+
* @param customResource to update
114+
* @return UpdateControl instance
115+
* @param <T> resource type
116+
*/
117+
@Deprecated(forRemoval = true)
118+
public static <T extends HasMetadata> UpdateControl<T> patchResourceAndStatus(T customResource) {
119+
return updateResourceAndStatus(customResource);
120+
}
90121

91122
public static <T extends HasMetadata> UpdateControl<T> noUpdate() {
92123
return new UpdateControl<>(null, false, false, false);
@@ -104,8 +135,8 @@ public boolean isUpdateResource() {
104135
return updateResource;
105136
}
106137

107-
public boolean isPatch() {
108-
return patch;
138+
public boolean isPatchStatus() {
139+
return patchStatus;
109140
}
110141

111142
public boolean isNoUpdate() {
@@ -115,4 +146,5 @@ public boolean isNoUpdate() {
115146
public boolean isUpdateResourceAndStatus() {
116147
return updateResource && updateStatus;
117148
}
149+
118150
}

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private PostExecutionControl<P> handleDispatch(ExecutionScope<P> executionScope)
8484
Context<P> context =
8585
new DefaultContext<>(executionScope.getRetryInfo(), controller, originalResource);
8686
if (markedForDeletion) {
87-
return handleCleanup(originalResource, resourceForExecution, context);
87+
return handleCleanup(resourceForExecution, context);
8888
} else {
8989
return handleReconcile(executionScope, resourceForExecution, originalResource, context);
9090
}
@@ -147,23 +147,24 @@ private PostExecutionControl<P> reconcileExecution(ExecutionScope<P> executionSc
147147
.setResourceVersion(updatedCustomResource.getMetadata().getResourceVersion());
148148
updatedCustomResource =
149149
updateStatusGenerationAware(updateControl.getResource(), originalResource,
150-
updateControl.isPatch());
150+
updateControl.isPatchStatus());
151151
} else if (updateControl.isUpdateStatus()) {
152152
updatedCustomResource =
153153
updateStatusGenerationAware(updateControl.getResource(), originalResource,
154-
updateControl.isPatch());
154+
updateControl.isPatchStatus());
155155
} else if (updateControl.isUpdateResource()) {
156156
updatedCustomResource =
157157
updateCustomResource(updateControl.getResource());
158158
if (shouldUpdateObservedGenerationAutomatically(updatedCustomResource)) {
159159
updatedCustomResource =
160160
updateStatusGenerationAware(updateControl.getResource(), originalResource,
161-
updateControl.isPatch());
161+
updateControl.isPatchStatus());
162162
}
163163
} else if (updateControl.isNoUpdate()
164164
&& shouldUpdateObservedGenerationAutomatically(resourceForExecution)) {
165165
updatedCustomResource =
166-
updateStatusGenerationAware(originalResource, originalResource, updateControl.isPatch());
166+
updateStatusGenerationAware(originalResource, originalResource,
167+
updateControl.isPatchStatus());
167168
}
168169
return createPostExecutionControl(updatedCustomResource, updateControl);
169170
}
@@ -259,7 +260,7 @@ private PostExecutionControl<P> createPostExecutionControl(P updatedCustomResour
259260
UpdateControl<P> updateControl) {
260261
PostExecutionControl<P> postExecutionControl;
261262
if (updatedCustomResource != null) {
262-
if (updateControl.isUpdateStatus() && updateControl.isPatch()) {
263+
if (updateControl.isUpdateStatus() && updateControl.isPatchStatus()) {
263264
postExecutionControl =
264265
PostExecutionControl.customResourceStatusPatched(updatedCustomResource);
265266
} else {
@@ -279,7 +280,7 @@ private void updatePostExecutionControlWithReschedule(
279280
}
280281

281282

282-
private PostExecutionControl<P> handleCleanup(P originalResource, P resource,
283+
private PostExecutionControl<P> handleCleanup(P resource,
283284
Context<P> context) {
284285
log.debug(
285286
"Executing delete for resource: {} with version: {}",
@@ -317,8 +318,10 @@ private P updateCustomResourceWithFinalizer(P resourceForExecution, P originalRe
317318
}
318319

319320
private P updateCustomResource(P resource) {
320-
log.debug("Updating resource: {} with version: {}", getUID(resource), getVersion(resource));
321+
log.debug("Updating resource: {} with version: {}", getUID(resource),
322+
getVersion(resource));
321323
log.trace("Resource before update: {}", resource);
324+
322325
return customResourceFacade.updateResource(resource);
323326
}
324327

0 commit comments

Comments
 (0)