- 
                Notifications
    You must be signed in to change notification settings 
- Fork 233
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
  25
  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
          
          
            25 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      9b331da
              
                improve: ExternalResourceIDProvider extended to be used in CacheKeyMa…
              
              
                csviri cb532df
              
                wip
              
              
                csviri d0867e4
              
                improve: ResourceIDMapper and ResourceIDProvider for external resources
              
              
                csviri f615a86
              
                wip
              
              
                csviri a620cbe
              
                wip
              
              
                csviri c295061
              
                feat: ResourceIDMapper for external event sources, external dependent…
              
              
                csviri 4d274b4
              
                wip
              
              
                csviri c31eab1
              
                wip
              
              
                csviri 9c7b6ad
              
                migration guide skeleton
              
              
                csviri b39cafe
              
                changed sample
              
              
                csviri e511b1a
              
                wip
              
              
                csviri bae1e76
              
                wip
              
              
                csviri 5020e76
              
                wip
              
              
                csviri 5332039
              
                Update operator-framework-core/src/main/java/io/javaoperatorsdk/opera…
              
              
                csviri f8ccb78
              
                Update operator-framework-core/src/main/java/io/javaoperatorsdk/opera…
              
              
                csviri 3b25831
              
                Update operator-framework-core/src/main/java/io/javaoperatorsdk/opera…
              
              
                csviri 1bb99cd
              
                Update operator-framework-core/src/main/java/io/javaoperatorsdk/opera…
              
              
                csviri 35be84a
              
                format
              
              
                csviri be27232
              
                javadoc
              
              
                csviri bdf08c4
              
                migration guide
              
              
                csviri 261e741
              
                migration guide grammar
              
              
                csviri 108cb82
              
                docs update
              
              
                csviri 70038a6
              
                fix: use namespaced constant to avoid potential conflicts
              
              
                metacosm e5d0380
              
                fix: typo
              
              
                metacosm 1bf7b17
              
                revert change of the id,
              
              
                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.
          
    
        
          
          
            67 changes: 67 additions & 0 deletions
          
          67 
        
  ...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,67 @@ | ||
| /* | ||
| * 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} | ||
| * | ||
| * @return static id mapper, all resources are mapped for same id. | ||
| * @param <R> secondary resource type | ||
| */ | ||
| static <R> ResourceIDMapper<R, String> singleResourceResourceIDMapper() { | ||
| return r -> "id"; | ||
| } | ||
|  | ||
| @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
    
  
  
    
              
  
    
      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.