Skip to content

Commit

Permalink
Merge pull request #3770 from scovl/scovl-patch-01
Browse files Browse the repository at this point in the history
Modify ModelMapper to return DynamicKubernetesObject
  • Loading branch information
k8s-ci-robot authored Oct 28, 2024
2 parents ffcafa6 + 67aec36 commit a495270
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
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"));
}
}

0 comments on commit a495270

Please sign in to comment.