Skip to content

Commit 8a8bc82

Browse files
committed
wip
Signed-off-by: Attila Mészáros <[email protected]>
1 parent fccc3e3 commit 8a8bc82

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,8 @@ public Optional<R> getSecondaryResource(P primary, Context<P> context) {
134134
* @throws IllegalStateException if more than one candidate is found, in which case some other
135135
* mechanism might be necessary to distinguish between candidate secondary resources
136136
*/
137-
protected Optional<R> selectTargetSecondaryResource(
138-
Set<R> secondaryResources, P primary, Context<P> context) {
139-
R desired = desired(primary, context);
140-
var targetResources = secondaryResources.stream().filter(r -> r.equals(desired)).toList();
141-
if (targetResources.size() > 1) {
142-
throw new IllegalStateException(
143-
"More than one secondary resource related to primary: " + targetResources);
144-
}
145-
return targetResources.isEmpty() ? Optional.empty() : Optional.of(targetResources.get(0));
146-
}
137+
protected abstract Optional<R> selectTargetSecondaryResource(
138+
Set<R> secondaryResources, P primary, Context<P> context);
147139

148140
private void throwIfNull(R desired, P primary, String descriptor) {
149141
if (desired == null) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.Collections;
55
import java.util.List;
66
import java.util.Map;
7+
import java.util.Optional;
78
import java.util.Set;
89

910
import io.fabric8.kubernetes.api.model.HasMetadata;
@@ -105,6 +106,13 @@ public Result<R> match(R resource, P primary, Context<P> context) {
105106
return bulkDependentResource.match(resource, desired, primary, context);
106107
}
107108

109+
@Override
110+
protected Optional<R> selectTargetSecondaryResource(
111+
Set<R> secondaryResources, P primary, Context<P> context) {
112+
throw new IllegalStateException(
113+
"BulkDependentResource should not call selectTargetSecondaryResource.");
114+
}
115+
108116
@Override
109117
protected void onCreated(P primary, R created, Context<P> context) {
110118
asAbstractDependentResource().onCreated(primary, created, context);

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/dependent/AbstractDependentResourceTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.javaoperatorsdk.operator.processing.dependent;
22

33
import java.util.Optional;
4+
import java.util.Set;
45

56
import org.junit.jupiter.api.Test;
67

@@ -84,6 +85,20 @@ public Optional<ConfigMap> getSecondaryResource(
8485
return Optional.ofNullable(secondary);
8586
}
8687

88+
@Override
89+
protected Optional<ConfigMap> selectTargetSecondaryResource(
90+
Set<ConfigMap> secondaryResources,
91+
TestCustomResource primary,
92+
Context<TestCustomResource> context) {
93+
if (secondaryResources.size() == 1) {
94+
return Optional.of(secondaryResources.iterator().next());
95+
} else if (secondaryResources.isEmpty()) {
96+
return Optional.empty();
97+
} else {
98+
throw new IllegalStateException();
99+
}
100+
}
101+
87102
@Override
88103
protected void onCreated(
89104
TestCustomResource primary, ConfigMap created, Context<TestCustomResource> context) {}

0 commit comments

Comments
 (0)