Skip to content
Closed
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
30 changes: 0 additions & 30 deletions .github/workflows/fabric8-next-version-schedule.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.javaoperatorsdk.operator.api.reconciler.RetryInfo;
import io.javaoperatorsdk.operator.api.reconciler.UpdateControl;
import io.javaoperatorsdk.operator.processing.Controller;
import io.javaoperatorsdk.operator.processing.event.source.controller.ControllerEventSource;

import static io.javaoperatorsdk.operator.processing.KubernetesResourceUtils.*;

Expand Down Expand Up @@ -60,7 +61,8 @@ public ReconciliationDispatcher(Controller<P> controller) {
new CustomResourceFacade<>(
controller.getCRClient(),
controller.getConfiguration(),
controller.getConfiguration().getConfigurationService().getResourceCloner()));
controller.getConfiguration().getConfigurationService().getResourceCloner(),
controller.getEventSourceManager().getControllerEventSource()));
}

public PostExecutionControl<P> handleExecution(ExecutionScope<P> executionScope) {
Expand Down Expand Up @@ -175,7 +177,7 @@ private PostExecutionControl<P> reconcileExecution(
}

if (updateControl.isPatchStatus()) {
customResourceFacade.patchStatus(toUpdate, originalResource);
updatedCustomResource = customResourceFacade.patchStatus(toUpdate, originalResource);
}
return createPostExecutionControl(updatedCustomResource, updateControl);
}
Expand Down Expand Up @@ -315,7 +317,7 @@ private P addFinalizerWithSSA(P originalResource) {
objectMeta.setNamespace(originalResource.getMetadata().getNamespace());
resource.setMetadata(objectMeta);
resource.addFinalizer(configuration().getFinalizerName());
return customResourceFacade.patchResourceWithSSA(resource);
return customResourceFacade.patchResourceWithSSA(resource, originalResource);
} catch (InstantiationException
| IllegalAccessException
| InvocationTargetException
Expand Down Expand Up @@ -414,15 +416,30 @@ static class CustomResourceFacade<R extends HasMetadata> {
private final boolean useSSA;
private final String fieldManager;
private final Cloner cloner;
private final boolean previousAnnotationForDependentResourcesEventFiltering;
private final ControllerEventSource<R> controllerEventSource;

public CustomResourceFacade(
MixedOperation<R, KubernetesResourceList<R>, Resource<R>> resourceOperation,
ControllerConfiguration<R> configuration,
Cloner cloner) {
Cloner cloner,
ControllerEventSource<R> controllerEventSource) {
this.resourceOperation = resourceOperation;
this.useSSA = configuration.getConfigurationService().useSSAToPatchPrimaryResource();
this.fieldManager = configuration.fieldManager();
this.previousAnnotationForDependentResourcesEventFiltering =
configuration
.getConfigurationService()
.previousAnnotationForDependentResourcesEventFiltering();
this.cloner = cloner;
this.controllerEventSource = controllerEventSource;
}

private void cachePrimaryResource(R updatedResource, R previousVersionOfResource) {
if (previousAnnotationForDependentResourcesEventFiltering) {
controllerEventSource.handleRecentResourceUpdate(
ResourceID.fromResource(updatedResource), updatedResource, previousVersionOfResource);
}
}

public R getResource(String namespace, String name) {
Expand All @@ -434,7 +451,9 @@ public R getResource(String namespace, String name) {
}

public R patchResourceWithoutSSA(R resource, R originalResource) {
return resource(originalResource).edit(r -> resource);
R updated = resource(originalResource).edit(r -> resource);
cachePrimaryResource(updated, originalResource);
return updated;
}

public R patchResource(R resource, R originalResource) {
Expand All @@ -444,32 +463,43 @@ public R patchResource(R resource, R originalResource) {
ResourceID.fromResource(resource),
resource.getMetadata().getResourceVersion());
}
R updated;
if (useSSA) {
return patchResourceWithSSA(resource);
return patchResourceWithSSA(resource, originalResource);
} else {
return resource(originalResource).edit(r -> resource);
updated = resource(originalResource).edit(r -> resource);
cachePrimaryResource(updated, originalResource);
return updated;
}
}

public R patchStatus(R resource, R originalResource) {
log.trace("Patching status for resource: {} with ssa: {}", resource, useSSA);
if (useSSA) {
R updated = null;
var managedFields = resource.getMetadata().getManagedFields();
try {
resource.getMetadata().setManagedFields(null);
var res = resource(resource);
return res.subresource("status")
.patch(
new PatchContext.Builder()
.withFieldManager(fieldManager)
.withForce(true)
.withPatchType(PatchType.SERVER_SIDE_APPLY)
.build());
updated =
res.subresource("status")
.patch(
new PatchContext.Builder()
.withFieldManager(fieldManager)
.withForce(true)
.withPatchType(PatchType.SERVER_SIDE_APPLY)
.build());
return updated;
} finally {
resource.getMetadata().setManagedFields(managedFields);
if (updated != null) {
cachePrimaryResource(updated, originalResource);
}
}
} else {
return editStatus(resource, originalResource);
R updated = editStatus(resource, originalResource);
cachePrimaryResource(updated, originalResource);
return updated;
}
}

Expand All @@ -490,14 +520,17 @@ private R editStatus(R resource, R originalResource) {
}
}

public R patchResourceWithSSA(R resource) {
return resource(resource)
.patch(
new PatchContext.Builder()
.withFieldManager(fieldManager)
.withForce(true)
.withPatchType(PatchType.SERVER_SIDE_APPLY)
.build());
public R patchResourceWithSSA(R resource, R originalResource) {
R updated =
resource(resource)
.patch(
new PatchContext.Builder()
.withFieldManager(fieldManager)
.withForce(true)
.withPatchType(PatchType.SERVER_SIDE_APPLY)
.build());
cachePrimaryResource(updated, originalResource);
return updated;
}

private Resource<R> resource(R resource) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ void addFinalizerOnNewResource() {
verify(reconciler, never()).reconcile(ArgumentMatchers.eq(testCustomResource), any());
verify(customResourceFacade, times(1))
.patchResourceWithSSA(
argThat(testCustomResource -> testCustomResource.hasFinalizer(DEFAULT_FINALIZER)));
argThat(testCustomResource -> testCustomResource.hasFinalizer(DEFAULT_FINALIZER)),
any());
}

@Test
Expand Down Expand Up @@ -357,13 +358,13 @@ void doesNotUpdateTheResourceIfNoUpdateUpdateControlIfFinalizerSet() {
void addsFinalizerIfNotMarkedForDeletionAndEmptyCustomResourceReturned() {
removeFinalizers(testCustomResource);
reconciler.reconcile = (r, c) -> UpdateControl.noUpdate();
when(customResourceFacade.patchResourceWithSSA(any())).thenReturn(testCustomResource);
when(customResourceFacade.patchResourceWithSSA(any(), any())).thenReturn(testCustomResource);

var postExecControl =
reconciliationDispatcher.handleExecution(executionScopeWithCREvent(testCustomResource));

verify(customResourceFacade, times(1))
.patchResourceWithSSA(argThat(a -> !a.getMetadata().getFinalizers().isEmpty()));
.patchResourceWithSSA(argThat(a -> !a.getMetadata().getFinalizers().isEmpty()), any());
assertThat(postExecControl.updateIsStatusPatch()).isFalse();
assertThat(postExecControl.getUpdatedCustomResource()).isPresent();
}
Expand Down
Loading