Skip to content

fix(model-core) apiVersion for core k8s resources #6941

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 2 commits 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 @@ -25,7 +25,7 @@ private ApiVersionUtil() {

/**
* Extracts apiGroupName from apiGroupVersion when in resource for apiGroupName/apiGroupVersion combination
*
*
* @param <T> Template argument provided
* @param item resource which is being used
* @param apiGroup apiGroupName present if any
Expand All @@ -42,7 +42,7 @@ public static <T> String apiGroup(T item, String apiGroup) {

/**
* Returns the api version falling back to the items apiGroupVersion if not null.
*
*
* @param <T> type of parameter
* @param item item to be processed
* @param apiVersion apiVersion string
Expand All @@ -59,7 +59,7 @@ public static <T> String apiVersion(T item, String apiVersion) {

/**
* Separates apiGroupVersion for apiGroupName/apiGroupVersion combination.
*
*
* @param apiVersion The apiGroupVersion or apiGroupName/apiGroupVersion combo.
* @return Just the apiGroupVersion part without the apiGroupName.
*/
Expand All @@ -75,7 +75,7 @@ public static String trimVersion(String apiVersion) {

/**
* Separates apiGroupName for apiGroupName/apiGroupVersion combination.
*
*
* @param apiVersion The apiGroupVersion or apiGroupName/apiGroupVersion combo.
* @return Just the apiGroupName part without the apiGroupName, or apiVersion if no separator is found.
*/
Expand All @@ -91,7 +91,7 @@ public static String trimGroup(String apiVersion) {

/**
* Separates apiGroupName for apiGroupName/apiGroupVersion combination.
*
*
* @param apiVersion The apiGroupVersion or apiGroupName/apiGroupVersion combo.
* @return Just the apiGroupName part without the apiGroupName, or null if no separator is found.
*/
Expand All @@ -110,9 +110,6 @@ public static String trimGroupOrNull(String apiVersion) {
* @return version if group is null or empty, joined string otherwise.
*/
public static String joinApiGroupAndVersion(String group, String version) {
if (Utils.isNullOrEmpty(group)) {
return version;
}
return group + "/" + version;
return HasMetadata.getApiVersion(group, version);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static String getApiVersion(Class<?> clazz) {
final String group = getGroup(clazz);
final String version = getVersion(clazz);
if (group != null && version != null) {
return group + "/" + version;
return getApiVersion(group, version);
}
if (group != null || version != null) {
throw new IllegalArgumentException(
Expand All @@ -86,6 +86,14 @@ static String getApiVersion(Class<?> clazz) {
return null;
}

static String getApiVersion(String group, String version) {
Objects.requireNonNull(version);
if (group == null || group.isBlank()) {
return version;
}
return group + "/" + version;
}

/**
* Retrieves the group associated with the specified HasMetadata as defined by the {@link Group} annotation.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ void addingAndRemovingOwnerReferenceShouldWork() {
assertFalse(hasMetadata.hasOwnerReferenceFor(owner2));
}

@Test
void apiVersionOfConfigMapShouldBeCorrect() {
assertEquals("v1", HasMetadata.getApiVersion(ConfigMap.class));
}

@Test
void addingSameOwnerReferenceMultipleTimesShouldAddItOnlyOnce() {
HasMetadata hasMetadata = new Default();
Expand Down
Loading