Skip to content

improve: owner reference mappers checks only group not apiVersion#3302

Open
csviri wants to merge 2 commits intooperator-framework:mainfrom
csviri:owner-ref-mapper
Open

improve: owner reference mappers checks only group not apiVersion#3302
csviri wants to merge 2 commits intooperator-framework:mainfrom
csviri:owner-ref-mapper

Conversation

@csviri
Copy link
Copy Markdown
Collaborator

@csviri csviri commented Apr 20, 2026

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

Copilot AI review requested due to automatic review settings April 20, 2026 10:25
@openshift-ci openshift-ci bot requested review from metacosm and xstefank April 20, 2026 10:25
@csviri
Copy link
Copy Markdown
Collaborator Author

csviri commented Apr 20, 2026

cc @shawkins

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 an apiVersion string.
  • 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.

Comment on lines 101 to 110
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());
Copy link

Copilot AI Apr 20, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@csviri
Copy link
Copy Markdown
Collaborator Author

csviri commented Apr 20, 2026

see also: keycloak/keycloak#48030 (comment)

csviri added 2 commits April 20, 2026 12:46
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>
Signed-off-by: Attila Mészáros <a_meszaros@apple.com>
@csviri csviri force-pushed the owner-ref-mapper branch from 0345443 to e57272d Compare April 20, 2026 10:46
@csviri csviri requested a review from shawkins April 20, 2026 10:46
@Test
void extractsGroupFromApiVersion() {
assertThat(getGroup("v1")).isEqualTo("");
assertThat(getGroup("/v1")).isEqualTo("");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a valid apiVersion String

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see conversation here: #3302 (comment)

assertThat(getGroup("v1")).isEqualTo("");
assertThat(getGroup("/v1")).isEqualTo("");
assertThat(getGroup("apps/v1")).isEqualTo("apps");
assertThat(getGroup("/apps/v1")).isEqualTo("apps");
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a valid apiVersion so I don't think we should cover this case, apart maybe to throw an IllegalArgumentException

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)))
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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…

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @metacosm see this part. Also if you check the related commit, that explains the issue here.

@csviri
Copy link
Copy Markdown
Collaborator Author

csviri commented Apr 20, 2026

Maybe would be more pc to target next with this PR.

Copy link
Copy Markdown
Collaborator

@xstefank xstefank left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have anything else to add to what was already mentioned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants