feat: move ReconcileUtils methods to ResourceOperations accessible from Context by csviri · Pull Request #3142 · operator-framework/java-operator-sdk · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class DefaultContext<P extends HasMetadata> implements Context<P> {
private final boolean primaryResourceDeleted;
private final boolean primaryResourceFinalStateUnknown;
private final Map<DependentResource<?, P>, Object> desiredStates = new ConcurrentHashMap<>();
private final ResourceOperations<P> resourceOperations;

public DefaultContext(
RetryInfo retryInfo,
Expand All @@ -61,6 +62,7 @@ public DefaultContext(
this.primaryResourceFinalStateUnknown = primaryResourceFinalStateUnknown;
this.defaultManagedDependentResourceContext =
new DefaultManagedWorkflowAndDependentResourceContext<>(controller, primaryResource, this);
this.resourceOperations = new ResourceOperations<>(this);
}

@Override
Expand Down Expand Up @@ -124,6 +126,11 @@ public KubernetesClient getClient() {
return controller.getClient();
}

@Override
public ResourceOperations<P> resourceOperations() {
return resourceOperations;
}

@Override
public ExecutorService getWorkflowExecutorService() {
// note that this should be always received from executor service manager, so we are able to do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
* If the update fails, it reads the primary resource from the cluster, applies the modifications
* again and retries the update.
*
* @deprecated Use {@link ReconcileUtils} that contains the more efficient up-to-date versions of
* the target utils.
* @deprecated Use {@link Context#resourceOperations()} that contains the more efficient up-to-date
* versions of methods.
*/
@Deprecated(forRemoval = true)
public class PrimaryUpdateAndCacheUtils {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.api.reconciler.DefaultContext;
import io.javaoperatorsdk.operator.api.reconciler.DeleteControl;
import io.javaoperatorsdk.operator.api.reconciler.ReconcileUtils;
import io.javaoperatorsdk.operator.api.reconciler.RetryInfo;
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
import io.javaoperatorsdk.operator.processing.Controller;
Expand Down Expand Up @@ -73,13 +72,14 @@ public ReconciliationDispatcher(Controller<P> controller) {
public PostExecutionControl<P> handleExecution(ExecutionScope<P> executionScope) {
validateExecutionScope(executionScope);
try {
return handleDispatch(executionScope);
return handleDispatch(executionScope, null);
} catch (Exception e) {
return PostExecutionControl.exceptionDuringExecution(e);
}
}

private PostExecutionControl<P> handleDispatch(ExecutionScope<P> executionScope)
// visible for testing
PostExecutionControl<P> handleDispatch(ExecutionScope<P> executionScope, Context<P> context)
throws Exception {
P originalResource = executionScope.getResource();
var resourceForExecution = cloneResource(originalResource);
Expand All @@ -98,13 +98,16 @@ && shouldNotDispatchToCleanupWhenMarkedForDeletion(originalResource)) {
originalResource.getMetadata().getFinalizers());
return PostExecutionControl.defaultDispatch();
}
Context<P> context =
new DefaultContext<>(
executionScope.getRetryInfo(),
controller,
resourceForExecution,
executionScope.isDeleteEvent(),
executionScope.isDeleteFinalStateUnknown());
// context can be provided only for testing purposes
context =
context == null
? new DefaultContext<>(
executionScope.getRetryInfo(),
controller,
resourceForExecution,
executionScope.isDeleteEvent(),
executionScope.isDeleteFinalStateUnknown())
: context;

// checking the cleaner for all-event-mode
if (!triggerOnAllEvents() && markedForDeletion) {
Expand Down Expand Up @@ -137,9 +140,9 @@ private PostExecutionControl<P> handleReconcile(
*/
P updatedResource;
if (useSSA) {
updatedResource = ReconcileUtils.addFinalizerWithSSA(context);
updatedResource = context.resourceOperations().addFinalizerWithSSA();
} else {
updatedResource = ReconcileUtils.addFinalizer(context);
updatedResource = context.resourceOperations().addFinalizer();
}
return PostExecutionControl.onlyFinalizerAdded(updatedResource)
.withReSchedule(BaseControl.INSTANT_RESCHEDULE);
Expand Down Expand Up @@ -321,7 +324,7 @@ private PostExecutionControl<P> handleCleanup(
// cleanup is finished, nothing left to be done
final var finalizerName = configuration().getFinalizerName();
if (deleteControl.isRemoveFinalizer() && resourceForExecution.hasFinalizer(finalizerName)) {
P customResource = ReconcileUtils.removeFinalizer(context);
P customResource = context.resourceOperations().removeFinalizer();
return PostExecutionControl.customResourceFinalizerRemoved(customResource);
}
}
Expand Down Expand Up @@ -387,9 +390,9 @@ public R patchResource(Context<R> context, R resource, R originalResource) {
resource.getMetadata().getResourceVersion());
}
if (useSSA) {
return ReconcileUtils.serverSideApplyPrimary(context, resource);
return context.resourceOperations().serverSideApplyPrimary(resource);
} else {
return ReconcileUtils.jsonPatchPrimary(context, originalResource, r -> resource);
return context.resourceOperations().jsonPatchPrimary(originalResource, r -> resource);
}
}

Expand All @@ -399,7 +402,7 @@ public R patchStatus(Context<R> context, R resource, R originalResource) {
var managedFields = resource.getMetadata().getManagedFields();
try {
resource.getMetadata().setManagedFields(null);
return ReconcileUtils.serverSideApplyPrimaryStatus(context, resource);
return context.resourceOperations().serverSideApplyPrimaryStatus(resource);
} finally {
resource.getMetadata().setManagedFields(managedFields);
}
Expand All @@ -416,13 +419,14 @@ private R editStatus(Context<R> context, R resource, R originalResource) {
try {
clonedOriginal.getMetadata().setResourceVersion(null);
resource.getMetadata().setResourceVersion(null);
return ReconcileUtils.jsonPatchPrimaryStatus(
context,
clonedOriginal,
r -> {
ReconcilerUtilsInternal.setStatus(r, ReconcilerUtilsInternal.getStatus(resource));
return r;
});
return context
.resourceOperations()
.jsonPatchPrimaryStatus(
clonedOriginal,
r -> {
ReconcilerUtilsInternal.setStatus(r, ReconcilerUtilsInternal.getStatus(resource));
return r;
});
} finally {
// restore initial resource version
clonedOriginal.getMetadata().setResourceVersion(resourceVersion);
Expand Down
Loading