Skip to content

Commit 3dfbd8f

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

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
@@ -98,7 +98,7 @@ public Optional<R> getSecondaryResource(P primary, Context<P> context) {
9898
if (secondaryResources.isEmpty()) {
9999
return Optional.empty();
100100
} else {
101-
return selectSecondaryBasedOnDesiredState(secondaryResources, desired(primary, context));
101+
return selectManagedResource(secondaryResources, primary, context);
102102
}
103103
}
104104
}
@@ -109,14 +109,15 @@ public Optional<R> getSecondaryResource(P primary, Context<P> context) {
109109
* for optimized implementations in subclasses since this default implementation will check each
110110
* secondary candidates for equality with the specified desired state, which might end up costly.
111111
*
112-
* @param secondaryResources a Set of potential candidates of the looked for resource type
113-
* @param desired the desired state that the matching secondary resource must have to match the
114-
* primary resource
112+
* @param secondaryResources to select the target resource from
113+
*
115114
* @return the matching secondary resource or {@link Optional#empty()} if none matches
116115
* @throws IllegalStateException if more than one candidate is found, in which case some other
117116
* mechanism might be necessary to distinguish between candidate secondary resources
118117
*/
119-
protected Optional<R> selectSecondaryBasedOnDesiredState(Set<R> secondaryResources, R desired) {
118+
protected Optional<R> selectManagedResource(Set<R> secondaryResources, P primary,
119+
Context<P> context) {
120+
R desired = desired(primary, context);
120121
var targetResources =
121122
secondaryResources.stream().filter(r -> r.equals(desired)).collect(Collectors.toList());
122123
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
@@ -291,13 +291,20 @@ protected void addSecondaryToPrimaryMapperAnnotations(R desired, P primary, Stri
291291
}
292292

293293
@Override
294-
protected Optional<R> selectSecondaryBasedOnDesiredState(Set<R> secondaryResources, R desired) {
294+
protected Optional<R> selectManagedResource(Set<R> secondaryResources, P primary,
295+
Context<P> context) {
296+
ResourceID managedResourceID = managedResourceID(primary, context);
295297
return secondaryResources.stream()
296-
.filter(r -> r.getMetadata().getName().equals(desired.getMetadata().getName()) &&
297-
Objects.equals(r.getMetadata().getNamespace(), desired.getMetadata().getNamespace()))
298+
.filter(r -> r.getMetadata().getName().equals(managedResourceID.getName()) &&
299+
Objects.equals(r.getMetadata().getNamespace(),
300+
managedResourceID.getNamespace().orElse(null)))
298301
.findFirst();
299302
}
300303

304+
protected ResourceID managedResourceID(P primary, Context<P> context) {
305+
return ResourceID.fromResource(desired(primary, context));
306+
}
307+
301308
protected boolean addOwnerReference() {
302309
return garbageCollected;
303310
}

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)