2323import org .slf4j .LoggerFactory ;
2424
2525import io .fabric8 .kubernetes .api .model .HasMetadata ;
26- import io .fabric8 .kubernetes .api .model .ObjectMeta ;
2726import io .fabric8 .kubernetes .client .KubernetesClientException ;
2827import io .fabric8 .kubernetes .client .dsl .base .PatchContext ;
2928import io .fabric8 .kubernetes .client .dsl .base .PatchType ;
3433import static io .javaoperatorsdk .operator .processing .KubernetesResourceUtils .getUID ;
3534import static io .javaoperatorsdk .operator .processing .KubernetesResourceUtils .getVersion ;
3635
36+ /**
37+ * Provides useful operations to manipulate resources (server-side apply, patch, etc.) in an
38+ * idiomatic way, in particular to make sure that the latest version of the resource is present in
39+ * the caches for the next reconciliation.
40+ *
41+ * @param <P> the resource type on which this object operates
42+ */
3743public class ResourceOperations <P extends HasMetadata > {
3844
3945 public static final int DEFAULT_MAX_RETRY = 10 ;
@@ -118,9 +124,8 @@ public <R extends HasMetadata> R serverSideApplyStatus(R resource) {
118124 *
119125 * @param resource primary resource for server side apply
120126 * @return updated resource
121- * @param <P> primary resource type
122127 */
123- public < P extends HasMetadata > P serverSideApplyPrimary (P resource ) {
128+ public P serverSideApplyPrimary (P resource ) {
124129 return resourcePatch (
125130 resource ,
126131 r ->
@@ -149,9 +154,8 @@ public <P extends HasMetadata> P serverSideApplyPrimary(P resource) {
149154 *
150155 * @param resource primary resource for server side apply
151156 * @return updated resource
152- * @param <P> primary resource type
153157 */
154- public < P extends HasMetadata > P serverSideApplyPrimaryStatus (P resource ) {
158+ public P serverSideApplyPrimaryStatus (P resource ) {
155159 return resourcePatch (
156160 resource ,
157161 r ->
@@ -217,9 +221,8 @@ public <R extends HasMetadata> R updateStatus(R resource) {
217221 *
218222 * @param resource primary resource to update
219223 * @return updated resource
220- * @param <R> resource type
221224 */
222- public < R extends HasMetadata > R updatePrimary (R resource ) {
225+ public P updatePrimary (P resource ) {
223226 return resourcePatch (
224227 resource ,
225228 r -> context .getClient ().resource (r ).update (),
@@ -239,9 +242,8 @@ public <R extends HasMetadata> R updatePrimary(R resource) {
239242 *
240243 * @param resource primary resource to update
241244 * @return updated resource
242- * @param <R> resource type
243245 */
244- public < R extends HasMetadata > R updatePrimaryStatus (R resource ) {
246+ public P updatePrimaryStatus (P resource ) {
245247 return resourcePatch (
246248 resource ,
247249 r -> context .getClient ().resource (r ).updateStatus (),
@@ -304,9 +306,8 @@ public <R extends HasMetadata> R jsonPatchStatus(R resource, UnaryOperator<R> un
304306 * @param resource primary resource to patch
305307 * @param unaryOperator function to modify the resource
306308 * @return updated resource
307- * @param <R> resource type
308309 */
309- public < R extends HasMetadata > R jsonPatchPrimary (R resource , UnaryOperator <R > unaryOperator ) {
310+ public P jsonPatchPrimary (P resource , UnaryOperator <P > unaryOperator ) {
310311 return resourcePatch (
311312 resource ,
312313 r -> context .getClient ().resource (r ).edit (unaryOperator ),
@@ -327,10 +328,8 @@ public <R extends HasMetadata> R jsonPatchPrimary(R resource, UnaryOperator<R> u
327328 * @param resource primary resource to patch
328329 * @param unaryOperator function to modify the resource
329330 * @return updated resource
330- * @param <R> resource type
331331 */
332- public <R extends HasMetadata > R jsonPatchPrimaryStatus (
333- R resource , UnaryOperator <R > unaryOperator ) {
332+ public P jsonPatchPrimaryStatus (P resource , UnaryOperator <P > unaryOperator ) {
334333 return resourcePatch (
335334 resource ,
336335 r -> context .getClient ().resource (r ).editStatus (unaryOperator ),
@@ -390,9 +389,8 @@ public <R extends HasMetadata> R jsonMergePatchStatus(R resource) {
390389 *
391390 * @param resource primary resource to patch reconciliation
392391 * @return updated resource
393- * @param <R> resource type
394392 */
395- public < R extends HasMetadata > R jsonMergePatchPrimary (R resource ) {
393+ public P jsonMergePatchPrimary (P resource ) {
396394 return resourcePatch (
397395 resource ,
398396 r -> context .getClient ().resource (r ).patch (),
@@ -412,10 +410,9 @@ public <R extends HasMetadata> R jsonMergePatchPrimary(R resource) {
412410 *
413411 * @param resource primary resource to patch
414412 * @return updated resource
415- * @param <R> resource type
416413 * @see #jsonMergePatchPrimaryStatus(HasMetadata)
417414 */
418- public < R extends HasMetadata > R jsonMergePatchPrimaryStatus (R resource ) {
415+ public P jsonMergePatchPrimaryStatus (P resource ) {
419416 return resourcePatch (
420417 resource ,
421418 r -> context .getClient ().resource (r ).patchStatus (),
@@ -433,6 +430,7 @@ public <R extends HasMetadata> R jsonMergePatchPrimaryStatus(R resource) {
433430 * @param <R> resource type
434431 * @throws IllegalStateException if no event source or multiple event sources are found
435432 */
433+ @ SuppressWarnings ({"rawtypes" , "unchecked" })
436434 public <R extends HasMetadata > R resourcePatch (R resource , UnaryOperator <R > updateOperation ) {
437435
438436 var esList = context .eventSourceRetriever ().getEventSourcesFor (resource .getClass ());
@@ -447,7 +445,7 @@ public <R extends HasMetadata> R resourcePatch(R resource, UnaryOperator<R> upda
447445 }
448446 var es = esList .get (0 );
449447 if (es instanceof ManagedInformerEventSource mes ) {
450- return resourcePatch (resource , updateOperation , mes );
448+ return resourcePatch (resource , updateOperation , ( ManagedInformerEventSource < R , P , ?>) mes );
451449 } else {
452450 throw new IllegalStateException (
453451 "Target event source must be a subclass off "
@@ -466,10 +464,9 @@ public <R extends HasMetadata> R resourcePatch(R resource, UnaryOperator<R> upda
466464 * @return updated resource
467465 * @param <R> resource type
468466 */
469- @ SuppressWarnings ("unchecked" )
470467 public <R extends HasMetadata > R resourcePatch (
471- R resource , UnaryOperator <R > updateOperation , ManagedInformerEventSource ies ) {
472- return ( R ) ies .eventFilteringUpdateAndCacheResource (resource , updateOperation );
468+ R resource , UnaryOperator <R > updateOperation , ManagedInformerEventSource < R , P , ?> ies ) {
469+ return ies .eventFilteringUpdateAndCacheResource (resource , updateOperation );
473470 }
474471
475472 /**
@@ -498,7 +495,7 @@ public P addFinalizer(String finalizerName) {
498495 if (resource .isMarkedForDeletion () || resource .hasFinalizer (finalizerName )) {
499496 return resource ;
500497 }
501- return conflictRetryingPatch (
498+ return conflictRetryingPatchPrimary (
502499 r -> {
503500 r .addFinalizer (finalizerName );
504501 return r ;
@@ -532,7 +529,7 @@ public P removeFinalizer(String finalizerName) {
532529 if (!resource .hasFinalizer (finalizerName )) {
533530 return resource ;
534531 }
535- return conflictRetryingPatch (
532+ return conflictRetryingPatchPrimary (
536533 r -> {
537534 r .removeFinalizer (finalizerName );
538535 return r ;
@@ -555,11 +552,10 @@ public P removeFinalizer(String finalizerName) {
555552 * @param preCondition condition to check if the patch operation still needs to be performed or
556553 * not.
557554 * @return updated resource from the server or unchanged if the precondition does not hold.
558- * @param <R> resource type
559555 */
560556 @ SuppressWarnings ("unchecked" )
561- public < R extends HasMetadata > R conflictRetryingPatch (
562- UnaryOperator <R > resourceChangesOperator , Predicate <R > preCondition ) {
557+ public P conflictRetryingPatchPrimary (
558+ UnaryOperator <P > resourceChangesOperator , Predicate <P > preCondition ) {
563559 var resource = context .getPrimaryResource ();
564560 var client = context .getClient ();
565561 if (log .isDebugEnabled ()) {
@@ -568,10 +564,10 @@ public <R extends HasMetadata> R conflictRetryingPatch(
568564 int retryIndex = 0 ;
569565 while (true ) {
570566 try {
571- if (!preCondition .test (( R ) resource )) {
572- return ( R ) resource ;
567+ if (!preCondition .test (resource )) {
568+ return resource ;
573569 }
574- return jsonPatchPrimary (( R ) resource , resourceChangesOperator );
570+ return jsonPatchPrimary (resource , resourceChangesOperator );
575571 } catch (KubernetesClientException e ) {
576572 log .trace ("Exception during patch for resource: {}" , resource );
577573 retryIndex ++;
@@ -642,11 +638,9 @@ public P addFinalizerWithSSA(String finalizerName) {
642638 getVersion (originalResource ));
643639 }
644640 try {
641+ @ SuppressWarnings ("unchecked" )
645642 P resource = (P ) originalResource .getClass ().getConstructor ().newInstance ();
646- ObjectMeta objectMeta = new ObjectMeta ();
647- objectMeta .setName (originalResource .getMetadata ().getName ());
648- objectMeta .setNamespace (originalResource .getMetadata ().getNamespace ());
649- resource .setMetadata (objectMeta );
643+ resource .initNameAndNamespaceFrom (originalResource );
650644 resource .addFinalizer (finalizerName );
651645
652646 return serverSideApplyPrimary (resource );
0 commit comments