-
Notifications
You must be signed in to change notification settings - Fork 236
feat: ResourceIDMapper for external event sources, external dependents and bulk dependents #3020
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
csviri
wants to merge
28
commits into
next
Choose a base branch
from
resource-id-mapper-bulk
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
44ea02e
improve: ExternalResourceIDProvider extended to be used in CacheKeyMa…
csviri 91ea413
wip
csviri 1df9117
improve: ResourceIDMapper and ResourceIDProvider for external resources
csviri 232d714
wip
csviri cd28c5f
wip
csviri 0baa6c7
feat: ResourceIDMapper for external event sources, external dependent…
csviri 9e64e33
wip
csviri 3928dba
wip
csviri bbed526
migration guide skeleton
csviri 323cb49
changed sample
csviri fe1dacb
wip
csviri 3a5a36d
wip
csviri 9a5f724
wip
csviri 53ef664
Update operator-framework-core/src/main/java/io/javaoperatorsdk/opera…
csviri 66a4b8e
Update operator-framework-core/src/main/java/io/javaoperatorsdk/opera…
csviri 89469f9
Update operator-framework-core/src/main/java/io/javaoperatorsdk/opera…
csviri f31dc0e
Update operator-framework-core/src/main/java/io/javaoperatorsdk/opera…
csviri bbc9b1f
format
csviri cfcbc51
javadoc
csviri 1e4b19b
migration guide
csviri 7fdb2f4
migration guide grammar
csviri 8f90ad3
docs update
csviri 579d0c2
fix: use namespaced constant to avoid potential conflicts
metacosm d8f4c31
fix: typo
metacosm bb741e8
revert change of the id,
csviri 352067e
improve javadoc for ResourceIDMapper.singleResourceResourceIDMapper
csviri dc39477
comment
csviri 3b39577
changes from CR
csviri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| --- | ||
| title: Migrating from v5.1 to v5.2 | ||
| description: Migrating from v5.1 to v5.2 | ||
| --- | ||
|
|
||
| Version 5.2 brings some breaking changes to certain components. This document provides | ||
| a migration guide for these changes. For all the new features, see the release notes. | ||
|
|
||
| ## Custom ID types across multiple components using ResourceIDMapper and ResourceIDProvider | ||
|
|
||
| Working with the id of a resource is needed across various components in the framework. | ||
| Until this version, the components provided by the framework assumed that you could easily | ||
| convert the id of a resource into a String representation. For example, | ||
| [BulkDependentResources](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/BulkDependentResource.java#L46) | ||
| worked with a `Map<String,R>` of resources, where the id was always of type String. | ||
|
|
||
| Mainly because of the need to manage external dependent resources more elegantly, | ||
| we introduced a cross-cutting concept: [`ResourceIDMapper`](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/ResourceIDMapper.java), | ||
| which gets the ID of a resource. This is used across various components, see: | ||
|
|
||
| - [`ExternalResourceCachingEventSource`](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/ExternalResourceCachingEventSource.java#L66) | ||
| - [`ExternalBulkDependentResource`](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/ExternalBulkDependentResource.java) | ||
| - [`AbstractExternalDependentResource`](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/AbstractExternalDependentResource.java#L39) | ||
| and its subclasses. | ||
|
|
||
| We also added [`ResourceIDProvider`](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/ResourceIDProvider.java), | ||
| which you can implement in your Pojo representing a resource. | ||
|
|
||
| The easiest way to migrate to this new approach is to implement this interface for your (external) resource | ||
| and set the ID type generics for the components above. The default implementation of the `ResourceIDMapper` | ||
| works with `ResourceIDProvider` (see [related implementation](https://github.com/operator-framework/java-operator-sdk/blob/main/operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/ResourceIDMapper.java#L52)). | ||
|
|
||
| If you cannot implement `ResourceIDProvider` because, for example, the class that represents the external resource is generated and final, | ||
| you can always set a custom `ResourceIDMapper` on the components above. | ||
|
|
||
| See also: | ||
| - related issue: [link](https://github.com/operator-framework/java-operator-sdk/issues/2972) | ||
| - related pull requests: | ||
| - [2970](https://github.com/operator-framework/java-operator-sdk/pull/2970) | ||
| - [3020](https://github.com/operator-framework/java-operator-sdk/pull/3020) | ||
|
|
||
|
|
||
|
|
||
|
|
||
Empty file.
71 changes: 71 additions & 0 deletions
71
...framework-core/src/main/java/io/javaoperatorsdk/operator/processing/ResourceIDMapper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Copyright Java Operator SDK Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.javaoperatorsdk.operator.processing; | ||
|
|
||
| import io.fabric8.kubernetes.api.model.HasMetadata; | ||
| import io.javaoperatorsdk.operator.processing.event.ResourceID; | ||
| import io.javaoperatorsdk.operator.processing.event.source.ExternalResourceCachingEventSource; | ||
|
|
||
| /** | ||
| * Provides id for the target resource. This mapper is used across multiple components of the | ||
| * framework, like: | ||
| * | ||
| * <ul> | ||
| * <li>{@link io.javaoperatorsdk.operator.processing.dependent.AbstractExternalDependentResource} | ||
| * <li>{@link ExternalResourceCachingEventSource} | ||
| * <li>{@link io.javaoperatorsdk.operator.processing.dependent.KubernetesBulkDependentResource} | ||
| * </ul> | ||
| * | ||
| * @see ResourceIDProvider<ID> | ||
| */ | ||
| public interface ResourceIDMapper<R, ID> { | ||
|
|
||
| /** | ||
| * @return id for the target resource. | ||
| */ | ||
| ID idFor(R resource); | ||
csviri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Can be used if a polling event source handles only single secondary resource and the id is | ||
| * String. See also docs for: {@link ExternalResourceCachingEventSource}; or in {@link | ||
| * io.javaoperatorsdk.operator.processing.dependent.AbstractExternalDependentResource} when there | ||
| * is always only one secondary resource for that dependent resource. By definition cannot be used | ||
| * for a BulkDependent resources. | ||
| * | ||
| * @return static id mapper, all resources are mapped for same id. | ||
| * @param <R> secondary resource type | ||
| */ | ||
| static <R> ResourceIDMapper<R, String> singleResourceResourceIDMapper() { | ||
| // the result could be any string, by definition would work with any value | ||
| return r -> "irrelevant"; | ||
| } | ||
|
|
||
| @SuppressWarnings({"rawtypes", "unchecked"}) | ||
| static <R, ID> ResourceIDMapper<R, ID> resourceIdProviderMapper() { | ||
| return r -> { | ||
| if (r instanceof ResourceIDProvider resourceIDProvider) { | ||
| return (ID) resourceIDProvider.resourceId(); | ||
| } else { | ||
| throw new IllegalStateException( | ||
| "Resource does not implement ExternalDependentIDProvider: " + r.getClass()); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| static <R extends HasMetadata> ResourceIDMapper<R, ResourceID> kubernetesResourceIdMapper() { | ||
| return ResourceID::fromResource; | ||
| } | ||
| } | ||
38 changes: 38 additions & 0 deletions
38
...amework-core/src/main/java/io/javaoperatorsdk/operator/processing/ResourceIDProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Copyright Java Operator SDK Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.javaoperatorsdk.operator.processing; | ||
|
|
||
| /** | ||
| * Provides the identifier for an object that represents a resource. This ID is used: | ||
| * | ||
| * <ul> | ||
| * <li>to select the target external resource for a dependent resource from the resources returned | ||
| * by {@link io.javaoperatorsdk.operator.api.reconciler.Context#getSecondaryResources(Class)}, | ||
| * <li>used in {@link ResourceIDMapper} for event sources in external resources. But also for bulk | ||
| * dependent resource see {@link | ||
| * io.javaoperatorsdk.operator.processing.dependent.ExternalBulkDependentResource}, | ||
| * <li>and external event sources, see {@link | ||
| * io.javaoperatorsdk.operator.processing.event.source.ExternalResourceCachingEventSource} | ||
| * </ul> | ||
| * | ||
| * @see ResourceIDMapper | ||
| * @param <ID> type of the id | ||
| */ | ||
| public interface ResourceIDProvider<ID> { | ||
|
|
||
| /** ID for the resource POJO that implement this interface. */ | ||
| ID resourceId(); | ||
csviri marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.