Skip to content

Commit f00cf05

Browse files
committed
wip
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent b339d9d commit f00cf05

File tree

2 files changed

+87
-4
lines changed

2 files changed

+87
-4
lines changed

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import io.fabric8.kubernetes.client.dsl.base.PatchType;
2929
import io.javaoperatorsdk.operator.OperatorException;
3030
import io.javaoperatorsdk.operator.processing.event.ResourceID;
31+
import io.javaoperatorsdk.operator.processing.event.source.informer.InformerEventSource;
3132
import io.javaoperatorsdk.operator.processing.event.source.informer.ManagedInformerEventSource;
3233

3334
import static io.javaoperatorsdk.operator.processing.KubernetesResourceUtils.getUID;
@@ -80,6 +81,40 @@ public <R extends HasMetadata> R serverSideApply(R resource) {
8081
.build()));
8182
}
8283

84+
/**
85+
* Updates the resource and caches the response if needed, thus making sure that next
86+
* reconciliation will see to updated resource - or more recent one if additional update happened
87+
* after this update; In addition to that it filters out the event from the update, so
88+
* reconciliation is not triggered by own update.
89+
*
90+
* <p>You are free to control the optimistic locking by setting the resource version in resource
91+
* metadata. In case of SSA we advise not to do updates with optimistic locking.
92+
*
93+
* @param resource fresh resource for server side apply
94+
* @return updated resource
95+
* @param informerEventSource InformerEventSource to use for resource caching and filtering
96+
* @param <R> resource type
97+
*/
98+
public <R extends HasMetadata> R serverSideApply(
99+
R resource, InformerEventSource<R, P> informerEventSource) {
100+
if (informerEventSource == null) {
101+
return serverSideApply(resource);
102+
}
103+
return resourcePatch(
104+
resource,
105+
r ->
106+
context
107+
.getClient()
108+
.resource(r)
109+
.patch(
110+
new PatchContext.Builder()
111+
.withForce(true)
112+
.withFieldManager(context.getControllerConfiguration().fieldManager())
113+
.withPatchType(PatchType.SERVER_SIDE_APPLY)
114+
.build()),
115+
informerEventSource);
116+
}
117+
83118
/**
84119
* Server-Side Apply the resource status subresource.
85120
*
@@ -189,6 +224,29 @@ public <R extends HasMetadata> R update(R resource) {
189224
return resourcePatch(resource, r -> context.getClient().resource(r).update());
190225
}
191226

227+
/**
228+
* Updates the resource and caches the response if needed, thus making sure that next
229+
* reconciliation will see to updated resource - or more recent one if additional update happened
230+
* after this update; In addition to that it filters out the event from this update, so
231+
* reconciliation is not triggered by own update.
232+
*
233+
* <p>You are free to control the optimistic locking by setting the resource version in resource
234+
* metadata.
235+
*
236+
* @param resource resource to update
237+
* @return updated resource
238+
* @param informerEventSource InformerEventSource to use for resource caching and filtering
239+
* @param <R> resource type
240+
*/
241+
public <R extends HasMetadata> R update(
242+
R resource, InformerEventSource<R, P> informerEventSource) {
243+
if (informerEventSource == null) {
244+
return update(resource);
245+
}
246+
return resourcePatch(
247+
resource, r -> context.getClient().resource(r).update(), informerEventSource);
248+
}
249+
192250
/**
193251
* Creates the resource and caches the response if needed, thus making sure that next
194252
* reconciliation will see to updated resource - or more recent one if additional update happened
@@ -206,6 +264,29 @@ public <R extends HasMetadata> R create(R resource) {
206264
return resourcePatch(resource, r -> context.getClient().resource(r).create());
207265
}
208266

267+
/**
268+
* Creates the resource and caches the response if needed, thus making sure that next
269+
* reconciliation will see to updated resource - or more recent one if additional update happened
270+
* after this update; In addition to that it filters out the event from this update, so
271+
* reconciliation is not triggered by own update.
272+
*
273+
* <p>You are free to control the optimistic locking by setting the resource version in resource
274+
* metadata.
275+
*
276+
* @param resource resource to update
277+
* @return updated resource
278+
* @param informerEventSource InformerEventSource to use for resource caching and filtering
279+
* @param <R> resource type
280+
*/
281+
public <R extends HasMetadata> R create(
282+
R resource, InformerEventSource<R, P> informerEventSource) {
283+
if (informerEventSource == null) {
284+
return create(resource);
285+
}
286+
return resourcePatch(
287+
resource, r -> context.getClient().resource(r).create(), informerEventSource);
288+
}
289+
209290
/**
210291
* Updates the resource status subresource.
211292
*

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ public R create(R desired, P primary, Context<P> context) {
9292
ssa);
9393

9494
return ssa
95-
? context.resourceOperations().serverSideApply(desired)
96-
: context.resourceOperations().create(desired);
95+
? context.resourceOperations().serverSideApply(desired, eventSource().orElse(null))
96+
: context.resourceOperations().create(desired, eventSource().orElse(null));
9797
}
9898

9999
public R update(R actual, R desired, P primary, Context<P> context) {
@@ -113,10 +113,12 @@ public R update(R actual, R desired, P primary, Context<P> context) {
113113
ResourceID.fromResource(desired),
114114
ssa);
115115
if (ssa) {
116-
updatedResource = context.resourceOperations().serverSideApply(desired);
116+
updatedResource =
117+
context.resourceOperations().serverSideApply(desired, eventSource().orElse(null));
117118
} else {
118119
var updatedActual = GenericResourceUpdater.updateResource(actual, desired, context);
119-
updatedResource = context.resourceOperations().update(updatedActual);
120+
updatedResource =
121+
context.resourceOperations().update(updatedActual, eventSource().orElse(null));
120122
}
121123
log.debug(
122124
"Resource version after update: {}", updatedResource.getMetadata().getResourceVersion());

0 commit comments

Comments
 (0)