1
1
package io .javaoperatorsdk .operator .api .reconciler ;
2
2
3
- import java .util .function .BiFunction ;
3
+ import java .util .function .Supplier ;
4
4
import java .util .function .UnaryOperator ;
5
5
6
6
import org .slf4j .Logger ;
7
7
import org .slf4j .LoggerFactory ;
8
8
9
9
import io .fabric8 .kubernetes .api .model .HasMetadata ;
10
- import io .fabric8 .kubernetes .client .KubernetesClient ;
11
10
import io .fabric8 .kubernetes .client .dsl .base .PatchContext ;
12
11
import io .fabric8 .kubernetes .client .dsl .base .PatchType ;
13
12
import io .javaoperatorsdk .operator .api .reconciler .support .PrimaryResourceCache ;
14
13
import io .javaoperatorsdk .operator .processing .event .ResourceID ;
15
14
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
+ */
16
21
public class PrimaryUpdateAndCacheUtils {
17
22
18
23
private PrimaryUpdateAndCacheUtils () {}
@@ -29,7 +34,9 @@ private PrimaryUpdateAndCacheUtils() {}
29
34
* @param <P> primary resource type
30
35
*/
31
36
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 ());
33
40
}
34
41
35
42
/**
@@ -42,7 +49,9 @@ public static <P extends HasMetadata> P updateAndCacheStatus(P primary, Context<
42
49
* @param <P> primary resource type
43
50
*/
44
51
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 ());
46
55
}
47
56
48
57
/**
@@ -56,23 +65,23 @@ public static <P extends HasMetadata> P patchAndCacheStatus(P primary, Context<P
56
65
*/
57
66
public static <P extends HasMetadata > P editAndCacheStatus (
58
67
P primary , Context <P > context , UnaryOperator <P > operation ) {
68
+ logWarnIfResourceVersionPresent (primary );
59
69
return patchAndCacheStatus (
60
- primary , context , (p , c ) -> c .resource (primary ).editStatus (operation ));
70
+ primary , context , () -> context . getClient () .resource (primary ).editStatus (operation ));
61
71
}
62
72
63
73
/**
64
74
* Makes sure that the up-to-date primary resource will be present during the next reconciliation.
65
75
*
66
76
* @param primary resource
67
77
* @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
70
79
* @return the updated resource.
71
80
* @param <P> primary resource type
72
81
*/
73
82
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 ( );
76
85
context
77
86
.eventSourceRetriever ()
78
87
.getControllerEventSource ()
@@ -92,6 +101,7 @@ public static <P extends HasMetadata> P patchAndCacheStatus(
92
101
*/
93
102
public static <P extends HasMetadata > P ssaPatchAndCacheStatus (
94
103
P primary , P freshResourceWithStatus , Context <P > context ) {
104
+ logWarnIfResourceVersionPresent (freshResourceWithStatus );
95
105
var res =
96
106
context
97
107
.getClient ()
@@ -112,8 +122,7 @@ public static <P extends HasMetadata> P ssaPatchAndCacheStatus(
112
122
}
113
123
114
124
/**
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.
117
126
*
118
127
* @param primary resource
119
128
* @param freshResourceWithStatus - fresh resource with target state
@@ -127,10 +136,11 @@ public static <P extends HasMetadata> P ssaPatchAndCacheStatus(
127
136
logWarnIfResourceVersionPresent (freshResourceWithStatus );
128
137
return patchAndCacheStatus (
129
138
primary ,
130
- context .getClient (),
131
139
cache ,
132
- (P p , KubernetesClient c ) ->
133
- c .resource (freshResourceWithStatus )
140
+ () ->
141
+ context
142
+ .getClient ()
143
+ .resource (freshResourceWithStatus )
134
144
.subresource ("status" )
135
145
.patch (
136
146
new PatchContext .Builder ()
@@ -142,7 +152,6 @@ public static <P extends HasMetadata> P ssaPatchAndCacheStatus(
142
152
143
153
/**
144
154
* Patches the resource with JSON Patch and adds it to the {@link PrimaryResourceCache} provided.
145
- * Optimistic locking is not required.
146
155
*
147
156
* @param primary resource
148
157
* @param context of reconciliation
@@ -154,15 +163,12 @@ public static <P extends HasMetadata> P editAndCacheStatus(
154
163
P primary , Context <P > context , PrimaryResourceCache <P > cache , UnaryOperator <P > operation ) {
155
164
logWarnIfResourceVersionPresent (primary );
156
165
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 ));
161
167
}
162
168
163
169
/**
164
170
* Patches the resource with JSON Merge patch and adds it to the {@link PrimaryResourceCache}
165
- * provided. Optimistic locking is not required.
171
+ * provided.
166
172
*
167
173
* @param primary resource
168
174
* @param context of reconciliation
@@ -174,15 +180,11 @@ public static <P extends HasMetadata> P patchAndCacheStatus(
174
180
P primary , Context <P > context , PrimaryResourceCache <P > cache ) {
175
181
logWarnIfResourceVersionPresent (primary );
176
182
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 ());
181
184
}
182
185
183
186
/**
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.
186
188
*
187
189
* @param primary resource
188
190
* @param context of reconciliation
@@ -194,29 +196,30 @@ public static <P extends HasMetadata> P updateAndCacheStatus(
194
196
P primary , Context <P > context , PrimaryResourceCache <P > cache ) {
195
197
logWarnIfResourceVersionPresent (primary );
196
198
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 ());
201
200
}
202
201
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
+ */
203
211
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 ();
209
214
cache .cacheResource (primary , updatedResource );
210
215
return updatedResource ;
211
216
}
212
217
213
218
private static <P extends HasMetadata > void logWarnIfResourceVersionPresent (P primary ) {
214
219
if (primary .getMetadata ().getResourceVersion () != null ) {
215
220
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. " );
220
223
}
221
224
}
222
225
}
0 commit comments