Skip to content

feature: add from first reference to ResourceID too #846

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

Merged
merged 3 commits into from
Jan 19, 2022
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 @@ -13,6 +13,16 @@ public static ResourceID fromResource(HasMetadata resource) {
resource.getMetadata().getNamespace());
}

public static Optional<ResourceID> fromFirstOwnerReference(HasMetadata resource) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would still put this method on the Mappers class because it's not easily findable here, imo.

Copy link
Collaborator Author

@csviri csviri Jan 19, 2022

Choose a reason for hiding this comment

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

Based on the current situation it would fit also there. But ResourceID already has fromResource static initializer. A lean usually to have those methods directly in the class, basically an utility class is always harder to find (actually already a problem if there is need to search for it).
Currently Mapper contains static init methods for PrimaryResourceRetriever, these might be also directly on that interface, so its obviouse how to create them and not search for Mappers.

So I kinda lean towards this, to have it as it is now. If you insist we can move it however, ok with that too.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah yes, and the method doesn't have the same exact semantics so yes, it makes kind of sense to keep it on ResourceID. Diff reviews sometimes make you miss context… 🤦🏼

var ownerReferences = resource.getMetadata().getOwnerReferences();
if (!ownerReferences.isEmpty()) {
return Optional.of(new ResourceID(ownerReferences.get(0).getName(),
resource.getMetadata().getNamespace()));
} else {
return Optional.empty();
}
}

private final String name;
private final String namespace;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,8 @@ public static <T extends HasMetadata> PrimaryResourcesRetriever<T> fromLabel(
}

public static <T extends HasMetadata> PrimaryResourcesRetriever<T> fromOwnerReference() {
return resource -> {
var ownerReferences = resource.getMetadata().getOwnerReferences();
if (!ownerReferences.isEmpty()) {
return Set.of(new ResourceID(ownerReferences.get(0).getName(),
resource.getMetadata().getNamespace()));
} else {
return Collections.emptySet();
}
};
return resource -> ResourceID.fromFirstOwnerReference(resource).map(Set::of)
.orElse(Collections.emptySet());
}

private static <T extends HasMetadata> PrimaryResourcesRetriever<T> fromMetadata(
Expand Down