Skip to content

Commit 4e44a50

Browse files
committed
refactor: make ReconcileResult immutable
1 parent d5e814d commit 4e44a50

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88

99
public class ReconcileResult<R> {
1010

11-
private Map<R, Operation> resourceOperations = new HashMap<>(1);
12-
13-
public ReconcileResult() {}
11+
private final Map<R, Operation> resourceOperations;
1412

1513
public static <T> ReconcileResult<T> resourceCreated(T resource) {
1614
return new ReconcileResult<>(resource, Operation.CREATED);
@@ -24,6 +22,21 @@ public static <T> ReconcileResult<T> noOperation(T resource) {
2422
return new ReconcileResult<>(resource, Operation.NONE);
2523
}
2624

25+
@SafeVarargs
26+
public static <T> ReconcileResult<T> aggregatedResult(ReconcileResult<T>... results) {
27+
if (results == null) {
28+
throw new IllegalArgumentException("Should provide results to aggregate");
29+
}
30+
if (results.length == 1) {
31+
return results[0];
32+
}
33+
final Map<T, Operation> operations = new HashMap<>(results.length);
34+
for (ReconcileResult<T> res : results) {
35+
res.getSingleResource().ifPresent(r -> operations.put(r, res.getSingleOperation()));
36+
}
37+
return new ReconcileResult<>(operations);
38+
}
39+
2740
@Override
2841
public String toString() {
2942
return resourceOperations.entrySet().stream().collect(Collectors.toMap(
@@ -33,7 +46,11 @@ public String toString() {
3346
}
3447

3548
private ReconcileResult(R resource, Operation operation) {
36-
resourceOperations.put(resource, operation);
49+
resourceOperations = Map.of(resource, operation);
50+
}
51+
52+
private ReconcileResult(Map<R, Operation> operations) {
53+
resourceOperations = Collections.unmodifiableMap(operations);
3754
}
3855

3956
public Optional<R> getSingleResource() {
@@ -45,17 +62,11 @@ public Operation getSingleOperation() {
4562
.orElseThrow();
4663
}
4764

65+
@SuppressWarnings("unused")
4866
public Map<R, Operation> getResourceOperations() {
4967
return resourceOperations;
5068
}
5169

52-
public void addReconcileResult(ReconcileResult<R> result) {
53-
result.getSingleResource().ifPresent(r -> {
54-
resourceOperations.put(r, result.getSingleOperation());
55-
});
56-
57-
}
58-
5970
public enum Operation {
6071
CREATED, UPDATED, NONE
6172
}

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,19 @@ public AbstractDependentResource() {
4040

4141
@Override
4242
public ReconcileResult<R> reconcile(P primary, Context<P> context) {
43-
var count = bulk ? bulkDependentResource.count(primary, context) : 1;
4443
if (bulk) {
44+
final var count = bulkDependentResource.count(primary, context);
4545
deleteBulkResourcesIfRequired(count, lastKnownBulkSize(), primary, context);
4646
adjustDiscriminators(count);
47+
@SuppressWarnings("unchecked")
48+
final ReconcileResult<R>[] results = new ReconcileResult[count];
49+
for (int i = 0; i < count; i++) {
50+
results[i] = reconcileIndexAware(primary, i, context);
51+
}
52+
return ReconcileResult.aggregatedResult(results);
53+
} else {
54+
return reconcileIndexAware(primary, 0, context);
4755
}
48-
ReconcileResult<R> result = new ReconcileResult<>();
49-
for (int i = 0; i < count; i++) {
50-
var res = reconcileIndexAware(primary, i, context);
51-
result.addReconcileResult(res);
52-
}
53-
return result;
5456
}
5557

5658
protected void deleteBulkResourcesIfRequired(int targetCount, int actualCount, P primary,

0 commit comments

Comments
 (0)