From 67aec36aefb7a0da741c9514a6b9408aea6a4009 Mon Sep 17 00:00:00 2001 From: Vitor Lobo Ramos Date: Sat, 26 Oct 2024 21:25:09 -0300 Subject: [PATCH] Modify ModelMapper to return DynamicKubernetesObject for unknown GroupVersionKinds --- .../kubernetes/client/util/ModelMapper.java | 10 +++- .../client/util/ModelMapperTest.java | 51 ++++++++++++------- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/util/src/main/java/io/kubernetes/client/util/ModelMapper.java b/util/src/main/java/io/kubernetes/client/util/ModelMapper.java index 9803bb04cb..25ba91607a 100644 --- a/util/src/main/java/io/kubernetes/client/util/ModelMapper.java +++ b/util/src/main/java/io/kubernetes/client/util/ModelMapper.java @@ -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; @@ -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. * diff --git a/util/src/test/java/io/kubernetes/client/util/ModelMapperTest.java b/util/src/test/java/io/kubernetes/client/util/ModelMapperTest.java index ca631f9345..e8a419e1be 100644 --- a/util/src/test/java/io/kubernetes/client/util/ModelMapperTest.java +++ b/util/src/test/java/io/kubernetes/client/util/ModelMapperTest.java @@ -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 { @@ -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")); } }