improve: owner reference mappers checks only group not apiVersion#3302
improve: owner reference mappers checks only group not apiVersion#3302csviri wants to merge 2 commits intooperator-framework:mainfrom
Conversation
|
cc @shawkins |
There was a problem hiding this comment.
Pull request overview
This PR updates owner-reference-based secondary-to-primary mapping so events still propagate when the owner reference points to an older version of the same API group (e.g., apps/v1beta1 vs apps/v1), by matching on group instead of full apiVersion.
Changes:
- Update
Mappers.fromOwnerReferences(...)to match owner references by kind + API group (ignoring version). - Add
ReconcilerUtilsInternal.getGroup(String apiVersion)helper to extract the API group from anapiVersionstring. - Add a unit test validating group extraction behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/Mappers.java |
Relaxes owner-reference matching from exact apiVersion to API group comparison. |
operator-framework-core/src/main/java/io/javaoperatorsdk/operator/ReconcilerUtilsInternal.java |
Introduces getGroup helper for parsing API group from apiVersion. |
operator-framework-core/src/test/java/io/javaoperatorsdk/operator/ReconcilerUtilsInternalTest.java |
Adds coverage for getGroup parsing behavior. |
| public static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromOwnerReferences( | ||
| String apiVersion, String kind, boolean clusterScope) { | ||
| String correctApiVersion = apiVersion.startsWith("/") ? apiVersion.substring(1) : apiVersion; | ||
| return resource -> | ||
| resource.getMetadata().getOwnerReferences().stream() | ||
| .filter(r -> r.getKind().equals(kind) && r.getApiVersion().equals(correctApiVersion)) | ||
| .filter( | ||
| r -> | ||
| r.getKind().equals(kind) | ||
| && getGroup(r.getApiVersion()).equals(getGroup(apiVersion))) | ||
| .map(or -> ResourceID.fromOwnerReference(resource, or, clusterScope)) | ||
| .collect(Collectors.toSet()); |
There was a problem hiding this comment.
This change intentionally relaxes ownerReference matching from full apiVersion to just group. Please add a focused unit test in the existing MappersTest that demonstrates the new behavior (same group + kind but different versions should still map), so future refactors don’t accidentally revert to strict apiVersion matching.
|
see also: keycloak/keycloak#48030 (comment) |
In case of a resource is has a new version, but owner reference still points to the old version we should still propapage the event. Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
0345443 to
e57272d
Compare
| @Test | ||
| void extractsGroupFromApiVersion() { | ||
| assertThat(getGroup("v1")).isEqualTo(""); | ||
| assertThat(getGroup("/v1")).isEqualTo(""); |
There was a problem hiding this comment.
This isn't a valid apiVersion String
| assertThat(getGroup("v1")).isEqualTo(""); | ||
| assertThat(getGroup("/v1")).isEqualTo(""); | ||
| assertThat(getGroup("apps/v1")).isEqualTo("apps"); | ||
| assertThat(getGroup("/apps/v1")).isEqualTo("apps"); |
There was a problem hiding this comment.
This isn't a valid apiVersion so I don't think we should cover this case, apart maybe to throw an IllegalArgumentException
There was a problem hiding this comment.
I made this so we are backwards compatible with the current implementation marked you on the related code part. But I tend to agree that we should make it throw such exception in next minor version.
| .filter( | ||
| r -> | ||
| r.getKind().equals(kind) | ||
| && getGroup(r.getApiVersion()).equals(getGroup(apiVersion))) |
There was a problem hiding this comment.
I'm not sure this is the correct behavior: shouldn't there be a conversion hook instead? It seems dangerous to potentially match resources with differing versions…
There was a problem hiding this comment.
Conversion hooks, does not update the owner references of the resource. Maybe we should have an integration test also this, to see how exactly and what happens. I will add one.
| return v1Length; | ||
| } | ||
|
|
||
| public static String getGroup(String apiVersion) { |
There was a problem hiding this comment.
This method should probably eventually move to the client code itself
|
|
||
| public static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromOwnerReferences( | ||
| String apiVersion, String kind, boolean clusterScope) { | ||
| String correctApiVersion = apiVersion.startsWith("/") ? apiVersion.substring(1) : apiVersion; |
There was a problem hiding this comment.
cc @metacosm see this part. Also if you check the related commit, that explains the issue here.
|
Maybe would be more pc to target |
xstefank
left a comment
There was a problem hiding this comment.
I don't have anything else to add to what was already mentioned.
In case of a resource is has a new version, but owner reference
still points to the old version we should still propapage the event.
Signed-off-by: Attila Mészáros a_meszaros@apple.com