11package io .javaoperatorsdk .operator .api .reconciler ;
22
3- import java .util .function .BiFunction ;
3+ import java .util .function .Supplier ;
44import java .util .function .UnaryOperator ;
55
66import org .slf4j .Logger ;
77import org .slf4j .LoggerFactory ;
88
99import io .fabric8 .kubernetes .api .model .HasMetadata ;
10- import io .fabric8 .kubernetes .client .KubernetesClient ;
1110import io .fabric8 .kubernetes .client .dsl .base .PatchContext ;
1211import io .fabric8 .kubernetes .client .dsl .base .PatchType ;
1312import io .javaoperatorsdk .operator .api .reconciler .support .PrimaryResourceCache ;
1413import io .javaoperatorsdk .operator .processing .event .ResourceID ;
1514
15+ /**
16+ * Utility methods to patch the primary resource state and store it to the related cache, to make
17+ * sure that fresh resource is present for the next reconciliation. The main use case for such
18+ * updates is to store state is resource status. Use of optimistic locking is not desired for such
19+ * updates, since we don't want to patch fail and lose information that we want to store.
20+ */
1621public class PrimaryUpdateAndCacheUtils {
1722
1823 private PrimaryUpdateAndCacheUtils () {}
@@ -29,7 +34,9 @@ private PrimaryUpdateAndCacheUtils() {}
2934 * @param <P> primary resource type
3035 */
3136 public static <P extends HasMetadata > P updateAndCacheStatus (P primary , Context <P > context ) {
32- return patchAndCacheStatus (primary , context , (p , c ) -> c .resource (primary ).updateStatus ());
37+ logWarnIfResourceVersionPresent (primary );
38+ return patchAndCacheStatus (
39+ primary , context , () -> context .getClient ().resource (primary ).updateStatus ());
3340 }
3441
3542 /**
@@ -42,7 +49,9 @@ public static <P extends HasMetadata> P updateAndCacheStatus(P primary, Context<
4249 * @param <P> primary resource type
4350 */
4451 public static <P extends HasMetadata > P patchAndCacheStatus (P primary , Context <P > context ) {
45- return patchAndCacheStatus (primary , context , (p , c ) -> c .resource (primary ).patchStatus ());
52+ logWarnIfResourceVersionPresent (primary );
53+ return patchAndCacheStatus (
54+ primary , context , () -> context .getClient ().resource (primary ).patchStatus ());
4655 }
4756
4857 /**
@@ -56,23 +65,23 @@ public static <P extends HasMetadata> P patchAndCacheStatus(P primary, Context<P
5665 */
5766 public static <P extends HasMetadata > P editAndCacheStatus (
5867 P primary , Context <P > context , UnaryOperator <P > operation ) {
68+ logWarnIfResourceVersionPresent (primary );
5969 return patchAndCacheStatus (
60- primary , context , (p , c ) -> c .resource (primary ).editStatus (operation ));
70+ primary , context , () -> context . getClient () .resource (primary ).editStatus (operation ));
6171 }
6272
6373 /**
6474 * Makes sure that the up-to-date primary resource will be present during the next reconciliation.
6575 *
6676 * @param primary resource
6777 * @param context of reconciliation
68- * @param patch free implementation of cache - make sure you use optimistic locking during the
69- * update
78+ * @param patch free implementation of cache
7079 * @return the updated resource.
7180 * @param <P> primary resource type
7281 */
7382 public static <P extends HasMetadata > P patchAndCacheStatus (
74- P primary , Context <P > context , BiFunction < P , KubernetesClient , P > patch ) {
75- var updatedResource = patch .apply ( primary , context . getClient () );
83+ P primary , Context <P > context , Supplier < P > patch ) {
84+ var updatedResource = patch .get ( );
7685 context
7786 .eventSourceRetriever ()
7887 .getControllerEventSource ()
@@ -92,6 +101,7 @@ public static <P extends HasMetadata> P patchAndCacheStatus(
92101 */
93102 public static <P extends HasMetadata > P ssaPatchAndCacheStatus (
94103 P primary , P freshResourceWithStatus , Context <P > context ) {
104+ logWarnIfResourceVersionPresent (freshResourceWithStatus );
95105 var res =
96106 context
97107 .getClient ()
@@ -112,8 +122,7 @@ public static <P extends HasMetadata> P ssaPatchAndCacheStatus(
112122 }
113123
114124 /**
115- * Patches the resource and adds it to the {@link PrimaryResourceCache} provided. Optimistic
116- * locking is not required.
125+ * Patches the resource and adds it to the {@link PrimaryResourceCache} provided.
117126 *
118127 * @param primary resource
119128 * @param freshResourceWithStatus - fresh resource with target state
@@ -127,10 +136,11 @@ public static <P extends HasMetadata> P ssaPatchAndCacheStatus(
127136 logWarnIfResourceVersionPresent (freshResourceWithStatus );
128137 return patchAndCacheStatus (
129138 primary ,
130- context .getClient (),
131139 cache ,
132- (P p , KubernetesClient c ) ->
133- c .resource (freshResourceWithStatus )
140+ () ->
141+ context
142+ .getClient ()
143+ .resource (freshResourceWithStatus )
134144 .subresource ("status" )
135145 .patch (
136146 new PatchContext .Builder ()
@@ -142,7 +152,6 @@ public static <P extends HasMetadata> P ssaPatchAndCacheStatus(
142152
143153 /**
144154 * Patches the resource with JSON Patch and adds it to the {@link PrimaryResourceCache} provided.
145- * Optimistic locking is not required.
146155 *
147156 * @param primary resource
148157 * @param context of reconciliation
@@ -154,15 +163,12 @@ public static <P extends HasMetadata> P editAndCacheStatus(
154163 P primary , Context <P > context , PrimaryResourceCache <P > cache , UnaryOperator <P > operation ) {
155164 logWarnIfResourceVersionPresent (primary );
156165 return patchAndCacheStatus (
157- primary ,
158- context .getClient (),
159- cache ,
160- (P p , KubernetesClient c ) -> c .resource (primary ).editStatus (operation ));
166+ primary , cache , () -> context .getClient ().resource (primary ).editStatus (operation ));
161167 }
162168
163169 /**
164170 * Patches the resource with JSON Merge patch and adds it to the {@link PrimaryResourceCache}
165- * provided. Optimistic locking is not required.
171+ * provided.
166172 *
167173 * @param primary resource
168174 * @param context of reconciliation
@@ -174,15 +180,11 @@ public static <P extends HasMetadata> P patchAndCacheStatus(
174180 P primary , Context <P > context , PrimaryResourceCache <P > cache ) {
175181 logWarnIfResourceVersionPresent (primary );
176182 return patchAndCacheStatus (
177- primary ,
178- context .getClient (),
179- cache ,
180- (P p , KubernetesClient c ) -> c .resource (primary ).patchStatus ());
183+ primary , cache , () -> context .getClient ().resource (primary ).patchStatus ());
181184 }
182185
183186 /**
184- * Updates the resource and adds it to the {@link PrimaryResourceCache} provided. Optimistic
185- * locking is not required.
187+ * Updates the resource and adds it to the {@link PrimaryResourceCache} provided.
186188 *
187189 * @param primary resource
188190 * @param context of reconciliation
@@ -194,29 +196,30 @@ public static <P extends HasMetadata> P updateAndCacheStatus(
194196 P primary , Context <P > context , PrimaryResourceCache <P > cache ) {
195197 logWarnIfResourceVersionPresent (primary );
196198 return patchAndCacheStatus (
197- primary ,
198- context .getClient (),
199- cache ,
200- (P p , KubernetesClient c ) -> c .resource (primary ).updateStatus ());
199+ primary , cache , () -> context .getClient ().resource (primary ).updateStatus ());
201200 }
202201
202+ /**
203+ * Updates the resource using the user provided implementation anc caches the result.
204+ *
205+ * @param primary resource
206+ * @param cache resource cache managed by user
207+ * @param patch implementation of resource update*
208+ * @return the updated resource.
209+ * @param <P> primary resource type
210+ */
203211 public static <P extends HasMetadata > P patchAndCacheStatus (
204- P primary ,
205- KubernetesClient client ,
206- PrimaryResourceCache <P > cache ,
207- BiFunction <P , KubernetesClient , P > patch ) {
208- var updatedResource = patch .apply (primary , client );
212+ P primary , PrimaryResourceCache <P > cache , Supplier <P > patch ) {
213+ var updatedResource = patch .get ();
209214 cache .cacheResource (primary , updatedResource );
210215 return updatedResource ;
211216 }
212217
213218 private static <P extends HasMetadata > void logWarnIfResourceVersionPresent (P primary ) {
214219 if (primary .getMetadata ().getResourceVersion () != null ) {
215220 log .warn (
216- "Primary resource version is NOT null, for caching with optimistic locking use"
217- + " alternative methods. Name: {} namespace: {}" ,
218- primary .getMetadata ().getName (),
219- primary .getMetadata ().getNamespace ());
221+ "The metadata.resourceVersion of primary resource is NOT null, "
222+ + "using optimistic locking is discouraged for this purpose. " );
220223 }
221224 }
222225}
0 commit comments