2828import io .fabric8 .kubernetes .client .dsl .base .PatchType ;
2929import io .javaoperatorsdk .operator .OperatorException ;
3030import io .javaoperatorsdk .operator .processing .event .ResourceID ;
31+ import io .javaoperatorsdk .operator .processing .event .source .informer .InformerEventSource ;
3132import io .javaoperatorsdk .operator .processing .event .source .informer .ManagedInformerEventSource ;
3233
3334import 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 *
0 commit comments