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

Modify ModelMapper to return DynamicKubernetesObject #3770

Merged
merged 1 commit into from
Oct 28, 2024
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
10 changes: 9 additions & 1 deletion util/src/main/java/io/kubernetes/client/util/ModelMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import io.kubernetes.client.common.KubernetesObject;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.util.exception.IncompleteDiscoveryException;
import io.kubernetes.client.util.generic.dynamic.DynamicKubernetesObject;
import java.io.File;

import java.io.IOException;
Expand Down Expand Up @@ -215,9 +216,16 @@ public static Class<?> getApiTypeClass(String group, String version, String kind
if (clazz != null) {
return clazz;
}
return preBuiltGetApiTypeClass(group, version, kind);

clazz = preBuiltGetApiTypeClass(group, version, kind);

if (clazz != null) {
return clazz;
}
return DynamicKubernetesObject.class;
}


/**
* Gets the GVK by the given model class.
*
Expand Down
51 changes: 32 additions & 19 deletions util/src/test/java/io/kubernetes/client/util/ModelMapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import io.kubernetes.client.openapi.models.V1CustomResourceDefinition;
import io.kubernetes.client.openapi.models.V1Deployment;
import io.kubernetes.client.openapi.models.V1Pod;
import io.kubernetes.client.util.generic.dynamic.DynamicKubernetesObject;

import org.junit.jupiter.api.Test;

class ModelMapperTest {
Expand All @@ -35,41 +37,52 @@ void prebuiltModelMapping() {

@Test
void addingModel() {
Class objClass =
new Object() {
{
}
}.getClass();
Class<?> objClass = new Object() {}.getClass();

assertThat(ModelMapper.getApiTypeClass("example.io/V1", "Toss"))
.isEqualTo(DynamicKubernetesObject.class);

ModelMapper.addModelMap("example.io", "v1", "Toss", objClass);

assertThat(ModelMapper.getApiTypeClass("example.io/v1", "Toss"))
.isEqualTo(objClass);
.isEqualTo(objClass);
assertThat(ModelMapper.getApiTypeClass("example.io", "v1", "Toss"))
.isEqualTo(objClass);
.isEqualTo(objClass);

assertThat(ModelMapper.getApiTypeClass("example.io/V1", "Toss"))
.isEqualTo(DynamicKubernetesObject.class);
assertThat(ModelMapper.getApiTypeClass("example.io", "V1", "Toss"))
.isEqualTo(DynamicKubernetesObject.class);

assertThat(ModelMapper.getApiTypeClass("example.io/V1", "Toss")).isNull();
assertThat(ModelMapper.getApiTypeClass("example.io", "V1", "Toss")).isNull();
assertThat(ModelMapper.getApiTypeClass("example.io/v1", "Tofu"))
.isEqualTo(DynamicKubernetesObject.class);
assertThat(ModelMapper.getApiTypeClass("example.io", "v1", "Tofu"))
.isEqualTo(DynamicKubernetesObject.class);

assertThat(ModelMapper.getApiTypeClass("v1", "Togu"))
.isEqualTo(DynamicKubernetesObject.class);

assertThat(ModelMapper.getApiTypeClass("example.io/v1", "Tofu")).isNull();
assertThat(ModelMapper.getApiTypeClass("example.io", "v1", "Tofu")).isNull();
ModelMapper.addModelMap("", "v1", "Togu", objClass);

assertThat(ModelMapper.getApiTypeClass("v1", "Togu")).isNull();
ModelMapper.addModelMap("v1", "Togu", objClass);
assertThat(ModelMapper.getApiTypeClass("", "v1", "Togu"))
.isEqualTo(objClass);
.isEqualTo(objClass);
assertThat(ModelMapper.getApiTypeClass("v1", "Togu"))
.isEqualTo(objClass);
.isEqualTo(objClass);
}

@Test
void getApiTypeClassReturnsDynamicKubernetesObjectWhenClassNotFound() {
assertThat(ModelMapper.getApiTypeClass("unknown.group", "v1", "UnknownKind"))
.isEqualTo(DynamicKubernetesObject.class);
}

@Test
void preBuiltGetGroupVersionKindByClass() {
assertThat(ModelMapper.preBuiltGetGroupVersionKindByClass(V1Pod.class))
.hasValue(new GroupVersionKind("", "v1", "Pod"));
assertThat(ModelMapper.preBuiltGetGroupVersionKindByClass(V1Deployment.class))
.hasValue(new GroupVersionKind("", "v1", "Deployment"));
.hasValue(new GroupVersionKind("", "v1", "Deployment"));
assertThat(ModelMapper.preBuiltGetGroupVersionKindByClass(V1Pod.class))
.hasValue(new GroupVersionKind("", "v1", "Pod"));
assertThat(ModelMapper.preBuiltGetGroupVersionKindByClass(V1CustomResourceDefinition.class))
.hasValue(new GroupVersionKind("", "v1", "CustomResourceDefinition"));
.hasValue(new GroupVersionKind("", "v1", "CustomResourceDefinition"));
}
}
Loading