From 4b968203b70159b97b5a767d46511994434b68d9 Mon Sep 17 00:00:00 2001 From: Chris Laprun Date: Thu, 22 Jun 2023 21:09:20 +0200 Subject: [PATCH] Properly configure KubernetesSerialization for OpenShiftClient - Fix KubernetesClientObjectMapperCDITest - Prevent devservices from starting (seems needed for Windows) - Add OpenShiftClientObjectMapperCDITest --- .../KubernetesClientObjectMapperCDITest.java | 2 - .../openshift-client/deployment/pom.xml | 5 ++ .../OpenShiftClientObjectMapperCDITest.java | 59 +++++++++++++++++++ .../runtime/OpenShiftClientProducer.java | 14 ++++- 4 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 extensions/openshift-client/deployment/src/test/java/io/quarkus/openshift/client/deployment/OpenShiftClientObjectMapperCDITest.java diff --git a/extensions/kubernetes-client/deployment/src/test/java/io/quarkus/kubernetes/client/deployment/KubernetesClientObjectMapperCDITest.java b/extensions/kubernetes-client/deployment/src/test/java/io/quarkus/kubernetes/client/deployment/KubernetesClientObjectMapperCDITest.java index 256e75a5071db..e1f778b246b05 100644 --- a/extensions/kubernetes-client/deployment/src/test/java/io/quarkus/kubernetes/client/deployment/KubernetesClientObjectMapperCDITest.java +++ b/extensions/kubernetes-client/deployment/src/test/java/io/quarkus/kubernetes/client/deployment/KubernetesClientObjectMapperCDITest.java @@ -42,8 +42,6 @@ public void kubernetesClientUsesCustomizedObjectMapper() { @RegisterExtension static QuarkusUnitTest runner = new QuarkusUnitTest() - .withApplicationRoot((jar) -> jar - .addClasses(KubernetesClientCDITest.Customizer.class)) .overrideConfigKey("quarkus.kubernetes-client.devservices.enabled", "false"); @Singleton diff --git a/extensions/openshift-client/deployment/pom.xml b/extensions/openshift-client/deployment/pom.xml index 5f54dae121772..e5cd572c2c931 100644 --- a/extensions/openshift-client/deployment/pom.xml +++ b/extensions/openshift-client/deployment/pom.xml @@ -23,6 +23,11 @@ quarkus-kubernetes-client-deployment ${project.version} + + io.quarkus + quarkus-junit5-internal + test + diff --git a/extensions/openshift-client/deployment/src/test/java/io/quarkus/openshift/client/deployment/OpenShiftClientObjectMapperCDITest.java b/extensions/openshift-client/deployment/src/test/java/io/quarkus/openshift/client/deployment/OpenShiftClientObjectMapperCDITest.java new file mode 100644 index 0000000000000..5f7478d873ee8 --- /dev/null +++ b/extensions/openshift-client/deployment/src/test/java/io/quarkus/openshift/client/deployment/OpenShiftClientObjectMapperCDITest.java @@ -0,0 +1,59 @@ +package io.quarkus.openshift.client.deployment; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import jakarta.inject.Inject; +import jakarta.inject.Singleton; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import io.fabric8.kubernetes.api.model.ObjectMeta; +import io.fabric8.openshift.client.OpenShiftClient; +import io.quarkus.kubernetes.client.KubernetesClientObjectMapper; +import io.quarkus.kubernetes.client.KubernetesClientObjectMapperCustomizer; +import io.quarkus.test.QuarkusUnitTest; + +public class OpenShiftClientObjectMapperCDITest { + + @Inject + OpenShiftClient client; + + @Inject + @KubernetesClientObjectMapper + ObjectMapper objectMapper; + + @Test + public void kubernetesClientObjectMapperCustomizer() throws JsonProcessingException { + final var result = objectMapper.readValue("{\"quarkusName\":\"the-name\"}", ObjectMeta.class); + assertEquals("the-name", result.getName()); + } + + @Test + public void kubernetesClientUsesCustomizedObjectMapper() { + final var result = client.getKubernetesSerialization() + .unmarshal("{\"quarkusName\":\"the-name\"}", ObjectMeta.class); + assertEquals("the-name", result.getName()); + } + + @RegisterExtension + static QuarkusUnitTest runner = new QuarkusUnitTest() + .overrideConfigKey("quarkus.kubernetes-client.devservices.enabled", "false"); + + @Singleton + public static class Customizer implements KubernetesClientObjectMapperCustomizer { + @Override + public void customize(ObjectMapper objectMapper) { + objectMapper.addMixIn(ObjectMeta.class, ObjectMetaMixin.class); + } + + private static final class ObjectMetaMixin { + @JsonProperty("quarkusName") + String name; + } + } +} diff --git a/extensions/openshift-client/runtime/src/main/java/io/quarkus/it/openshift/client/runtime/OpenShiftClientProducer.java b/extensions/openshift-client/runtime/src/main/java/io/quarkus/it/openshift/client/runtime/OpenShiftClientProducer.java index 5c83f9dbca32b..adbfd8b9bb335 100644 --- a/extensions/openshift-client/runtime/src/main/java/io/quarkus/it/openshift/client/runtime/OpenShiftClientProducer.java +++ b/extensions/openshift-client/runtime/src/main/java/io/quarkus/it/openshift/client/runtime/OpenShiftClientProducer.java @@ -7,7 +7,8 @@ import org.jboss.logging.Logger; import io.fabric8.kubernetes.client.Config; -import io.fabric8.openshift.client.DefaultOpenShiftClient; +import io.fabric8.kubernetes.client.KubernetesClientBuilder; +import io.fabric8.kubernetes.client.utils.KubernetesSerialization; import io.fabric8.openshift.client.OpenShiftClient; import io.fabric8.openshift.client.OpenShiftConfig; import io.quarkus.arc.DefaultBean; @@ -22,11 +23,18 @@ public class OpenShiftClientProducer { @DefaultBean @Singleton @Produces - public OpenShiftClient openShiftClient(Config config) { + public OpenShiftClient openShiftClient(KubernetesSerialization kubernetesSerialization, + Config config) { // TODO - Temporary fix for https://github.com/fabric8io/kubernetes-client/pull/3347 + WithOpenShiftTestServer final OpenShiftConfig openShiftConfig = new OpenShiftConfig(config); openShiftConfig.setHttp2Disable(config.isHttp2Disable()); - client = new DefaultOpenShiftClient(openShiftConfig); + + client = new KubernetesClientBuilder() + .withConfig(openShiftConfig) + .withKubernetesSerialization(kubernetesSerialization) + .build() + .adapt(OpenShiftClient.class); + return client; }