Skip to content

Commit c5267b8

Browse files
committed
wip
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent db2ced0 commit c5267b8

7 files changed

Lines changed: 160 additions & 111 deletions

File tree

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

Lines changed: 111 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,17 @@ public P serverSideApplyPrimary(P resource, Options options) {
217217
* <p>You are free to control the optimistic locking by setting the resource version in resource
218218
* metadata. In case of SSA we advise not to do updates with optimistic locking.
219219
*
220-
* @param resource primary resource for server side apply
220+
* @param desired primary resource for server side apply
221221
* @return updated resource
222222
*/
223-
public P serverSideApplyPrimaryStatus(P resource) {
223+
public P serverSideApplyPrimaryStatus(P desired) {
224224
return serverSideApplyPrimaryStatus(
225-
resource, Options.matchAndFilterWithDefaultMatcher(UpdateType.SSA_STATUS));
225+
desired, Options.matchAndFilterWithDefaultMatcher(UpdateType.SSA_STATUS));
226226
}
227227

228-
public P serverSideApplyPrimaryStatus(P resource, Options options) {
228+
public P serverSideApplyPrimaryStatus(P desired, Options options) {
229229
return resourcePatch(
230-
resource,
230+
desired,
231231
r ->
232232
context
233233
.getClient()
@@ -256,13 +256,13 @@ public <R extends HasMetadata> R update(R resource) {
256256
* <p>You are free to control the optimistic locking by setting the resource version in resource
257257
* metadata.
258258
*
259-
* @param resource resource to update
259+
* @param desired resource to update
260260
* @return updated resource
261261
* @param <R> resource type
262262
*/
263263
@Experimental(API_MIGHT_CHANGE)
264-
public <R extends HasMetadata> R update(R resource, Options options) {
265-
return resourcePatch(resource, r -> context.getClient().resource(r).update(), options);
264+
public <R extends HasMetadata> R update(R desired, Options options) {
265+
return resourcePatch(desired, r -> context.getClient().resource(r).update(), options);
266266
}
267267

268268
public <R extends HasMetadata> R update(
@@ -310,7 +310,7 @@ public <R extends HasMetadata> R update(
310310
*/
311311
public <R extends HasMetadata> R create(R resource) {
312312
// it is safe to do event filtering for create since check if the resource already exists.
313-
return create(resource, Options.alwaysFilter());
313+
return create(resource, Options.forceFilterEvents());
314314
}
315315

316316
public <R extends HasMetadata> R create(R resource, Options options) {
@@ -380,16 +380,16 @@ public <R extends HasMetadata> R updateStatus(R resource, Options options) {
380380
* <p>You are free to control the optimistic locking by setting the resource version in resource
381381
* metadata.
382382
*
383-
* @param resource primary resource to update
383+
* @param desired primary resource to update
384384
* @return updated resource
385385
*/
386-
public P updatePrimary(P resource) {
387-
return updatePrimary(resource, Options.matchAndFilterWithDefaultMatcher(UpdateType.UPDATE));
386+
public P updatePrimary(P desired) {
387+
return updatePrimary(desired, Options.matchAndFilterWithDefaultMatcher(UpdateType.UPDATE));
388388
}
389389

390-
public P updatePrimary(P resource, Options options) {
390+
public P updatePrimary(P desired, Options options) {
391391
return resourcePatch(
392-
resource,
392+
desired,
393393
r -> context.getClient().resource(r).update(),
394394
context.eventSourceRetriever().getControllerEventSource(),
395395
options);
@@ -406,12 +406,12 @@ public P updatePrimary(P resource, Options options) {
406406
* <p>You are free to control the optimistic locking by setting the resource version in resource
407407
* metadata.
408408
*
409-
* @param resource primary resource to update
409+
* @param desired primary resource to update
410410
* @return updated resource
411411
*/
412-
public P updatePrimaryStatus(P resource) {
412+
public P updatePrimaryStatus(P desired) {
413413
return resourcePatch(
414-
resource,
414+
desired,
415415
r -> context.getClient().resource(r).updateStatus(),
416416
context.eventSourceRetriever().getControllerEventSource(),
417417
Options.matchAndFilterWithDefaultMatcher(UpdateType.UPDATE_STATUS));
@@ -432,29 +432,32 @@ public P updatePrimaryStatus(P resource) {
432432
* @return updated resource
433433
* @param <R> resource type
434434
*/
435-
public <R extends HasMetadata> R jsonPatch(R actual, R desired) {
435+
public <R extends HasMetadata> R jsonPatch(R actualResource, UnaryOperator<R> unaryOperator) {
436436
return jsonPatch(
437-
actual, desired, Options.matchAndFilterWithDefaultMatcher(UpdateType.JSON_PATCH));
437+
actualResource,
438+
unaryOperator,
439+
Options.matchAndFilterWithDefaultMatcher(UpdateType.JSON_PATCH));
438440
}
439441

440-
public <R extends HasMetadata> R jsonPatch(R actual, R desired, Options options) {
442+
public <R extends HasMetadata> R jsonPatch(
443+
R actualResource, UnaryOperator<R> unaryOperator, Options options) {
441444
return resourcePatch(
442-
desired,
443-
actual,
444-
r -> context.getClient().resource(actual).edit(res -> desired),
445-
options,
446-
null);
445+
actualResource,
446+
r -> context.getClient().resource(actualResource).edit(unaryOperator),
447+
options);
447448
}
448449

449450
public <R extends HasMetadata> R jsonPatch(
450-
R desired, R actual, InformerEventSource<R, P> informerEventSource, Options options) {
451+
R actualResource,
452+
UnaryOperator<R> unaryOperator,
453+
InformerEventSource<R, P> informerEventSource,
454+
Options options) {
455+
451456
return resourcePatch(
452-
desired,
453-
actual,
454-
r -> context.getClient().resource(actual).edit(res -> desired),
457+
actualResource,
458+
r -> context.getClient().resource(actualResource).edit(unaryOperator),
455459
informerEventSource,
456-
options,
457-
null);
460+
options);
458461
}
459462

460463
/**
@@ -469,29 +472,39 @@ public <R extends HasMetadata> R jsonPatch(
469472
* <p>You are free to control the optimistic locking by setting the resource version in resource
470473
* metadata.
471474
*
472-
* @param desired resource to patch
475+
* @param actualResource resource to patch
476+
* @param unaryOperator function to modify the resource status
473477
* @return updated resource
474478
* @param <R> resource type
475479
*/
476-
public <R extends HasMetadata> R jsonPatchStatus(R desired, R actual) {
480+
public <R extends HasMetadata> R jsonPatchStatus(
481+
R actualResource, UnaryOperator<R> unaryOperator) {
477482
return jsonPatchStatus(
478-
desired, actual, Options.matchAndFilterWithDefaultMatcher(UpdateType.JSON_PATCH_STATUS));
483+
actualResource,
484+
unaryOperator,
485+
Options.matchAndFilterWithDefaultMatcher(UpdateType.JSON_PATCH_STATUS));
479486
}
480487

481-
public <R extends HasMetadata> R jsonPatchStatus(R desired, R actual, Options options) {
488+
public <R extends HasMetadata> R jsonPatchStatus(
489+
R actualResource, UnaryOperator<R> unaryOperator, Options options) {
490+
R desired = desiredForJsonPatch(actualResource, unaryOperator, options);
482491
return resourcePatch(
483492
desired,
484-
actual,
485-
r -> context.getClient().resource(actual).editStatus(res -> desired),
486-
options,
487-
null);
493+
actualResource,
494+
r -> context.getClient().resource(r).editStatus(unaryOperator),
495+
options);
488496
}
489497

490498
public <R extends HasMetadata> R jsonPatchStatus(
491-
R desired, R actual, InformerEventSource<R, P> informerEventSource, Options options) {
499+
R actualResource,
500+
UnaryOperator<R> unaryOperator,
501+
InformerEventSource<R, P> informerEventSource,
502+
Options options) {
503+
R desired = desiredForJsonPatch(actualResource, unaryOperator, options);
492504
return resourcePatch(
493505
desired,
494-
r -> context.getClient().resource(actual).editStatus(res -> desired),
506+
actualResource,
507+
r -> context.getClient().resource(r).editStatus(unaryOperator),
495508
informerEventSource,
496509
options);
497510
}
@@ -507,18 +520,23 @@ public <R extends HasMetadata> R jsonPatchStatus(
507520
* <p>You are free to control the optimistic locking by setting the resource version in resource
508521
* metadata.
509522
*
510-
* @param resource primary resource to patch
523+
* @param actualResource primary resource to patch
511524
* @param unaryOperator function to modify the resource
512525
* @return updated resource
513526
*/
514-
public P jsonPatchPrimary(P resource, UnaryOperator<P> unaryOperator) {
515-
return jsonPatchPrimary(resource, unaryOperator, Options.filterIfOptimisticLocking());
527+
public P jsonPatchPrimary(P actualResource, UnaryOperator<P> unaryOperator) {
528+
return jsonPatchPrimary(
529+
actualResource,
530+
unaryOperator,
531+
Options.matchAndFilterWithDefaultMatcher(UpdateType.JSON_PATCH));
516532
}
517533

518-
public P jsonPatchPrimary(P resource, UnaryOperator<P> unaryOperator, Options options) {
534+
public P jsonPatchPrimary(P actualResource, UnaryOperator<P> unaryOperator, Options options) {
535+
P desired = desiredForJsonPatch(actualResource, unaryOperator, options);
519536
return resourcePatch(
520-
resource,
521-
r -> context.getClient().resource(r).edit(unaryOperator),
537+
desired,
538+
actualResource,
539+
r -> context.getClient().resource(actualResource).edit(unaryOperator),
522540
context.eventSourceRetriever().getControllerEventSource(),
523541
options);
524542
}
@@ -534,18 +552,24 @@ public P jsonPatchPrimary(P resource, UnaryOperator<P> unaryOperator, Options op
534552
* <p>You are free to control the optimistic locking by setting the resource version in resource
535553
* metadata.
536554
*
537-
* @param resource primary resource to patch
555+
* @param actualResource primary resource to patch
538556
* @param unaryOperator function to modify the resource
539557
* @return updated resource
540558
*/
541-
public P jsonPatchPrimaryStatus(P resource, UnaryOperator<P> unaryOperator) {
542-
return jsonPatchPrimaryStatus(resource, unaryOperator, Options.filterIfOptimisticLocking());
559+
public P jsonPatchPrimaryStatus(P actualResource, UnaryOperator<P> unaryOperator) {
560+
return jsonPatchPrimaryStatus(
561+
actualResource,
562+
unaryOperator,
563+
Options.matchAndFilterWithDefaultMatcher(UpdateType.JSON_PATCH_STATUS));
543564
}
544565

545-
public P jsonPatchPrimaryStatus(P resource, UnaryOperator<P> unaryOperator, Options options) {
566+
public P jsonPatchPrimaryStatus(
567+
P actualResource, UnaryOperator<P> unaryOperator, Options options) {
568+
P desired = desiredForJsonPatch(actualResource, unaryOperator, options);
546569
return resourcePatch(
547-
resource,
548-
r -> context.getClient().resource(r).editStatus(unaryOperator),
570+
desired,
571+
actualResource,
572+
r -> context.getClient().resource(actualResource).editStatus(unaryOperator),
549573
context.eventSourceRetriever().getControllerEventSource(),
550574
options);
551575
}
@@ -682,40 +706,30 @@ public <R extends HasMetadata> R resourcePatch(R resource, UnaryOperator<R> upda
682706
@SuppressWarnings({"rawtypes", "unchecked"})
683707
private <R extends HasMetadata> R resourcePatch(
684708
R desired, UnaryOperator<R> updateOperation, Options options) {
685-
return resourcePatch(desired, updateOperation, options, null);
709+
return resourcePatch(desired, null, updateOperation, options);
686710
}
687711

688712
@Experimental(API_MIGHT_CHANGE)
689713
@SuppressWarnings({"rawtypes", "unchecked"})
690714
private <R extends HasMetadata> R resourcePatch(
691-
R desired, UnaryOperator<R> updateOperation, Options options, Matcher matcher) {
692-
return resourcePatch(desired, null, updateOperation, options, matcher);
693-
}
715+
R desired, R actual, UnaryOperator<R> updateOperation, Options options) {
694716

695-
@Experimental(API_MIGHT_CHANGE)
696-
@SuppressWarnings({"rawtypes", "unchecked"})
697-
private <R extends HasMetadata> R resourcePatch(
698-
R desired, R actual, UnaryOperator<R> updateOperation, Options options, Matcher matcher) {
717+
Class<?> targetClass = desired == null ? actual.getClass() : desired.getClass();
699718

700-
var esList = context.eventSourceRetriever().getEventSourcesFor(desired.getClass());
719+
var esList = context.eventSourceRetriever().getEventSourcesFor(targetClass);
701720
if (esList.isEmpty()) {
702-
throw new IllegalStateException("No event source found for type: " + desired.getClass());
721+
throw new IllegalStateException("No event source found for type: " + targetClass);
703722
}
704723
var es = esList.get(0);
705724
if (esList.size() > 1) {
706725
log.warn(
707726
"Multiple event sources found for type: {}, selecting first with name {}",
708-
desired.getClass(),
727+
targetClass,
709728
es.name());
710729
}
711730
if (es instanceof ManagedInformerEventSource mes) {
712731
return resourcePatch(
713-
desired,
714-
actual,
715-
updateOperation,
716-
(ManagedInformerEventSource<R, P, ?>) mes,
717-
options,
718-
matcher);
732+
desired, actual, updateOperation, (ManagedInformerEventSource<R, P, ?>) mes, options);
719733
} else {
720734
throw new IllegalStateException(
721735
"Target event source must be a subclass off "
@@ -737,18 +751,17 @@ public <R extends HasMetadata> R resourcePatch(
737751
if (options.getMatcher().isPresent()) {
738752
originalResource = ies.get(ResourceID.fromResource(desired)).orElse(null);
739753
}
740-
return resourcePatch(desired, originalResource, updateOperation, ies, options, null);
754+
return resourcePatch(desired, originalResource, updateOperation, ies, options);
741755
}
742756

743757
public <R extends HasMetadata> R resourcePatch(
744758
R desiredResource,
745759
R actualResource,
746760
UnaryOperator<R> updateOperation,
747761
ManagedInformerEventSource<R, P, ?> ies,
748-
Options options,
749-
Matcher matcherToUse) {
762+
Options options) {
750763

751-
var matcher = matcherToUse == null ? options.getMatcher().orElse(null) : matcherToUse;
764+
var matcher = options.getMatcher().orElse(null);
752765
boolean matches = false;
753766
if (matcher != null) {
754767
if (actualResource == null) {
@@ -764,7 +777,7 @@ public <R extends HasMetadata> R resourcePatch(
764777
}
765778
boolean optimisticLocking = desiredResource.getMetadata().getResourceVersion() != null;
766779

767-
if (options.getMode() == Mode.ONLY_CACHE
780+
if (options.getMode() == Mode.CACHE_ONLY
768781
|| (options.getMode() == Mode.FILTER_IF_OPTIMISTIC_LOCKING && !optimisticLocking)) {
769782
return ies.updateAndCacheResource(desiredResource, updateOperation);
770783
} else {
@@ -946,7 +959,7 @@ public P addFinalizerWithSSA(String finalizerName) {
946959
resource.initNameAndNamespaceFrom(originalResource);
947960
resource.addFinalizer(finalizerName);
948961

949-
return serverSideApplyPrimary(resource, Options.onlyCache());
962+
return serverSideApplyPrimary(resource, Options.cacheOnly());
950963
} catch (InstantiationException
951964
| IllegalAccessException
952965
| InvocationTargetException
@@ -962,8 +975,8 @@ public P addFinalizerWithSSA(String finalizerName) {
962975
@Experimental(API_MIGHT_CHANGE)
963976
public static class Options {
964977

965-
private static final Options ALWAYS_FILTER = new Options(Mode.ALWAYS_FILTER, null);
966-
private static final Options ONLY_CACHE = new Options(Mode.ONLY_CACHE, null);
978+
private static final Options ALWAYS_FILTER = new Options(Mode.FORCE_FILTER, null);
979+
private static final Options ONLY_CACHE = new Options(Mode.CACHE_ONLY, null);
967980
private static final Options FILTER_IF_OPTIMISTIC_LOCKING =
968981
new Options(Mode.FILTER_IF_OPTIMISTIC_LOCKING, null);
969982

@@ -975,16 +988,16 @@ private Options(Mode mode, Matcher matcher) {
975988
this.matcher = matcher;
976989
}
977990

978-
public static Options alwaysFilter() {
991+
public static Options forceFilterEvents() {
979992
return ALWAYS_FILTER;
980993
}
981994

982-
public static Options onlyCache() {
995+
public static Options cacheOnly() {
983996
return ONLY_CACHE;
984997
}
985998

986-
public static Options onlyCache(Matcher matcher) {
987-
return new Options(Mode.ONLY_CACHE, matcher);
999+
public static Options cacheOnly(Matcher matcher) {
1000+
return new Options(Mode.CACHE_ONLY, matcher);
9881001
}
9891002

9901003
public static Options filterIfOptimisticLocking() {
@@ -1016,10 +1029,6 @@ public Optional<Matcher> getMatcher() {
10161029
return Optional.ofNullable(matcher);
10171030
}
10181031

1019-
public boolean isAlwaysFilter() {
1020-
return mode == Mode.ALWAYS_FILTER;
1021-
}
1022-
10231032
public boolean requiresMatcher() {
10241033
return mode == Mode.FILTER_IF_NOT_MATCHING;
10251034
}
@@ -1029,7 +1038,21 @@ public boolean requiresMatcher() {
10291038
public enum Mode {
10301039
FILTER_IF_OPTIMISTIC_LOCKING,
10311040
FILTER_IF_NOT_MATCHING,
1032-
ONLY_CACHE,
1033-
ALWAYS_FILTER,
1041+
CACHE_ONLY,
1042+
FORCE_FILTER,
1043+
}
1044+
1045+
private <T extends HasMetadata> T desiredForJsonPatch(
1046+
T actualResource, UnaryOperator<T> unaryOperator, Options options) {
1047+
if (options.getMatcher().isPresent()) {
1048+
var cloned =
1049+
context
1050+
.getControllerConfiguration()
1051+
.getConfigurationService()
1052+
.getResourceCloner()
1053+
.clone(actualResource);
1054+
return unaryOperator.apply(cloned);
1055+
}
1056+
return null;
10341057
}
10351058
}

0 commit comments

Comments
 (0)