Skip to content

Commit a516e76

Browse files
committed
refactoring for better api
Signed-off-by: Attila Mészáros <csviri@gmail.com>
1 parent f2c5708 commit a516e76

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public Optional<R> getSecondaryResource(P primary, Context<P> context) {
106106
if (secondaryResources.isEmpty()) {
107107
return Optional.empty();
108108
} else {
109-
return selectSecondaryBasedOnDesiredState(secondaryResources, desired(primary, context));
109+
return selectManagedResource(secondaryResources, primary, context);
110110
}
111111
}
112112
}
@@ -117,14 +117,15 @@ public Optional<R> getSecondaryResource(P primary, Context<P> context) {
117117
* for optimized implementations in subclasses since this default implementation will check each
118118
* secondary candidates for equality with the specified desired state, which might end up costly.
119119
*
120-
* @param secondaryResources a Set of potential candidates of the looked for resource type
121-
* @param desired the desired state that the matching secondary resource must have to match the
122-
* primary resource
120+
* @param secondaryResources to select the target resource from
121+
*
123122
* @return the matching secondary resource or {@link Optional#empty()} if none matches
124123
* @throws IllegalStateException if more than one candidate is found, in which case some other
125124
* mechanism might be necessary to distinguish between candidate secondary resources
126125
*/
127-
protected Optional<R> selectSecondaryBasedOnDesiredState(Set<R> secondaryResources, R desired) {
126+
protected Optional<R> selectManagedResource(Set<R> secondaryResources, P primary,
127+
Context<P> context) {
128+
R desired = desired(primary, context);
128129
var targetResources =
129130
secondaryResources.stream().filter(r -> r.equals(desired)).collect(Collectors.toList());
130131
if (targetResources.size() > 1) {

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,13 +296,20 @@ protected void addSecondaryToPrimaryMapperAnnotations(R desired, P primary, Stri
296296
}
297297

298298
@Override
299-
protected Optional<R> selectSecondaryBasedOnDesiredState(Set<R> secondaryResources, R desired) {
299+
protected Optional<R> selectManagedResource(Set<R> secondaryResources, P primary,
300+
Context<P> context) {
301+
ResourceID managedResourceID = managedResourceID(primary, context);
300302
return secondaryResources.stream()
301-
.filter(r -> r.getMetadata().getName().equals(desired.getMetadata().getName()) &&
302-
Objects.equals(r.getMetadata().getNamespace(), desired.getMetadata().getNamespace()))
303+
.filter(r -> r.getMetadata().getName().equals(managedResourceID.getName()) &&
304+
Objects.equals(r.getMetadata().getNamespace(),
305+
managedResourceID.getNamespace().orElse(null)))
303306
.findFirst();
304307
}
305308

309+
protected ResourceID managedResourceID(P primary, Context<P> context) {
310+
return ResourceID.fromResource(desired(primary, context));
311+
}
312+
306313
protected boolean addOwnerReference() {
307314
return garbageCollected;
308315
}

operator-framework/src/test/java/io/javaoperatorsdk/operator/sample/primarytosecondaydependent/ConfigMapDependent.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ public ConfigMapDependent() {
1616
}
1717

1818
@Override
19-
protected ConfigMap desired(PrimaryToSecondaryDependentCustomResource primary, Context<PrimaryToSecondaryDependentCustomResource> context) {
19+
protected ConfigMap desired(PrimaryToSecondaryDependentCustomResource primary,
20+
Context<PrimaryToSecondaryDependentCustomResource> context) {
2021
return new ConfigMapBuilder()
21-
.withMetadata(new ObjectMetaBuilder()
22-
.withName(TEST_CONFIG_MAP_NAME)
23-
.withNamespace(primary.getMetadata().getName())
24-
.build())
25-
.build();
22+
.withMetadata(new ObjectMetaBuilder()
23+
.withName(TEST_CONFIG_MAP_NAME)
24+
.withNamespace(primary.getMetadata().getName())
25+
.build())
26+
.build();
2627
}
2728
}

0 commit comments

Comments
 (0)