Skip to content
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

Fix apiVersion match for standard K8s resources #2724

Merged
merged 1 commit into from
Mar 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,10 @@ public static <T extends HasMetadata> SecondaryToPrimaryMapper<T> fromOwnerRefer

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(apiVersion))
.filter(r -> r.getKind().equals(kind) && r.getApiVersion().equals(correctApiVersion))
.map(or -> ResourceID.fromOwnerReference(resource, or, clusterScope))
.collect(Collectors.toSet());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,31 @@ void secondaryToPrimaryMapperFromOwnerReference() {
assertThat(res).contains(ResourceID.fromResource(primary));
}

@Test
void secondaryToPrimaryMapperFromOwnerReferenceWhereGroupIdIsEmpty() {
var primary =
new ConfigMapBuilder()
.withNewMetadata()
.withName("test")
.withNamespace("default")
.endMetadata()
.build();
primary.getMetadata().setUid(UUID.randomUUID().toString());
var secondary =
new ConfigMapBuilder()
.withMetadata(
new ObjectMetaBuilder()
.withName("test1")
.withNamespace(primary.getMetadata().getNamespace())
.build())
.build();
secondary.addOwnerReference(primary);

var res = Mappers.fromOwnerReferences(ConfigMap.class).toPrimaryResourceIDs(secondary);

assertThat(res).contains(ResourceID.fromResource(primary));
}

@Test
void secondaryToPrimaryMapperFromOwnerReferenceFiltersByType() {
var primary = TestUtils.testCustomResource();
Expand Down