|
25 | 25 | import io.javaoperatorsdk.operator.api.config.ControllerConfiguration; |
26 | 26 | import io.javaoperatorsdk.operator.api.reconciler.dependent.managed.ManagedWorkflowAndDependentResourceContext; |
27 | 27 | import io.javaoperatorsdk.operator.processing.event.EventSourceRetriever; |
| 28 | +import io.javaoperatorsdk.operator.processing.event.ResourceID; |
28 | 29 | import io.javaoperatorsdk.operator.processing.event.source.IndexerResourceCache; |
29 | 30 |
|
30 | 31 | public interface Context<P extends HasMetadata> { |
@@ -114,6 +115,88 @@ default <R> Stream<R> getSecondaryResourcesAsStream(Class<R> expectedType) { |
114 | 115 |
|
115 | 116 | <R> Optional<R> getSecondaryResource(Class<R> expectedType, String eventSourceName); |
116 | 117 |
|
| 118 | + /** |
| 119 | + * Retrieves a specific secondary resource by its {@link ResourceID} from the event source |
| 120 | + * identified by the given name. |
| 121 | + * |
| 122 | + * <p>This is a typed convenience over manually retrieving the {@link |
| 123 | + * io.javaoperatorsdk.operator.processing.event.source.EventSource} and calling its cache. When |
| 124 | + * the underlying event source implements {@link |
| 125 | + * io.javaoperatorsdk.operator.processing.event.source.Cache}, the lookup is a direct cache lookup |
| 126 | + * and read-cache-after-write consistent. |
| 127 | + * |
| 128 | + * <p>{@code eventSourceName} may be {@code null}. When {@code null} and {@code expectedType} is |
| 129 | + * part of a managed workflow whose activation condition may not have registered the event source, |
| 130 | + * an empty {@link Optional} is returned instead of throwing {@link |
| 131 | + * io.javaoperatorsdk.operator.processing.event.NoEventSourceForClassException}. |
| 132 | + * |
| 133 | + * @param expectedType the class representing the type of secondary resource to retrieve |
| 134 | + * @param eventSourceName the name of the event source to look in (may be {@code null}) |
| 135 | + * @param resourceID the {@link ResourceID} identifying the secondary resource |
| 136 | + * @param <R> the type of secondary resource to retrieve |
| 137 | + * @return an {@link Optional} containing the matching secondary resource, or {@link |
| 138 | + * Optional#empty()} if none matches |
| 139 | + * @throws io.javaoperatorsdk.operator.processing.event.NoEventSourceForClassException if no event |
| 140 | + * source is registered for the given type and name (and no workflow activation condition |
| 141 | + * accounts for it) |
| 142 | + * @since 5.3.5 |
| 143 | + */ |
| 144 | + <R> Optional<R> getSecondaryResource( |
| 145 | + Class<R> expectedType, String eventSourceName, ResourceID resourceID); |
| 146 | + |
| 147 | + /** |
| 148 | + * Convenience overload of {@link #getSecondaryResource(Class, String, ResourceID)} that |
| 149 | + * constructs the {@link ResourceID} using the given name and the primary resource's namespace. |
| 150 | + * |
| 151 | + * <p>If the primary resource is cluster-scoped (no namespace), the lookup is performed against |
| 152 | + * the cluster scope. To target a specific namespace from a cluster-scoped primary, use {@link |
| 153 | + * #getSecondaryResource(Class, String, ResourceID)} directly. |
| 154 | + * |
| 155 | + * <p>{@code eventSourceName} may be {@code null} with the same semantics as in {@link |
| 156 | + * #getSecondaryResource(Class, String, ResourceID)}. |
| 157 | + * |
| 158 | + * @param expectedType the class representing the type of secondary resource to retrieve |
| 159 | + * @param eventSourceName the name of the event source to look in (may be {@code null}) |
| 160 | + * @param name the name of the secondary resource (namespace inferred from the primary) |
| 161 | + * @param <R> the type of secondary resource to retrieve |
| 162 | + * @return an {@link Optional} containing the matching secondary resource, or {@link |
| 163 | + * Optional#empty()} if none matches |
| 164 | + * @since 5.3.5 |
| 165 | + */ |
| 166 | + default <R> Optional<R> getSecondaryResource( |
| 167 | + Class<R> expectedType, String eventSourceName, String name) { |
| 168 | + return getSecondaryResource( |
| 169 | + expectedType, |
| 170 | + eventSourceName, |
| 171 | + new ResourceID(name, getPrimaryResource().getMetadata().getNamespace())); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Retrieves a {@link Stream} of the secondary resources of the specified type from the event |
| 176 | + * source identified by the given name. Useful when several event sources are registered for the |
| 177 | + * same type and you need to scope retrieval to one of them, or when you want to apply a custom |
| 178 | + * filter at the call site. |
| 179 | + * |
| 180 | + * <p>When the underlying event source implements {@link ResourceCache}, the stream is |
| 181 | + * read-cache-after-write consistent. |
| 182 | + * |
| 183 | + * <p>{@code eventSourceName} may be {@code null} with the same semantics as in {@link |
| 184 | + * #getSecondaryResource(Class, String, ResourceID)}: when {@code null} and {@code expectedType} |
| 185 | + * is part of a managed workflow whose activation condition may not have registered the event |
| 186 | + * source, an empty {@link Stream} is returned instead of throwing {@link |
| 187 | + * io.javaoperatorsdk.operator.processing.event.NoEventSourceForClassException}. |
| 188 | + * |
| 189 | + * @param expectedType the class representing the type of secondary resources to retrieve |
| 190 | + * @param eventSourceName the name of the event source to look in (may be {@code null}) |
| 191 | + * @param <R> the type of secondary resources to retrieve |
| 192 | + * @return a {@link Stream} of secondary resources of the specified type |
| 193 | + * @throws io.javaoperatorsdk.operator.processing.event.NoEventSourceForClassException if no event |
| 194 | + * source is registered for the given type and name (and no workflow activation condition |
| 195 | + * accounts for it) |
| 196 | + * @since 5.3.5 |
| 197 | + */ |
| 198 | + <R> Stream<R> getSecondaryResourcesAsStream(Class<R> expectedType, String eventSourceName); |
| 199 | + |
117 | 200 | ControllerConfiguration<P> getControllerConfiguration(); |
118 | 201 |
|
119 | 202 | /** |
|
0 commit comments