-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: customizable KubernetesClient-specific ObjectMapper and Kuberne…
…tesSerialization Signed-off-by: Marc Nuri <[email protected]>
- Loading branch information
Showing
10 changed files
with
237 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...est/java/io/quarkus/kubernetes/client/deployment/KubernetesClientObjectMapperCDITest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package io.quarkus.kubernetes.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.kubernetes.client.KubernetesClient; | ||
import io.quarkus.kubernetes.client.KubernetesClientObjectMapper; | ||
import io.quarkus.kubernetes.client.KubernetesClientObjectMapperCustomizer; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class KubernetesClientObjectMapperCDITest { | ||
|
||
@Inject | ||
KubernetesClient kubernetesClient; | ||
|
||
@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 = kubernetesClient.getKubernetesSerialization() | ||
.unmarshal("{\"quarkusName\":\"the-name\"}", ObjectMeta.class); | ||
assertEquals("the-name", result.getName()); | ||
} | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest runner = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(KubernetesClientCDITest.Customizer.class)) | ||
.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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...ient/runtime/src/main/java/io/quarkus/kubernetes/client/KubernetesClientObjectMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.quarkus.kubernetes.client; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import jakarta.inject.Qualifier; | ||
|
||
/** | ||
* {@link Qualifier} to inject the Fabric8 Kubernetes Client specific {@link com.fasterxml.jackson.databind.ObjectMapper}. | ||
* <p> | ||
* Allows users to modify the behavior of the mapper for very specific use cases (such as adding Kotlin-specific modules). | ||
* Otherwise, it's not recommended to modify the mapper since it might break the Kubernetes Client. | ||
*/ | ||
@Qualifier | ||
@Retention(RUNTIME) | ||
@Target({ METHOD, FIELD, PARAMETER, TYPE }) | ||
public @interface KubernetesClientObjectMapper { | ||
} |
40 changes: 40 additions & 0 deletions
40
...me/src/main/java/io/quarkus/kubernetes/client/KubernetesClientObjectMapperCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.quarkus.kubernetes.client; | ||
|
||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import io.fabric8.kubernetes.client.Config; | ||
import io.fabric8.kubernetes.client.utils.KubernetesSerialization; | ||
import io.quarkus.kubernetes.client.runtime.KubernetesClientObjectMapperProducer; | ||
import io.quarkus.kubernetes.client.runtime.KubernetesClientProducer; | ||
import io.quarkus.kubernetes.client.runtime.KubernetesSerializationProducer; | ||
|
||
/** | ||
* Allow the provision of beans that customize the default {@link ObjectMapper} used by the KubernetesClient to | ||
* perform kubernetes-specific serialization and deserialization operations. | ||
* <p> | ||
* The resulting {@link ObjectMapper} is used to produce the default {@link KubernetesSerialization} bean, which is in turn | ||
* used to produce the default {@link io.fabric8.kubernetes.client.KubernetesClient} bean. | ||
* <p> | ||
* The following code snippet shows how to provide a KubernetesClientObjectMapperCustomizer: | ||
* | ||
* <pre>{@code | ||
* | ||
* @Singleton | ||
* public static class Customizer implements KubernetesClientObjectMapperCustomizer { | ||
* @Override | ||
* public void customize(ObjectMapper objectMapper) { | ||
* objectMapper.setLocale(Locale.ROOT); | ||
* } | ||
* } | ||
* | ||
* }</pre> | ||
* | ||
* @see KubernetesClientObjectMapperProducer#kubernetesClientObjectMapper(List) | ||
* @see KubernetesSerializationProducer#kubernetesSerialization(ObjectMapper, Class[]) | ||
* @see KubernetesClientProducer#kubernetesClient(KubernetesSerialization, Config) | ||
*/ | ||
public interface KubernetesClientObjectMapperCustomizer { | ||
void customize(ObjectMapper objectMapper); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
.../main/java/io/quarkus/kubernetes/client/runtime/KubernetesClientObjectMapperProducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.quarkus.kubernetes.client.runtime; | ||
|
||
import java.util.List; | ||
|
||
import jakarta.annotation.Priority; | ||
import jakarta.enterprise.inject.Produces; | ||
import jakarta.inject.Singleton; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import io.quarkus.arc.All; | ||
import io.quarkus.arc.DefaultBean; | ||
import io.quarkus.kubernetes.client.KubernetesClientObjectMapper; | ||
import io.quarkus.kubernetes.client.KubernetesClientObjectMapperCustomizer; | ||
|
||
@Singleton | ||
public class KubernetesClientObjectMapperProducer { | ||
|
||
@KubernetesClientObjectMapper | ||
@DefaultBean | ||
@Priority(Integer.MIN_VALUE) | ||
@Singleton | ||
@Produces | ||
public ObjectMapper kubernetesClientObjectMapper(@All List<KubernetesClientObjectMapperCustomizer> customizers) { | ||
final var result = new ObjectMapper(); | ||
for (var customizer : customizers) { | ||
customizer.customize(result); | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...e/src/main/java/io/quarkus/kubernetes/client/runtime/KubernetesSerializationProducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.quarkus.kubernetes.client.runtime; | ||
|
||
import jakarta.enterprise.inject.Produces; | ||
import jakarta.inject.Singleton; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import io.fabric8.kubernetes.client.utils.KubernetesSerialization; | ||
import io.quarkus.arc.DefaultBean; | ||
import io.quarkus.kubernetes.client.KubernetesClientObjectMapper; | ||
|
||
@Singleton | ||
public class KubernetesSerializationProducer { | ||
|
||
@DefaultBean | ||
@Singleton | ||
@Produces | ||
public KubernetesSerialization kubernetesSerialization(@KubernetesClientObjectMapper ObjectMapper objectMapper) { | ||
final var kubernetesSerialization = new KubernetesSerialization(objectMapper, false); | ||
KubernetesClientUtils.scanKubernetesResources().forEach(kubernetesSerialization::registerKubernetesResource); | ||
return kubernetesSerialization; | ||
} | ||
} |