Skip to content
Draft
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 @@ -31,6 +31,7 @@
import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource;
import io.javaoperatorsdk.operator.processing.event.source.informer.ManagedInformerEventSource;

import static io.javaoperatorsdk.operator.api.reconciler.Experimental.API_MIGHT_CHANGE;
import static io.javaoperatorsdk.operator.processing.KubernetesResourceUtils.getUID;
import static io.javaoperatorsdk.operator.processing.KubernetesResourceUtils.getVersion;

Expand Down Expand Up @@ -68,6 +69,11 @@ public ResourceOperations(Context<P> context) {
* @param <R> resource type
*/
public <R extends HasMetadata> R serverSideApply(R resource) {
return serverSideApply(resource, (Options) null);
}

@Experimental(API_MIGHT_CHANGE)
public <R extends HasMetadata> R serverSideApply(R resource, Options options) {
return resourcePatch(
resource,
r ->
Expand All @@ -79,7 +85,13 @@ public <R extends HasMetadata> R serverSideApply(R resource) {
.withForce(true)
.withFieldManager(context.getControllerConfiguration().fieldManager())
.withPatchType(PatchType.SERVER_SIDE_APPLY)
.build()));
.build()),
options);
}

public <R extends HasMetadata> R serverSideApply(
R resource, InformerEventSource<R, P> informerEventSource) {
return serverSideApply(resource, informerEventSource, null);
}

/**
Expand All @@ -96,8 +108,9 @@ public <R extends HasMetadata> R serverSideApply(R resource) {
* @param informerEventSource InformerEventSource to use for resource caching and filtering
* @param <R> resource type
*/
@Experimental(API_MIGHT_CHANGE)
public <R extends HasMetadata> R serverSideApply(
R resource, InformerEventSource<R, P> informerEventSource) {
R resource, InformerEventSource<R, P> informerEventSource, Options options) {
if (informerEventSource == null) {
return serverSideApply(resource);
}
Expand All @@ -113,7 +126,8 @@ public <R extends HasMetadata> R serverSideApply(
.withFieldManager(context.getControllerConfiguration().fieldManager())
.withPatchType(PatchType.SERVER_SIDE_APPLY)
.build()),
informerEventSource);
informerEventSource,
options);
}

/**
Expand All @@ -131,7 +145,7 @@ public <R extends HasMetadata> R serverSideApply(
* @return updated resource
* @param <R> resource type
*/
public <R extends HasMetadata> R serverSideApplyStatus(R resource) {
public <R extends HasMetadata> R serverSideApplyStatus(R resource, Options options) {
return resourcePatch(
resource,
r ->
Expand All @@ -144,7 +158,12 @@ public <R extends HasMetadata> R serverSideApplyStatus(R resource) {
.withForce(true)
.withFieldManager(context.getControllerConfiguration().fieldManager())
.withPatchType(PatchType.SERVER_SIDE_APPLY)
.build()));
.build()),
options);
}

public <R extends HasMetadata> R serverSideApplyStatus(R resource) {
return serverSideApplyStatus(resource, null);
}

/**
Expand Down Expand Up @@ -208,6 +227,10 @@ public P serverSideApplyPrimaryStatus(P resource) {
context.eventSourceRetriever().getControllerEventSource());
}

public <R extends HasMetadata> R update(R resource) {
return update(resource, (Options) null);
}

/**
* Updates the resource and caches the response if needed, thus making sure that next
* reconciliation will see to updated resource - or more recent one if additional update happened
Expand All @@ -221,8 +244,14 @@ public P serverSideApplyPrimaryStatus(P resource) {
* @return updated resource
* @param <R> resource type
*/
public <R extends HasMetadata> R update(R resource) {
return resourcePatch(resource, r -> context.getClient().resource(r).update());
@Experimental(API_MIGHT_CHANGE)
public <R extends HasMetadata> R update(R resource, Options options) {
return resourcePatch(resource, r -> context.getClient().resource(r).update(), options);
}

public <R extends HasMetadata> R update(
R resource, InformerEventSource<R, P> informerEventSource) {
return update(resource, informerEventSource, new Options(true));
}

/**
Expand All @@ -239,13 +268,14 @@ public <R extends HasMetadata> R update(R resource) {
* @param informerEventSource InformerEventSource to use for resource caching and filtering
* @param <R> resource type
*/
@Experimental(API_MIGHT_CHANGE)
public <R extends HasMetadata> R update(
R resource, InformerEventSource<R, P> informerEventSource) {
R resource, InformerEventSource<R, P> informerEventSource, Options options) {
if (informerEventSource == null) {
return update(resource);
}
return resourcePatch(
resource, r -> context.getClient().resource(r).update(), informerEventSource);
resource, r -> context.getClient().resource(r).update(), informerEventSource, options);
}

/**
Expand All @@ -262,7 +292,8 @@ public <R extends HasMetadata> R update(
* @param <R> resource type
*/
public <R extends HasMetadata> R create(R resource) {
return resourcePatch(resource, r -> context.getClient().resource(r).create());
return resourcePatch(
resource, r -> context.getClient().resource(r).create(), new Options(true));
}

/**
Expand All @@ -284,8 +315,12 @@ public <R extends HasMetadata> R create(
if (informerEventSource == null) {
return create(resource);
}
// it is safe to do event filtering for create since check if the resource already exists.
return resourcePatch(
resource, r -> context.getClient().resource(r).create(), informerEventSource);
resource,
r -> context.getClient().resource(r).create(),
informerEventSource,
new Options(true));
}

/**
Expand All @@ -304,7 +339,8 @@ public <R extends HasMetadata> R create(
* @param <R> resource type
*/
public <R extends HasMetadata> R updateStatus(R resource) {
return resourcePatch(resource, r -> context.getClient().resource(r).updateStatus());
return resourcePatch(
resource, r -> context.getClient().resource(r).updateStatus(), new Options(true));
}

/**
Expand Down Expand Up @@ -367,7 +403,13 @@ public P updatePrimaryStatus(P resource) {
* @param <R> resource type
*/
public <R extends HasMetadata> R jsonPatch(R resource, UnaryOperator<R> unaryOperator) {
return resourcePatch(resource, r -> context.getClient().resource(r).edit(unaryOperator));
return jsonPatch(resource, unaryOperator, null);
}

public <R extends HasMetadata> R jsonPatch(
R resource, UnaryOperator<R> unaryOperator, Options options) {
return resourcePatch(
resource, r -> context.getClient().resource(r).edit(unaryOperator), options);
}

/**
Expand All @@ -387,8 +429,14 @@ public <R extends HasMetadata> R jsonPatch(R resource, UnaryOperator<R> unaryOpe
* @return updated resource
* @param <R> resource type
*/
public <R extends HasMetadata> R jsonPatchStatus(
R resource, UnaryOperator<R> unaryOperator, Options options) {
return resourcePatch(
resource, r -> context.getClient().resource(r).editStatus(unaryOperator), options);
}

public <R extends HasMetadata> R jsonPatchStatus(R resource, UnaryOperator<R> unaryOperator) {
return resourcePatch(resource, r -> context.getClient().resource(r).editStatus(unaryOperator));
return jsonPatchStatus(resource, unaryOperator, null);
}

/**
Expand Down Expand Up @@ -452,7 +500,11 @@ public P jsonPatchPrimaryStatus(P resource, UnaryOperator<P> unaryOperator) {
* @param <R> resource type
*/
public <R extends HasMetadata> R jsonMergePatch(R resource) {
return resourcePatch(resource, r -> context.getClient().resource(r).patch());
return jsonMergePatch(resource, null);
}

public <R extends HasMetadata> R jsonMergePatch(R resource, Options options) {
return resourcePatch(resource, r -> context.getClient().resource(r).patch(), options);
}

/**
Expand All @@ -471,7 +523,11 @@ public <R extends HasMetadata> R jsonMergePatch(R resource) {
* @param <R> resource type
*/
public <R extends HasMetadata> R jsonMergePatchStatus(R resource) {
return resourcePatch(resource, r -> context.getClient().resource(r).patchStatus());
return jsonMergePatchStatus(resource, null);
}

public <R extends HasMetadata> R jsonMergePatchStatus(R resource, Options options) {
return resourcePatch(resource, r -> context.getClient().resource(r).patchStatus(), options);
}

/**
Expand Down Expand Up @@ -518,6 +574,10 @@ public P jsonMergePatchPrimaryStatus(P resource) {
context.eventSourceRetriever().getControllerEventSource());
}

public <R extends HasMetadata> R resourcePatch(R resource, UnaryOperator<R> updateOperation) {
return resourcePatch(resource, updateOperation, (Options) null);
}

/**
* Utility method to patch a resource and cache the result. Automatically discovers the event
* source for the resource type and delegates to {@link #resourcePatch(HasMetadata, UnaryOperator,
Expand All @@ -529,8 +589,10 @@ public P jsonMergePatchPrimaryStatus(P resource) {
* @param <R> resource type
* @throws IllegalStateException if no event source or multiple event sources are found
*/
@Experimental(API_MIGHT_CHANGE)
@SuppressWarnings({"rawtypes", "unchecked"})
public <R extends HasMetadata> R resourcePatch(R resource, UnaryOperator<R> updateOperation) {
public <R extends HasMetadata> R resourcePatch(
R resource, UnaryOperator<R> updateOperation, Options options) {

var esList = context.eventSourceRetriever().getEventSourcesFor(resource.getClass());
if (esList.isEmpty()) {
Expand All @@ -544,7 +606,8 @@ public <R extends HasMetadata> R resourcePatch(R resource, UnaryOperator<R> upda
es.name());
}
if (es instanceof ManagedInformerEventSource mes) {
return resourcePatch(resource, updateOperation, (ManagedInformerEventSource<R, P, ?>) mes);
return resourcePatch(
resource, updateOperation, (ManagedInformerEventSource<R, P, ?>) mes, options);
} else {
throw new IllegalStateException(
"Target event source must be a subclass off "
Expand All @@ -565,7 +628,16 @@ public <R extends HasMetadata> R resourcePatch(R resource, UnaryOperator<R> upda
*/
public <R extends HasMetadata> R resourcePatch(
R resource, UnaryOperator<R> updateOperation, ManagedInformerEventSource<R, P, ?> ies) {
return ies.eventFilteringUpdateAndCacheResource(resource, updateOperation);
return resourcePatch(resource, updateOperation, ies, (Options) null);
}

public <R extends HasMetadata> R resourcePatch(
R resource,
UnaryOperator<R> updateOperation,
ManagedInformerEventSource<R, P, ?> ies,
Options options) {
return ies.eventFilteringUpdateAndCacheResource(
resource, updateOperation, options != null && options.forceEventFiltering());
}

/**
Expand Down Expand Up @@ -754,4 +826,12 @@ public P addFinalizerWithSSA(String finalizerName) {
e);
}
}

/**
* Force filtering only if it is made sure that the update not results on a no-op change. See
* details here: <a href="https://github.com/operator-framework/java-operator-sdk/pull/3484">PR
* 3484</a>
*/
@Experimental("This API might change")
public record Options(boolean forceEventFiltering) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
import io.javaoperatorsdk.operator.api.reconciler.Ignore;
import io.javaoperatorsdk.operator.api.reconciler.ResourceOperations;
import io.javaoperatorsdk.operator.api.reconciler.dependent.GarbageCollected;
import io.javaoperatorsdk.operator.api.reconciler.dependent.managed.ConfiguredDependentResource;
import io.javaoperatorsdk.operator.processing.GroupVersionKind;
Expand Down Expand Up @@ -90,7 +91,6 @@ public R create(R desired, P primary, Context<P> context) {
desired.getClass(),
ResourceID.fromResource(desired),
ssa);

return ssa
? context.resourceOperations().serverSideApply(desired, eventSource().orElse(null))
: context.resourceOperations().create(desired, eventSource().orElse(null));
Expand All @@ -114,7 +114,10 @@ public R update(R actual, R desired, P primary, Context<P> context) {
ssa);
if (ssa) {
updatedResource =
context.resourceOperations().serverSideApply(desired, eventSource().orElse(null));
context
.resourceOperations()
.serverSideApply(
desired, eventSource().orElse(null), new ResourceOperations.Options(true));
} else {
var updatedActual = GenericResourceUpdater.updateResource(actual, desired, context);
updatedResource =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ private PostExecutionControl<P> handleReconcile(
} else {
updatedResource = context.resourceOperations().addFinalizer();
}
return PostExecutionControl.onlyFinalizerAdded(updatedResource)
.withReSchedule(BaseControl.INSTANT_RESCHEDULE);
return PostExecutionControl.onlyFinalizerAdded(updatedResource);
} else {
try {
return reconcileExecution(executionScope, resourceForExecution, originalResource, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
import io.javaoperatorsdk.operator.api.config.Informable;
import io.javaoperatorsdk.operator.api.config.NamespaceChangeable;
import io.javaoperatorsdk.operator.api.reconciler.Experimental;
import io.javaoperatorsdk.operator.api.reconciler.dependent.RecentOperationCacheFiller;
import io.javaoperatorsdk.operator.health.InformerHealthIndicator;
import io.javaoperatorsdk.operator.health.InformerWrappingEventSourceHealthIndicator;
Expand Down Expand Up @@ -88,13 +89,27 @@ public void changeNamespaces(Set<String> namespaces) {
}
}

@Experimental(
"Internal API. Use ResourceOperation not this directly, this API might change in the future")
public R eventFilteringUpdateAndCacheResource(R resourceToUpdate, UnaryOperator<R> updateMethod) {
return eventFilteringUpdateAndCacheResource(resourceToUpdate, updateMethod, false);
}

/**
* Updates the resource and makes sure that the response is available for the next reconciliation.
* Also makes sure that the even produced by this update is filtered, thus does not trigger the
* reconciliation.
*/
@Experimental(
"Internal API. Use ResourceOperation not this directly, this API might change in the future")
@SuppressWarnings("unchecked")
public R eventFilteringUpdateAndCacheResource(R resourceToUpdate, UnaryOperator<R> updateMethod) {
public R eventFilteringUpdateAndCacheResource(
R resourceToUpdate, UnaryOperator<R> updateMethod, boolean forceUpdateFilter) {
if (resourceToUpdate.getMetadata().getResourceVersion() == null && !forceUpdateFilter) {
log.debug("No resourceVersion set. Skipping event filtering.");
return updateAndCacheResource(resourceToUpdate, updateMethod);
}

ResourceID id = ResourceID.fromResource(resourceToUpdate);
log.debug("Starting event filtering and caching update for id={}", id);
R updatedResource = null;
Expand Down Expand Up @@ -134,6 +149,12 @@ public R eventFilteringUpdateAndCacheResource(R resourceToUpdate, UnaryOperator<
}
}

private R updateAndCacheResource(R resourceToUpdate, UnaryOperator<R> updateOperation) {
var result = updateOperation.apply(resourceToUpdate);
handleRecentResourceUpdate(ResourceID.fromResource(resourceToUpdate), result, resourceToUpdate);
return result;
}

protected abstract void handleEvent(
ResourceAction action,
R resource,
Expand Down
Loading
Loading