Skip to content

Commit b339d9d

Browse files
committed
improve: KubernetesDependentResource uses resource operations directly
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent a3bc9e7 commit b339d9d

File tree

2 files changed

+38
-48
lines changed

2 files changed

+38
-48
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,23 @@ public <R extends HasMetadata> R update(R resource) {
189189
return resourcePatch(resource, r -> context.getClient().resource(r).update());
190190
}
191191

192+
/**
193+
* Creates the resource and caches the response if needed, thus making sure that next
194+
* reconciliation will see to updated resource - or more recent one if additional update happened
195+
* after this update; In addition to that it filters out the event from this update, so
196+
* reconciliation is not triggered by own update.
197+
*
198+
* <p>You are free to control the optimistic locking by setting the resource version in resource
199+
* metadata.
200+
*
201+
* @param resource resource to update
202+
* @return updated resource
203+
* @param <R> resource type
204+
*/
205+
public <R extends HasMetadata> R create(R resource) {
206+
return resourcePatch(resource, r -> context.getClient().resource(r).create());
207+
}
208+
192209
/**
193210
* Updates the resource status subresource.
194211
*

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependentResource.java

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

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

74-
@Override
75-
protected R handleCreate(R desired, P primary, Context<P> context) {
76-
return eventSource()
77-
.orElseThrow()
78-
.eventFilteringUpdateAndCacheResource(
79-
desired,
80-
toCreate -> KubernetesDependentResource.super.handleCreate(toCreate, primary, context));
81-
}
82-
83-
@Override
84-
protected R handleUpdate(R actual, R desired, P primary, Context<P> context) {
85-
return eventSource()
86-
.orElseThrow()
87-
.eventFilteringUpdateAndCacheResource(
88-
desired,
89-
toUpdate ->
90-
KubernetesDependentResource.super.handleUpdate(actual, toUpdate, primary, context));
91-
}
92-
9373
@SuppressWarnings("unused")
9474
public R create(R desired, P primary, Context<P> context) {
95-
if (useSSA(context)) {
75+
var ssa = useSSA(context);
76+
if (ssa) {
9677
// setting resource version for SSA so only created if it doesn't exist already
9778
var createIfNotExisting =
9879
kubernetesDependentResourceConfig == null
@@ -104,35 +85,38 @@ public R create(R desired, P primary, Context<P> context) {
10485
}
10586
}
10687
addMetadata(false, null, desired, primary, context);
107-
final var resource = prepare(context, desired, primary, "Creating");
108-
return useSSA(context)
109-
? resource
110-
.fieldManager(context.getControllerConfiguration().fieldManager())
111-
.forceConflicts()
112-
.serverSideApply()
113-
: resource.create();
88+
log.debug(
89+
"Creating target resource with type: {}, with id: {} use ssa: {}",
90+
desired.getClass(),
91+
ResourceID.fromResource(desired),
92+
ssa);
93+
94+
return ssa
95+
? context.resourceOperations().serverSideApply(desired)
96+
: context.resourceOperations().create(desired);
11497
}
11598

11699
public R update(R actual, R desired, P primary, Context<P> context) {
117-
boolean useSSA = useSSA(context);
100+
boolean ssa = useSSA(context);
118101
if (log.isDebugEnabled()) {
119102
log.debug(
120103
"Updating actual resource: {} version: {}; SSA: {}",
121104
ResourceID.fromResource(actual),
122105
actual.getMetadata().getResourceVersion(),
123-
useSSA);
106+
ssa);
124107
}
125108
R updatedResource;
126109
addMetadata(false, actual, desired, primary, context);
127-
if (useSSA) {
128-
updatedResource =
129-
prepare(context, desired, primary, "Updating")
130-
.fieldManager(context.getControllerConfiguration().fieldManager())
131-
.forceConflicts()
132-
.serverSideApply();
110+
log.debug(
111+
"Updating target resource with type: {}, with id: {} use ssa: {}",
112+
desired.getClass(),
113+
ResourceID.fromResource(desired),
114+
ssa);
115+
if (ssa) {
116+
updatedResource = context.resourceOperations().serverSideApply(desired);
133117
} else {
134118
var updatedActual = GenericResourceUpdater.updateResource(actual, desired, context);
135-
updatedResource = prepare(context, updatedActual, primary, "Updating").update();
119+
updatedResource = context.resourceOperations().update(updatedActual);
136120
}
137121
log.debug(
138122
"Resource version after update: {}", updatedResource.getMetadata().getResourceVersion());
@@ -203,17 +187,6 @@ public void deleteTargetResource(P primary, R resource, ResourceID key, Context<
203187
context.getClient().resource(resource).delete();
204188
}
205189

206-
@SuppressWarnings("unused")
207-
protected Resource<R> prepare(Context<P> context, R desired, P primary, String actionName) {
208-
log.debug(
209-
"{} target resource with type: {}, with id: {}",
210-
actionName,
211-
desired.getClass(),
212-
ResourceID.fromResource(desired));
213-
214-
return context.getClient().resource(desired);
215-
}
216-
217190
protected void addReferenceHandlingMetadata(R desired, P primary) {
218191
if (addOwnerReference()) {
219192
desired.addOwnerReference(primary);

0 commit comments

Comments
 (0)