Skip to content

improve: naming target resource selector method #2546

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public Optional<R> getSecondaryResource(P primary, Context<P> context) {
if (secondaryResources.isEmpty()) {
return Optional.empty();
} else {
return selectManagedSecondaryResource(secondaryResources, primary, context);
return selectTargetSecondaryResource(secondaryResources, primary, context);
}

}
Expand All @@ -132,7 +132,7 @@ public Optional<R> getSecondaryResource(P primary, Context<P> context) {
* @throws IllegalStateException if more than one candidate is found, in which case some other
* mechanism might be necessary to distinguish between candidate secondary resources
*/
protected Optional<R> selectManagedSecondaryResource(Set<R> secondaryResources, P primary,
protected Optional<R> selectTargetSecondaryResource(Set<R> secondaryResources, P primary,
Context<P> context) {
R desired = desired(primary, context);
var targetResources = secondaryResources.stream().filter(r -> r.equals(desired)).toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ protected void addSecondaryToPrimaryMapperAnnotations(R desired, P primary, Stri
}

@Override
protected Optional<R> selectManagedSecondaryResource(Set<R> secondaryResources, P primary,
protected Optional<R> selectTargetSecondaryResource(Set<R> secondaryResources, P primary,
Context<P> context) {
ResourceID managedResourceID = managedSecondaryResourceID(primary, context);
ResourceID managedResourceID = targetSecondaryResourceID(primary, context);
return secondaryResources.stream()
.filter(r -> r.getMetadata().getName().equals(managedResourceID.getName()) &&
Objects.equals(r.getMetadata().getNamespace(),
Expand All @@ -239,7 +239,7 @@ protected Optional<R> selectManagedSecondaryResource(Set<R> secondaryResources,
* @param context of current reconciliation
* @return id of the target managed resource
*/
protected ResourceID managedSecondaryResourceID(P primary, Context<P> context) {
protected ResourceID targetSecondaryResourceID(P primary, Context<P> context) {
return ResourceID.fromResource(desired(primary, context));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Set<ExternalResource> fetchResources(
}

@Override
protected Optional<ExternalResource> selectManagedSecondaryResource(
protected Optional<ExternalResource> selectTargetSecondaryResource(
Set<ExternalResource> secondaryResources,
ExternalStateCustomResource primary, Context<ExternalStateCustomResource> context) {
var id = getResourceID(primary);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.javaoperatorsdk.operator.api.reconciler.Context;
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDKubernetesDependentResource;
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent;
import io.javaoperatorsdk.operator.processing.event.ResourceID;

@KubernetesDependent
public class MultipleManagedDependentNoDiscriminatorConfigMap1
Expand All @@ -20,6 +21,19 @@ public MultipleManagedDependentNoDiscriminatorConfigMap1() {
super(ConfigMap.class);
}

/*
* Showcases optimization to avoid computing the whole desired state by providing the ResourceID
* of the target resource. In this particular case this would not be necessary, since desired
* state creation is pretty lightweight. However, this might make sense in situation where the
* desired state is more costly
*/
protected ResourceID targetSecondaryResourceID(
MultipleManagedDependentNoDiscriminatorCustomResource primary,
Context<MultipleManagedDependentNoDiscriminatorCustomResource> context) {
return new ResourceID(primary.getMetadata().getName() + NAME_SUFFIX,
primary.getMetadata().getNamespace());
}

@Override
protected ConfigMap desired(MultipleManagedDependentNoDiscriminatorCustomResource primary,
Context<MultipleManagedDependentNoDiscriminatorCustomResource> context) {
Expand Down
Loading