Skip to content

Commit 54f23d0

Browse files
committed
improve: simplify providing id for external dependent resources in order to avoid calling desired state
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
1 parent 6229ff6 commit 54f23d0

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,34 @@ protected InformerEventSource getExternalStateEventSource() {
126126
return externalStateEventSource;
127127
}
128128

129+
/**
130+
* Override this method to optimize and not compute the desired when selecting the target
131+
* secondary resource. Return the target id.
132+
*
133+
* @see ExternalDependentIDProvider
134+
* @param primary resource
135+
* @param context of current reconciliation
136+
* @return id of the target managed resource
137+
*/
138+
protected Optional<Object> targetSecondaryResourceID(P primary, Context<P> context) {
139+
R desired = desired(primary, context);
140+
if (desired instanceof ExternalDependentIDProvider<?> desiredWithId) {
141+
return Optional.of(desiredWithId.externalResourceId());
142+
} else {
143+
return Optional.empty();
144+
}
145+
}
146+
129147
@Override
130148
protected Optional<R> selectTargetSecondaryResource(
131149
Set<R> secondaryResources, P primary, Context<P> context) {
132-
R desired = desired(primary, context);
150+
var optionalId = targetSecondaryResourceID(primary, context);
133151
List<R> targetResources;
134-
if (desired instanceof ExternalDependentIDProvider<?> desiredWithId) {
152+
if (optionalId.isPresent()) {
153+
var id = optionalId.get();
135154
targetResources =
136155
secondaryResources.stream()
137-
.filter(
138-
r ->
139-
((ExternalDependentIDProvider<?>) r)
140-
.externalResourceId()
141-
.equals(desiredWithId.externalResourceId()))
156+
.filter(r -> ((ExternalDependentIDProvider<?>) r).externalResourceId().equals(id))
142157
.toList();
143158
} else {
144159
throw new IllegalStateException(

0 commit comments

Comments
 (0)