88import io .fabric8 .kubernetes .client .CustomResource ;
99import io .fabric8 .kubernetes .client .dsl .MixedOperation ;
1010import io .fabric8 .kubernetes .client .dsl .Resource ;
11+ import io .fabric8 .kubernetes .client .dsl .internal .HasMetadataOperationsImpl ;
1112import io .javaoperatorsdk .operator .api .ObservedGenerationAware ;
1213import io .javaoperatorsdk .operator .api .config .ConfigurationServiceProvider ;
1314import io .javaoperatorsdk .operator .api .config .ControllerConfiguration ;
@@ -134,7 +135,7 @@ private PostExecutionControl<R> reconcileExecution(ExecutionScope<R> executionSc
134135 R updatedCustomResource = null ;
135136 if (updateControl .isUpdateResourceAndStatus ()) {
136137 updatedCustomResource =
137- updateCustomResource (updateControl .getResource (), updateControl . isPatch () );
138+ updateCustomResource (updateControl .getResource ());
138139 updateControl
139140 .getResource ()
140141 .getMetadata ()
@@ -146,7 +147,7 @@ private PostExecutionControl<R> reconcileExecution(ExecutionScope<R> executionSc
146147 updateStatusGenerationAware (updateControl .getResource (), updateControl .isPatch ());
147148 } else if (updateControl .isUpdateResource ()) {
148149 updatedCustomResource =
149- updateCustomResource (updateControl .getResource (), updateControl . isPatch () );
150+ updateCustomResource (updateControl .getResource ());
150151 if (shouldUpdateObservedGenerationAutomatically (updatedCustomResource )) {
151152 updatedCustomResource =
152153 updateStatusGenerationAware (originalResource , updateControl .isPatch ());
@@ -181,13 +182,16 @@ public boolean isLastAttempt() {
181182
182183 R updatedResource = null ;
183184 if (errorStatusUpdateControl .getResource ().isPresent ()) {
184- updatedResource =
185- customResourceFacade
185+ updatedResource = errorStatusUpdateControl .isPatch () ? customResourceFacade
186+ .patchStatus (errorStatusUpdateControl .getResource ().orElseThrow ())
187+ : customResourceFacade
186188 .updateStatus (errorStatusUpdateControl .getResource ().orElseThrow ());
187189 }
188190 if (errorStatusUpdateControl .isNoRetry ()) {
189191 if (updatedResource != null ) {
190- return PostExecutionControl .customResourceUpdated (updatedResource );
192+ return errorStatusUpdateControl .isPatch ()
193+ ? PostExecutionControl .customResourceStatusPatched (updatedResource )
194+ : PostExecutionControl .customResourceUpdated (updatedResource );
191195 } else {
192196 return PostExecutionControl .defaultDispatch ();
193197 }
@@ -212,6 +216,7 @@ private R updateStatusGenerationAware(R resource, boolean patch) {
212216 }
213217 }
214218
219+ @ SuppressWarnings ("rawtypes" )
215220 private boolean shouldUpdateObservedGenerationAutomatically (R resource ) {
216221 if (configuration ().isGenerationAware () && resource instanceof CustomResource <?, ?>) {
217222 var customResource = (CustomResource ) resource ;
@@ -226,6 +231,7 @@ private boolean shouldUpdateObservedGenerationAutomatically(R resource) {
226231 return false ;
227232 }
228233
234+ @ SuppressWarnings ("rawtypes" )
229235 private void updateStatusObservedGenerationIfRequired (R resource ) {
230236 if (configuration ().isGenerationAware () && resource instanceof CustomResource <?, ?>) {
231237 var customResource = (CustomResource ) resource ;
@@ -242,7 +248,12 @@ private PostExecutionControl<R> createPostExecutionControl(R updatedCustomResour
242248 UpdateControl <R > updateControl ) {
243249 PostExecutionControl <R > postExecutionControl ;
244250 if (updatedCustomResource != null ) {
245- postExecutionControl = PostExecutionControl .customResourceUpdated (updatedCustomResource );
251+ if (updateControl .isUpdateStatus () && updateControl .isPatch ()) {
252+ postExecutionControl =
253+ PostExecutionControl .customResourceStatusPatched (updatedCustomResource );
254+ } else {
255+ postExecutionControl = PostExecutionControl .customResourceUpdated (updatedCustomResource );
256+ }
246257 } else {
247258 postExecutionControl = PostExecutionControl .defaultDispatch ();
248259 }
@@ -294,14 +305,10 @@ private void updateCustomResourceWithFinalizer(R resource) {
294305 customResourceFacade .replaceResourceWithLock (resource );
295306 }
296307
297- private R updateCustomResource (R resource , boolean patch ) {
308+ private R updateCustomResource (R resource ) {
298309 log .debug ("Updating resource: {} with version: {}" , getUID (resource ), getVersion (resource ));
299310 log .trace ("Resource before update: {}" , resource );
300- if (patch ) {
301- return customResourceFacade .patchResource (resource );
302- } else {
303- return customResourceFacade .replaceResourceWithLock (resource );
304- }
311+ return customResourceFacade .replaceResourceWithLock (resource );
305312 }
306313
307314 private R removeFinalizer (R resource ) {
@@ -340,27 +347,30 @@ public R replaceResourceWithLock(R resource) {
340347 .replace (resource );
341348 }
342349
343- public R patchResource (R resource ) {
344- return resourceOperation
345- .inNamespace (resource .getMetadata ().getNamespace ())
346- .withName (getName (resource ))
347- .patch (resource );
348- }
349-
350+ @ SuppressWarnings ({"rawtypes" , "unchecked" })
350351 public R updateStatus (R resource ) {
351352 log .trace ("Updating status for resource: {}" , resource );
352- return resourceOperation
353+ HasMetadataOperationsImpl hasMetadataOperation = ( HasMetadataOperationsImpl ) resourceOperation
353354 .inNamespace (resource .getMetadata ().getNamespace ())
354355 .withName (getName (resource ))
355- .replaceStatus (resource );
356+ .lockResourceVersion (resource .getMetadata ().getResourceVersion ());
357+ return (R ) hasMetadataOperation .replaceStatus (resource );
356358 }
357359
358360 public R patchStatus (R resource ) {
359361 log .trace ("Updating status for resource: {}" , resource );
360- return resourceOperation
361- .inNamespace (resource .getMetadata ().getNamespace ())
362- .withName (getName (resource ))
363- .patchStatus (resource );
362+ String resourceVersion = resource .getMetadata ().getResourceVersion ();
363+ try {
364+ // don't do optimistic locking on patch
365+ resource .getMetadata ().setResourceVersion (null );
366+ return resourceOperation
367+ .inNamespace (resource .getMetadata ().getNamespace ())
368+ .withName (getName (resource ))
369+ .patchStatus (resource );
370+ } finally {
371+ // restore initial resource version
372+ resource .getMetadata ().setResourceVersion (resourceVersion );
373+ }
364374 }
365375 }
366376}
0 commit comments