Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -189,6 +189,23 @@ public <R extends HasMetadata> R update(R resource) {
return resourcePatch(resource, r -> context.getClient().resource(r).update());
}

/**
* Creates 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
* after this update; In addition to that it filters out the event from this update, so
* reconciliation is not triggered by own update.
*
* <p>You are free to control the optimistic locking by setting the resource version in resource
* metadata.
*
* @param resource resource to update
* @return updated resource
* @param <R> resource type
*/
public <R extends HasMetadata> R create(R resource) {
return resourcePatch(resource, r -> context.getClient().resource(r).create());
}

/**
* Updates the resource status subresource.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Namespaced;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.javaoperatorsdk.operator.api.config.dependent.Configured;
import io.javaoperatorsdk.operator.api.config.informer.InformerEventSourceConfiguration;
import io.javaoperatorsdk.operator.api.reconciler.Context;
Expand Down Expand Up @@ -71,28 +70,10 @@ public void configureWith(KubernetesDependentResourceConfig<R> config) {
this.kubernetesDependentResourceConfig = config;
}

@Override
protected R handleCreate(R desired, P primary, Context<P> context) {
return eventSource()
.orElseThrow()
.eventFilteringUpdateAndCacheResource(
desired,
toCreate -> KubernetesDependentResource.super.handleCreate(toCreate, primary, context));
}

@Override
protected R handleUpdate(R actual, R desired, P primary, Context<P> context) {
return eventSource()
.orElseThrow()
.eventFilteringUpdateAndCacheResource(
desired,
toUpdate ->
KubernetesDependentResource.super.handleUpdate(actual, toUpdate, primary, context));
}

@SuppressWarnings("unused")
public R create(R desired, P primary, Context<P> context) {
if (useSSA(context)) {
var ssa = useSSA(context);
if (ssa) {
// setting resource version for SSA so only created if it doesn't exist already
var createIfNotExisting =
kubernetesDependentResourceConfig == null
Expand All @@ -104,35 +85,38 @@ public R create(R desired, P primary, Context<P> context) {
}
}
addMetadata(false, null, desired, primary, context);
final var resource = prepare(context, desired, primary, "Creating");
return useSSA(context)
? resource
.fieldManager(context.getControllerConfiguration().fieldManager())
.forceConflicts()
.serverSideApply()
: resource.create();
log.debug(
"Creating target resource with type: {}, with id: {} use ssa: {}",
desired.getClass(),
ResourceID.fromResource(desired),
ssa);

return ssa
? context.resourceOperations().serverSideApply(desired)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably we should have a variant for these methods where we can specify event source. Will add it.

: context.resourceOperations().create(desired);
}

public R update(R actual, R desired, P primary, Context<P> context) {
boolean useSSA = useSSA(context);
boolean ssa = useSSA(context);
if (log.isDebugEnabled()) {
log.debug(
"Updating actual resource: {} version: {}; SSA: {}",
ResourceID.fromResource(actual),
actual.getMetadata().getResourceVersion(),
useSSA);
ssa);
}
R updatedResource;
addMetadata(false, actual, desired, primary, context);
if (useSSA) {
updatedResource =
prepare(context, desired, primary, "Updating")
.fieldManager(context.getControllerConfiguration().fieldManager())
.forceConflicts()
.serverSideApply();
log.debug(
"Updating target resource with type: {}, with id: {} use ssa: {}",
desired.getClass(),
ResourceID.fromResource(desired),
ssa);
if (ssa) {
updatedResource = context.resourceOperations().serverSideApply(desired);
} else {
var updatedActual = GenericResourceUpdater.updateResource(actual, desired, context);
updatedResource = prepare(context, updatedActual, primary, "Updating").update();
updatedResource = context.resourceOperations().update(updatedActual);
}
log.debug(
"Resource version after update: {}", updatedResource.getMetadata().getResourceVersion());
Expand Down Expand Up @@ -203,17 +187,6 @@ public void deleteTargetResource(P primary, R resource, ResourceID key, Context<
context.getClient().resource(resource).delete();
}

@SuppressWarnings("unused")
protected Resource<R> prepare(Context<P> context, R desired, P primary, String actionName) {
log.debug(
"{} target resource with type: {}, with id: {}",
actionName,
desired.getClass(),
ResourceID.fromResource(desired));

return context.getClient().resource(desired);
}

protected void addReferenceHandlingMetadata(R desired, P primary) {
if (addOwnerReference()) {
desired.addOwnerReference(primary);
Expand Down
Loading