Skip to content

Commit 162eb42

Browse files
csvirimetacosm
authored andcommitted
improve: naming target resource selector method (#2546)
1 parent 87d3b95 commit 162eb42

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public Optional<R> getSecondaryResource(P primary, Context<P> context) {
114114
if (secondaryResources.isEmpty()) {
115115
return Optional.empty();
116116
} else {
117-
return selectManagedSecondaryResource(secondaryResources, primary, context);
117+
return selectTargetSecondaryResource(secondaryResources, primary, context);
118118
}
119119

120120
}
@@ -132,7 +132,7 @@ public Optional<R> getSecondaryResource(P primary, Context<P> context) {
132132
* @throws IllegalStateException if more than one candidate is found, in which case some other
133133
* mechanism might be necessary to distinguish between candidate secondary resources
134134
*/
135-
protected Optional<R> selectManagedSecondaryResource(Set<R> secondaryResources, P primary,
135+
protected Optional<R> selectTargetSecondaryResource(Set<R> secondaryResources, P primary,
136136
Context<P> context) {
137137
R desired = desired(primary, context);
138138
var targetResources = secondaryResources.stream().filter(r -> r.equals(desired)).toList();

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependentResource.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,9 @@ protected void addSecondaryToPrimaryMapperAnnotations(R desired, P primary, Stri
222222
}
223223

224224
@Override
225-
protected Optional<R> selectManagedSecondaryResource(Set<R> secondaryResources, P primary,
225+
protected Optional<R> selectTargetSecondaryResource(Set<R> secondaryResources, P primary,
226226
Context<P> context) {
227-
ResourceID managedResourceID = managedSecondaryResourceID(primary, context);
227+
ResourceID managedResourceID = targetSecondaryResourceID(primary, context);
228228
return secondaryResources.stream()
229229
.filter(r -> r.getMetadata().getName().equals(managedResourceID.getName()) &&
230230
Objects.equals(r.getMetadata().getNamespace(),
@@ -240,7 +240,7 @@ protected Optional<R> selectManagedSecondaryResource(Set<R> secondaryResources,
240240
* @param context of current reconciliation
241241
* @return id of the target managed resource
242242
*/
243-
protected ResourceID managedSecondaryResourceID(P primary, Context<P> context) {
243+
protected ResourceID targetSecondaryResourceID(P primary, Context<P> context) {
244244
return ResourceID.fromResource(desired(primary, context));
245245
}
246246

Diff for: operator-framework/src/test/java/io/javaoperatorsdk/operator/dependent/externalstate/ExternalWithStateDependentResource.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public Set<ExternalResource> fetchResources(
4040
}
4141

4242
@Override
43-
protected Optional<ExternalResource> selectManagedSecondaryResource(
43+
protected Optional<ExternalResource> selectTargetSecondaryResource(
4444
Set<ExternalResource> secondaryResources,
4545
ExternalStateCustomResource primary, Context<ExternalStateCustomResource> context) {
4646
var id = getResourceID(primary);

Diff for: operator-framework/src/test/java/io/javaoperatorsdk/operator/dependent/multipledrsametypenodiscriminator/MultipleManagedDependentNoDiscriminatorConfigMap1.java

+14
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import io.javaoperatorsdk.operator.api.reconciler.Context;
99
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.CRUDKubernetesDependentResource;
1010
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependent;
11+
import io.javaoperatorsdk.operator.processing.event.ResourceID;
1112

1213
@KubernetesDependent
1314
public class MultipleManagedDependentNoDiscriminatorConfigMap1
@@ -20,6 +21,19 @@ public MultipleManagedDependentNoDiscriminatorConfigMap1() {
2021
super(ConfigMap.class);
2122
}
2223

24+
/*
25+
* Showcases optimization to avoid computing the whole desired state by providing the ResourceID
26+
* of the target resource. In this particular case this would not be necessary, since desired
27+
* state creation is pretty lightweight. However, this might make sense in situation where the
28+
* desired state is more costly
29+
*/
30+
protected ResourceID targetSecondaryResourceID(
31+
MultipleManagedDependentNoDiscriminatorCustomResource primary,
32+
Context<MultipleManagedDependentNoDiscriminatorCustomResource> context) {
33+
return new ResourceID(primary.getMetadata().getName() + NAME_SUFFIX,
34+
primary.getMetadata().getNamespace());
35+
}
36+
2337
@Override
2438
protected ConfigMap desired(MultipleManagedDependentNoDiscriminatorCustomResource primary,
2539
Context<MultipleManagedDependentNoDiscriminatorCustomResource> context) {

0 commit comments

Comments
 (0)