From cef1edd400d10640f8900e7854c573e94b16d9fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20M=C3=A9sz=C3=A1ros?= Date: Wed, 15 Oct 2025 20:28:26 +0200 Subject: [PATCH 1/3] improve: simplify providing id for external dependent resources in order to avoid calling desired state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Attila Mészáros --- .../AbstractExternalDependentResource.java | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/AbstractExternalDependentResource.java b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/AbstractExternalDependentResource.java index 2c5be82288..418632a506 100644 --- a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/AbstractExternalDependentResource.java +++ b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/AbstractExternalDependentResource.java @@ -126,19 +126,34 @@ protected InformerEventSource getExternalStateEventSource() { return externalStateEventSource; } + /** + * Override this method to optimize and not compute the desired when selecting the target + * secondary resource. Return the target id. + * + * @see ExternalDependentIDProvider + * @param primary resource + * @param context of current reconciliation + * @return id of the target managed resource + */ + protected Optional targetSecondaryResourceID(P primary, Context

context) { + R desired = desired(primary, context); + if (desired instanceof ExternalDependentIDProvider desiredWithId) { + return Optional.of(desiredWithId.externalResourceId()); + } else { + return Optional.empty(); + } + } + @Override protected Optional selectTargetSecondaryResource( Set secondaryResources, P primary, Context

context) { - R desired = desired(primary, context); + var optionalId = targetSecondaryResourceID(primary, context); List targetResources; - if (desired instanceof ExternalDependentIDProvider desiredWithId) { + if (optionalId.isPresent()) { + var id = optionalId.get(); targetResources = secondaryResources.stream() - .filter( - r -> - ((ExternalDependentIDProvider) r) - .externalResourceId() - .equals(desiredWithId.externalResourceId())) + .filter(r -> ((ExternalDependentIDProvider) r).externalResourceId().equals(id)) .toList(); } else { throw new IllegalStateException( From ffc9e8596d1dc1833f573787d4bf8adb5b905a58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Attila=20M=C3=A9sz=C3=A1ros?= Date: Wed, 15 Oct 2025 20:35:15 +0200 Subject: [PATCH 2/3] wip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Attila Mészáros --- .../dependent-resources.md | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/docs/content/en/docs/documentation/dependent-resource-and-workflows/dependent-resources.md b/docs/content/en/docs/documentation/dependent-resource-and-workflows/dependent-resources.md index 8a575f716f..7a5db4ed5f 100644 --- a/docs/content/en/docs/documentation/dependent-resource-and-workflows/dependent-resources.md +++ b/docs/content/en/docs/documentation/dependent-resource-and-workflows/dependent-resources.md @@ -303,14 +303,15 @@ they should be reconciled. There might be cases, though, where it might be problematic to call the `desired` method several times (for example, because it is costly to do so), it is always possible to override this automated discrimination using several means (consider in this priority order): -- Override the `targetSecondaryResourceID` method, if your `DependentResource` extends `KubernetesDependentResource`, +- Override the [`targetSecondaryResourceID`](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependentResource.java#L282) + method, if your `DependentResource` extends `KubernetesDependentResource`, where it's very often possible to easily determine the `ResourceID` of the secondary resource. This would probably be - the easiest solution if you're working with Kubernetes resources. -- Override the `selectTargetSecondaryResource` method, if your `DependentResource` extends `AbstractDependentResource`. - This should be relatively simple to override this method to optimize the matching to your needs. You can see an - example of such an implementation in - the [`ExternalWithStateDependentResource`](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework/src/test/java/io/javaoperatorsdk/operator/dependent/externalstate/ExternalWithStateDependentResource.java) - class. + the easiest solution if you're working with Kubernetes resources. Similarly, for you can override the + [`targetSecondaryResourceID`](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/AbstractExternalDependentResource.java#L138) + for external resources. See below the related ID handling +- If the approach from above doesn't fit your needs, you can override the target resource selection mechanism by overriding + `selectTargetSecondaryResource` for both [`KubernetesDependentResource`](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependentResource.java#L282) + and [`AbstractExternalDependentResource`](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/AbstractExternalDependentResource.java#L148). - As last resort, you can implement your own `getSecondaryResource` method on your `DependentResource` implementation from scratch. ### Sharing an Event Source Between Dependent Resources From 5420ddccbad0c196825046a2342f1187e4518911 Mon Sep 17 00:00:00 2001 From: Chris Laprun Date: Thu, 23 Oct 2025 10:52:40 +0200 Subject: [PATCH 3/3] fix: wording Signed-off-by: Chris Laprun --- .../dependent-resource-and-workflows/dependent-resources.md | 4 ++-- .../dependent/AbstractExternalDependentResource.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/content/en/docs/documentation/dependent-resource-and-workflows/dependent-resources.md b/docs/content/en/docs/documentation/dependent-resource-and-workflows/dependent-resources.md index 7a5db4ed5f..c1e3ffc8e8 100644 --- a/docs/content/en/docs/documentation/dependent-resource-and-workflows/dependent-resources.md +++ b/docs/content/en/docs/documentation/dependent-resource-and-workflows/dependent-resources.md @@ -306,10 +306,10 @@ it is always possible to override this automated discrimination using several me - Override the [`targetSecondaryResourceID`](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependentResource.java#L282) method, if your `DependentResource` extends `KubernetesDependentResource`, where it's very often possible to easily determine the `ResourceID` of the secondary resource. This would probably be - the easiest solution if you're working with Kubernetes resources. Similarly, for you can override the + the easiest solution if you're working with Kubernetes resources. Similarly, you can override the [`targetSecondaryResourceID`](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/AbstractExternalDependentResource.java#L138) for external resources. See below the related ID handling -- If the approach from above doesn't fit your needs, you can override the target resource selection mechanism by overriding +- If the approach above doesn't fit your needs, you can override the target resource selection mechanism by overriding `selectTargetSecondaryResource` for both [`KubernetesDependentResource`](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependentResource.java#L282) and [`AbstractExternalDependentResource`](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/AbstractExternalDependentResource.java#L148). - As last resort, you can implement your own `getSecondaryResource` method on your `DependentResource` implementation from scratch. diff --git a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/AbstractExternalDependentResource.java b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/AbstractExternalDependentResource.java index 418632a506..ff8a5133ae 100644 --- a/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/AbstractExternalDependentResource.java +++ b/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/AbstractExternalDependentResource.java @@ -127,7 +127,7 @@ protected InformerEventSource getExternalStateEventSource() { } /** - * Override this method to optimize and not compute the desired when selecting the target + * Override this method to optimize and avoid computing the desired when selecting the target * secondary resource. Return the target id. * * @see ExternalDependentIDProvider