8000 fix: Optional associated resource (#793) · Sgitario/java-operator-sdk@4088a3c · GitHub
[go: up one dir, main page]

Skip to content

Commit 4088a3c

Browse files
authored
fix: Optional associated resource (operator-framework#793)
1 parent 073801a commit 4088a3c

File tree

5 files changed

+13
-9
lines changed

5 files changed

+13
-9
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import io.fabric8.kubernetes.api.model.HasMetadata;
66
import io.javaoperatorsdk.operator.processing.Controller;
7+
import io.javaoperatorsdk.operator.processing.event.source.ResourceEventSource;
78

89
public class DefaultContext<P extends HasMetadata> implements Context {
910

@@ -24,8 +25,9 @@ public Optional<RetryInfo> getRetryInfo() {
2425

2526
@Override
2627
public <T> Optional<T> getSecondaryResource(Class<T> expectedType, String eventSourceName) {
27-
final var eventSource =
28+
final Optional<ResourceEventSource<P, T>> eventSource =
2829
controller.getEventSourceManager().getResourceEventSourceFor(expectedType, eventSourceName);
29-
return eventSource.map(es -> es.getAssociated(primaryResource));
30+
return eventSource.isEmpty() ? Optional.empty()
31+
: eventSource.get().getAssociated(primaryResource);
3032
}
3133
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/CachingEventSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ public void stop() throws OperatorException {
9090
}
9191

9292
@Override
93-
public T getAssociated(P primary) {
94-
return cache.get(ResourceID.fromResource(primary)).orElse(null);
93+
public Optional<T> getAssociated(P primary) {
94+
return cache.get(ResourceID.fromResource(primary));
9595
}
9696

9797
protected static class MapCache<T> implements UpdatableCache<T> {
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package io.javaoperatorsdk.operator.processing.event.source;
22

3+
import java.util.Optional;
4+
35
import io.fabric8.kubernetes.api.model.HasMetadata;
46

57
public interface ResourceEventSource<P extends HasMetadata, R> extends EventSource {
68

79
Class<R> getResourceClass();
810

9-
R getAssociated(P primary);
11+
Optional<R> getAssociated(P primary);
1012
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/controller/ControllerResourceEventSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ private void handleKubernetesClientException(Exception e) {
201201
}
202202

203203
@Override
204-
public T getAssociated(T primary) {
205-
return cache.get(ResourceID.fromResource(primary)).orElse(null);
204+
public Optional<T> getAssociated(T primary) {
205+
return cache.get(ResourceID.fromResource(primary));
206206
}
207207
}

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/InformerEventSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ private Store<T> getStore() {
138138
* @param resource the primary resource we want to retrieve the associated resource for
139139
* @return the informed resource associated with the specified primary resource
140140
*/
141-
public T getAssociated(P resource) {
141+
public Optional<T> getAssociated(P resource) {
142142
final var id = associatedWith.associatedSecondaryID(resource);
143-
return get(id).orElse(null);
143+
return get(id);
144144
}
145145

146146

0 commit comments

Comments
 (0)
0