Skip to content

Commit 976cf0a

Browse files
committed
docs: add javadoc and elaborate on discrimination of secondary resources
Signed-off-by: Chris Laprun <claprun@redhat.com>
1 parent 678f4da commit 976cf0a

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

docs/documentation/dependent-resources.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,14 @@ tests [here](https://github.com/java-operator-sdk/java-operator-sdk/blob/main/op
388388

389389
When dealing with multiple dependent resources of same type, the dependent resource implementation
390390
needs to know which specific resource should be targeted when reconciling a given dependent
391-
resource, since there will be multiple instances of that type which could possibly be used, each
392-
associated with the same primary resource. The target resource is computed and selected based on
393-
desired state automatically.
394-
395-
Formally there were resource discriminators used for this purpose, still present for backwards compatibility:
391+
resource, since there could be multiple instances of that type which could possibly be used, each
392+
associated with the same primary resource. In this situation, JOSDK automatically selects the appropriate secondary
393+
resource which matches the desired state associated with the primary resource. This makes sense because the desired
394+
state computation already needs to be able to discriminate among multiple related secondary resources to tell JOSDK how
395+
they should be reconciled.
396+
397+
However, it is also possible to be more explicit about how JOSDK should identify the proper secondary resource by using
398+
a
396399
[resource discriminator](https://github.com/java-operator-sdk/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/api/reconciler/ResourceDiscriminator.java)
397400
Resource discriminators uniquely identify the target resource of a dependent resource.
398401
In the managed Kubernetes dependent resources case, the discriminator can be declaratively set
@@ -405,7 +408,9 @@ public class MultipleManagedDependentResourceConfigMap1 {
405408
//...
406409
}
407410
```
408-
Resource discriminators still can be used. But might be removed in the future releases.
411+
412+
While using the automated mechanism should work well in most cases, resource discriminators might make sense for
413+
optimization purposes, especially when computing the desired state is costly.
409414

410415
### Sharing an Event Source Between Dependent Resources
411416

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@ default Optional<? extends ResourceEventSource<R, P>> eventSource(
4949
return Optional.empty();
5050
}
5151

52+
/**
53+
* Retrieves the secondary resource (if it exists) associated with the specified primary resource
54+
* for this DependentResource.
55+
*
56+
* @param primary the primary resource for which we want to retrieve the secondary resource
57+
* associated with this DependentResource
58+
* @param context the current {@link Context} in which the operation is called
59+
* @return the secondary resource or {@link Optional#empty()} if it doesn't exist
60+
* @throws IllegalStateException if more than one secondary is found to match the primary resource
61+
*/
5262
default Optional<R> getSecondaryResource(P primary, Context<P> context) {
5363
return Optional.empty();
5464
}

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,25 @@ public Optional<R> getSecondaryResource(P primary, Context<P> context) {
104104
}
105105
}
106106

107+
/**
108+
* Selects the actual secondary resource matching the desired state derived from the primary
109+
* resource when several resources of the same type are found in the context. This method allows
110+
* for optimized implementations in subclasses since this default implementation will check each
111+
* secondary candidates for equality with the specified desired state, which might end up costly.
112+
*
113+
* @param secondaryResources a Set of potential candidates of the looked for resource type
114+
* @param desired the desired state that the matching secondary resource must have to match the
115+
* primary resource
116+
* @return the matching secondary resource or {@link Optional#empty()} if none matches
117+
* @throws IllegalStateException if more than one candidate is found, in which case some other
118+
* mechanism might be necessary to distinguish between candidate secondary resources
119+
*/
107120
protected Optional<R> selectSecondaryBasedOnDesiredState(Set<R> secondaryResources, R desired) {
108121
var targetResources =
109122
secondaryResources.stream().filter(r -> r.equals(desired)).collect(Collectors.toList());
110123
if (targetResources.size() > 1) {
111-
throw new IllegalStateException("More than 1 secondary resource related to primary");
124+
throw new IllegalStateException(
125+
"More than one secondary resource related to primary: " + targetResources);
112126
}
113127
return targetResources.isEmpty() ? Optional.empty() : Optional.of(targetResources.get(0));
114128
}
@@ -178,8 +192,7 @@ protected void handleDelete(P primary, R secondary, Context<P> context) {
178192
"handleDelete method must be implemented if Deleter trait is supported");
179193
}
180194

181-
public void setResourceDiscriminator(
182-
ResourceDiscriminator<R, P> resourceDiscriminator) {
195+
public void setResourceDiscriminator(ResourceDiscriminator<R, P> resourceDiscriminator) {
183196
this.resourceDiscriminator = resourceDiscriminator;
184197
}
185198

0 commit comments

Comments
 (0)