Skip to content

Commit 5da6a2a

Browse files
committed
wip
1 parent cf2a96d commit 5da6a2a

File tree

4 files changed

+174
-21
lines changed

4 files changed

+174
-21
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/ReconcileUtils.java

Lines changed: 164 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,18 @@ public class ReconcileUtils {
2626

2727
private ReconcileUtils() {}
2828

29-
// todo javadoc
3029
// todo move finalizers mtehods & deprecate
3130
// todo namespace handling
3231
// todo compare resource version if multiple event sources provide the same resource
33-
// for json patch make sense to retry for ?
32+
// todo for json patch make sense to retry for ?
3433

3534
public static <R extends HasMetadata> R serverSideApply(
3635
Context<? extends HasMetadata> context, R resource) {
36+
return serverSideApply(context, resource, true);
37+
}
38+
39+
public static <R extends HasMetadata> R serverSideApply(
40+
Context<? extends HasMetadata> context, R resource, boolean filterEvent) {
3741
return resourcePatch(
3842
context,
3943
resource,
@@ -46,11 +50,17 @@ public static <R extends HasMetadata> R serverSideApply(
4650
.withForce(true)
4751
.withFieldManager(context.getControllerConfiguration().fieldManager())
4852
.withPatchType(PatchType.SERVER_SIDE_APPLY)
49-
.build()));
53+
.build()),
54+
filterEvent);
5055
}
5156

5257
public static <R extends HasMetadata> R serverSideApplyStatus(
5358
Context<? extends HasMetadata> context, R resource) {
59+
return serverSideApplyStatus(context, resource, true);
60+
}
61+
62+
public static <R extends HasMetadata> R serverSideApplyStatus(
63+
Context<? extends HasMetadata> context, R resource, boolean filterEvent) {
5464
return resourcePatch(
5565
context,
5666
resource,
@@ -64,10 +74,16 @@ public static <R extends HasMetadata> R serverSideApplyStatus(
6474
.withForce(true)
6575
.withFieldManager(context.getControllerConfiguration().fieldManager())
6676
.withPatchType(PatchType.SERVER_SIDE_APPLY)
67-
.build()));
77+
.build()),
78+
filterEvent);
6879
}
6980

7081
public static <P extends HasMetadata> P serverSideApplyPrimary(Context<P> context, P resource) {
82+
return serverSideApplyPrimary(context, resource, true);
83+
}
84+
85+
public static <P extends HasMetadata> P serverSideApplyPrimary(
86+
Context<P> context, P resource, boolean filterEvent) {
7187
return resourcePatch(
7288
resource,
7389
r ->
@@ -80,11 +96,17 @@ public static <P extends HasMetadata> P serverSideApplyPrimary(Context<P> contex
8096
.withFieldManager(context.getControllerConfiguration().fieldManager())
8197
.withPatchType(PatchType.SERVER_SIDE_APPLY)
8298
.build()),
83-
context.eventSourceRetriever().getControllerEventSource());
99+
context.eventSourceRetriever().getControllerEventSource(),
100+
filterEvent);
84101
}
85102

86103
public static <P extends HasMetadata> P serverSideApplyPrimaryStatus(
87104
Context<P> context, P resource) {
105+
return serverSideApplyPrimaryStatus(context, resource, true);
106+
}
107+
108+
public static <P extends HasMetadata> P serverSideApplyPrimaryStatus(
109+
Context<P> context, P resource, boolean filterEvent) {
88110
return resourcePatch(
89111
resource,
90112
r ->
@@ -98,59 +120,178 @@ public static <P extends HasMetadata> P serverSideApplyPrimaryStatus(
98120
.withFieldManager(context.getControllerConfiguration().fieldManager())
99121
.withPatchType(PatchType.SERVER_SIDE_APPLY)
100122
.build()),
101-
context.eventSourceRetriever().getControllerEventSource());
123+
context.eventSourceRetriever().getControllerEventSource(),
124+
filterEvent);
102125
}
103126

104127
public static <R extends HasMetadata> R update(
105128
Context<? extends HasMetadata> context, R resource) {
106-
return resourcePatch(context, resource, r -> context.getClient().resource(r).update());
129+
return update(context, resource, true);
130+
}
131+
132+
public static <R extends HasMetadata> R update(
133+
Context<? extends HasMetadata> context, R resource, boolean filterEvent) {
134+
return resourcePatch(
135+
context, resource, r -> context.getClient().resource(r).update(), filterEvent);
107136
}
108137

109138
public static <R extends HasMetadata> R updateStatus(
110139
Context<? extends HasMetadata> context, R resource) {
111-
return resourcePatch(context, resource, r -> context.getClient().resource(r).updateStatus());
140+
return updateStatus(context, resource, true);
141+
}
142+
143+
public static <R extends HasMetadata> R updateStatus(
144+
Context<? extends HasMetadata> context, R resource, boolean filterEvent) {
145+
return resourcePatch(
146+
context, resource, r -> context.getClient().resource(r).updateStatus(), filterEvent);
147+
}
148+
149+
public static <R extends HasMetadata> R updatePrimary(
150+
Context<? extends HasMetadata> context, R resource) {
151+
return updatePrimary(context, resource, true);
152+
}
153+
154+
public static <R extends HasMetadata> R updatePrimary(
155+
Context<? extends HasMetadata> context, R resource, boolean filterEvent) {
156+
return resourcePatch(
157+
resource,
158+
r -> context.getClient().resource(r).update(),
159+
context.eventSourceRetriever().getControllerEventSource(),
160+
filterEvent);
161+
}
162+
163+
public static <R extends HasMetadata> R updatePrimaryStatus(
164+
Context<? extends HasMetadata> context, R resource) {
165+
return updatePrimaryStatus(context, resource, true);
166+
}
167+
168+
public static <R extends HasMetadata> R updatePrimaryStatus(
169+
Context<? extends HasMetadata> context, R resource, boolean filterEvent) {
170+
return resourcePatch(
171+
resource,
172+
r -> context.getClient().resource(r).updateStatus(),
173+
context.eventSourceRetriever().getControllerEventSource(),
174+
filterEvent);
112175
}
113176

114177
public static <R extends HasMetadata> R jsonPatch(
115178
Context<? extends HasMetadata> context, R resource, UnaryOperator<R> unaryOperator) {
179+
return jsonPatch(context, resource, unaryOperator, true);
180+
}
181+
182+
public static <R extends HasMetadata> R jsonPatch(
183+
Context<? extends HasMetadata> context,
184+
R resource,
185+
UnaryOperator<R> unaryOperator,
186+
boolean filterEvent) {
116187
return resourcePatch(
117-
context, resource, r -> context.getClient().resource(r).edit(unaryOperator));
188+
context, resource, r -> context.getClient().resource(r).edit(unaryOperator), filterEvent);
118189
}
119190

120191
public static <R extends HasMetadata> R jsonPatchStatus(
121192
Context<? extends HasMetadata> context, R resource, UnaryOperator<R> unaryOperator) {
193+
return jsonPatchStatus(context, resource, unaryOperator, true);
194+
}
195+
196+
public static <R extends HasMetadata> R jsonPatchStatus(
197+
Context<? extends HasMetadata> context,
198+
R resource,
199+
UnaryOperator<R> unaryOperator,
200+
boolean filterEvent) {
122201
return resourcePatch(
123-
context, resource, r -> context.getClient().resource(r).editStatus(unaryOperator));
202+
context,
203+
resource,
204+
r -> context.getClient().resource(r).editStatus(unaryOperator),
205+
filterEvent);
124206
}
125207

126208
public static <R extends HasMetadata> R jsonPatchPrimary(
127209
Context<? extends HasMetadata> context, R resource, UnaryOperator<R> unaryOperator) {
210+
return jsonPatchPrimary(context, resource, unaryOperator, true);
211+
}
212+
213+
public static <R extends HasMetadata> R jsonPatchPrimary(
214+
Context<? extends HasMetadata> context,
215+
R resource,
216+
UnaryOperator<R> unaryOperator,
217+
boolean filterEvent) {
128218
return resourcePatch(
129219
resource,
130220
r -> context.getClient().resource(r).edit(unaryOperator),
131-
context.eventSourceRetriever().getControllerEventSource());
221+
context.eventSourceRetriever().getControllerEventSource(),
222+
filterEvent);
132223
}
133224

134225
public static <R extends HasMetadata> R jsonPatchPrimaryStatus(
135226
Context<? extends HasMetadata> context, R resource, UnaryOperator<R> unaryOperator) {
227+
return jsonPatchPrimaryStatus(context, resource, unaryOperator, true);
228+
}
229+
230+
public static <R extends HasMetadata> R jsonPatchPrimaryStatus(
231+
Context<? extends HasMetadata> context,
232+
R resource,
233+
UnaryOperator<R> unaryOperator,
234+
boolean filterEvent) {
136235
return resourcePatch(
137236
resource,
138237
r -> context.getClient().resource(r).editStatus(unaryOperator),
139-
context.eventSourceRetriever().getControllerEventSource());
238+
context.eventSourceRetriever().getControllerEventSource(),
239+
filterEvent);
140240
}
141241

142242
public static <R extends HasMetadata> R jsonMergePatch(
143243
Context<? extends HasMetadata> context, R resource) {
144-
return resourcePatch(context, resource, r -> context.getClient().resource(r).patch());
244+
return jsonMergePatch(context, resource, true);
245+
}
246+
247+
public static <R extends HasMetadata> R jsonMergePatch(
248+
Context<? extends HasMetadata> context, R resource, boolean filterEvent) {
249+
return resourcePatch(
250+
context, resource, r -> context.getClient().resource(r).patch(), filterEvent);
251+
}
252+
253+
public static <R extends HasMetadata> R jsonMergePatchStatus(
254+
Context<? extends HasMetadata> context, R resource) {
255+
return jsonMergePatchStatus(context, resource, true);
145256
}
146257

147258
public static <R extends HasMetadata> R jsonMergePatchStatus(
259+
Context<? extends HasMetadata> context, R resource, boolean filterEvent) {
260+
return resourcePatch(
261+
context, resource, r -> context.getClient().resource(r).patchStatus(), filterEvent);
262+
}
263+
264+
public static <R extends HasMetadata> R jsonMergePatchPrimary(
265+
Context<? extends HasMetadata> context, R resource) {
266+
return jsonMergePatchPrimary(context, resource, true);
267+
}
268+
269+
public static <R extends HasMetadata> R jsonMergePatchPrimary(
270+
Context<? extends HasMetadata> context, R resource, boolean filterEvent) {
271+
return resourcePatch(
272+
resource,
273+
r -> context.getClient().resource(r).patch(),
274+
context.eventSourceRetriever().getControllerEventSource(),
275+
filterEvent);
276+
}
277+
278+
public static <R extends HasMetadata> R jsonMergePatchPrimaryStatus(
148279
Context<? extends HasMetadata> context, R resource) {
149-
return resourcePatch(context, resource, r -> context.getClient().resource(r).patchStatus());
280+
return jsonMergePatchPrimaryStatus(context, resource, true);
281+
}
282+
283+
public static <R extends HasMetadata> R jsonMergePatchPrimaryStatus(
284+
Context<? extends HasMetadata> context, R resource, boolean filterEvent) {
285+
return resourcePatch(
286+
resource,
287+
r -> context.getClient().resource(r).patch(),
288+
context.eventSourceRetriever().getControllerEventSource(),
289+
filterEvent);
150290
}
151291

152292
public static <R extends HasMetadata> R resourcePatch(
153-
Context<?> context, R resource, UnaryOperator<R> updateOperation) {
293+
Context<?> context, R resource, UnaryOperator<R> updateOperation, boolean filterEvent) {
294+
154295
var esList = context.eventSourceRetriever().getEventSourcesFor(resource.getClass());
155296
if (esList.isEmpty()) {
156297
throw new IllegalStateException("No event source found for type: " + resource.getClass());
@@ -163,7 +304,7 @@ public static <R extends HasMetadata> R resourcePatch(
163304
}
164305
var es = esList.get(0);
165306
if (es instanceof ManagedInformerEventSource mes) {
166-
return resourcePatch(resource, updateOperation, mes);
307+
return resourcePatch(resource, updateOperation, mes, filterEvent);
167308
} else {
168309
throw new IllegalStateException(
169310
"Target event source must be a subclass off "
@@ -173,7 +314,12 @@ public static <R extends HasMetadata> R resourcePatch(
173314

174315
@SuppressWarnings("unchecked")
175316
public static <R extends HasMetadata> R resourcePatch(
176-
R resource, UnaryOperator<R> updateOperation, ManagedInformerEventSource ies) {
177-
return (R) ies.updateAndCacheResource(resource, updateOperation);
317+
R resource,
318+
UnaryOperator<R> updateOperation,
319+
ManagedInformerEventSource ies,
320+
boolean filterEvent) {
321+
return filterEvent
322+
? (R) ies.eventFilteringUpdateAndCacheResource(resource, updateOperation)
323+
: (R) ies.updateAndCacheResource(resource, updateOperation);
178324
}
179325
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependentResource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void configureWith(KubernetesDependentResourceConfig<R> config) {
7575
protected R handleCreate(R desired, P primary, Context<P> context) {
7676
return eventSource()
7777
.orElseThrow()
78-
.updateAndCacheResource(
78+
.eventFilteringUpdateAndCacheResource(
7979
desired,
8080
toCreate -> KubernetesDependentResource.super.handleCreate(toCreate, primary, context));
8181
}
@@ -84,7 +84,7 @@ protected R handleCreate(R desired, P primary, Context<P> context) {
8484
protected R handleUpdate(R actual, R desired, P primary, Context<P> context) {
8585
return eventSource()
8686
.orElseThrow()
87-
.updateAndCacheResource(
87+
.eventFilteringUpdateAndCacheResource(
8888
desired,
8989
toUpdate ->
9090
KubernetesDependentResource.super.handleUpdate(actual, toUpdate, primary, context));

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/ManagedInformerEventSource.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ public void changeNamespaces(Set<String> namespaces) {
9999
}
100100

101101
public R updateAndCacheResource(R resourceToUpdate, UnaryOperator<R> updateMethod) {
102+
ResourceID id = ResourceID.fromResource(resourceToUpdate);
103+
var updated = updateMethod.apply(resourceToUpdate);
104+
handleRecentResourceUpdate(id, updated, resourceToUpdate);
105+
return updated;
106+
}
107+
108+
public R eventFilteringUpdateAndCacheResource(R resourceToUpdate, UnaryOperator<R> updateMethod) {
102109
ResourceID id = ResourceID.fromResource(resourceToUpdate);
103110
if (log.isDebugEnabled()) {
104111
log.debug("Update and cache: {}", id);

operator-framework/src/test/java/io/javaoperatorsdk/operator/dependent/externalstate/ExternalStateReconciler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private void createExternalResource(
109109
// Making sure that the created resources are in the cache for the next reconciliation.
110110
// This is critical in this case, since on next reconciliation if it would not be in the cache
111111
// it would be created again.
112-
configMapEventSource.updateAndCacheResource(
112+
configMapEventSource.eventFilteringUpdateAndCacheResource(
113113
configMap, toCreate -> context.getClient().configMaps().resource(toCreate).create());
114114
externalResourceEventSource.handleRecentResourceCreate(primaryID, createdResource);
115115
}

0 commit comments

Comments
 (0)