-
Notifications
You must be signed in to change notification settings - Fork 2k
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
add a module for Spring Boot 3 AOT #2457
Changes from 1 commit
2e57a31
5ac6132
e1685f4
294038b
6f04a47
3d710de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>io.kubernetes</groupId> | ||
<artifactId>client-java-spring-aot-integration</artifactId> | ||
<packaging>bundle</packaging> | ||
<name>client-java-spring-aot-integration</name> | ||
<url>https://github.com/kubernetes-client/java</url> | ||
<parent> | ||
<artifactId>client-java-parent</artifactId> | ||
<groupId>io.kubernetes</groupId> | ||
<version>16.0.0-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.reflections</groupId> | ||
<artifactId>reflections</artifactId> | ||
<version>0.10.2</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.kubernetes</groupId> | ||
<artifactId>client-java-spring-integration</artifactId> | ||
<version>${project.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<dependencyManagement> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-dependencies</artifactId> | ||
<version>3.0.0-SNAPSHOT</version> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we wait until this is released, I'd rather not depend on a snapshot version. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Absolutely ;) that'll be on Thursday this week. Happy thanksgiving if you celebrate! I am certainly grateful for everything y'all are doing in this great project There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's switch to non-snapshot release after that |
||
<type>pom</type> | ||
<scope>import</scope> | ||
</dependency> | ||
</dependencies> | ||
</dependencyManagement> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.jacoco</groupId> | ||
<artifactId>jacoco-maven-plugin</artifactId> | ||
<version>0.8.8</version> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pull this version up to the parent pom (this may be a problem in all of our poms...) |
||
<executions> | ||
<execution> | ||
<id>jacoco-initialize</id> | ||
<goals> | ||
<goal>prepare-agent</goal> | ||
</goals> | ||
</execution> | ||
<execution> | ||
<id>jacoco-report</id> | ||
<phase>test</phase> | ||
<goals> | ||
<goal>report</goal> | ||
</goals> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.felix</groupId> | ||
<artifactId>maven-bundle-plugin</artifactId> | ||
<extensions>true</extensions> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<configuration> | ||
<source>17</source> | ||
<target>17</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<!-- | ||
todo : we don't need this after 24 november, 2022 | ||
--> | ||
<repositories> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit:how about we lift/centralize these repository declarations to the parent pom? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. they won't be required after the 24th. no need to add them at all, afaict There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's wait until after the 24th and get these removed. |
||
<repository> | ||
<id>spring-milestones</id> | ||
<name>Spring Milestones</name> | ||
<url>https://repo.spring.io/milestone</url> | ||
<snapshots> | ||
<enabled>false</enabled> | ||
</snapshots> | ||
</repository> | ||
<repository> | ||
<id>spring-snapshots</id> | ||
<name>Spring Snapshots</name> | ||
<url>https://repo.spring.io/snapshot</url> | ||
<releases> | ||
<enabled>false</enabled> | ||
</releases> | ||
</repository> | ||
</repositories> | ||
<pluginRepositories> | ||
<pluginRepository> | ||
<id>spring-milestones</id> | ||
<name>Spring Milestones</name> | ||
<url>https://repo.spring.io/milestone</url> | ||
<snapshots> | ||
<enabled>false</enabled> | ||
</snapshots> | ||
</pluginRepository> | ||
<pluginRepository> | ||
<id>spring-snapshots</id> | ||
<name>Spring Snapshots</name> | ||
<url>https://repo.spring.io/snapshot</url> | ||
<releases> | ||
<enabled>false</enabled> | ||
</releases> | ||
</pluginRepository> | ||
</pluginRepositories> | ||
|
||
</project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package io.kubernetes.client.spring.aot; | ||
|
||
import com.google.gson.annotations.JsonAdapter; | ||
import io.kubernetes.client.extended.controller.Controller; | ||
import io.swagger.annotations.ApiModel; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.reflections.Reflections; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.aot.hint.MemberCategory; | ||
import org.springframework.aot.hint.RuntimeHints; | ||
import org.springframework.aot.hint.TypeReference; | ||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution; | ||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor; | ||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
import org.springframework.boot.autoconfigure.AutoConfigurationPackages; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
/** | ||
* GraalVM native images need to know about when you might reflectively work with types at runtime. The Kubernetes Java | ||
* client works with several types reflectively at runtime. This code uses the third-party Reflections library to | ||
* reflect upon all the code in your Spring Boot application or in the official Kubernetes Java client that has | ||
* {@link ApiModel}, {@link JsonAdapter}, and registers them. It also registers a few other specific handful of | ||
* classes that should be accounted for, in specific cases. | ||
* | ||
* @author Josh Long | ||
*/ | ||
@SuppressWarnings("unused") | ||
public class KubernetesBeanFactoryInitializationAotProcessor | ||
implements BeanFactoryInitializationAotProcessor { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(KubernetesBeanFactoryInitializationAotProcessor.class); | ||
|
||
private final MemberCategory[] allMemberCategories = MemberCategory.values(); | ||
|
||
@Override | ||
public BeanFactoryInitializationAotContribution processAheadOfTime( | ||
@NotNull ConfigurableListableBeanFactory beanFactory) { | ||
return (generationContext, beanFactoryInitializationCode) -> { | ||
RuntimeHints hints = generationContext.getRuntimeHints(); | ||
String[] classNames = new String[]{ | ||
"com.google.gson.JsonElement",// | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these class names need to be secured by unit-test, in case the linked classes are renamed/moved in the future. we can do this in a follow-up. |
||
"io.kubernetes.client.informer.cache.ProcessorListener", // | ||
"io.kubernetes.client.extended.controller.Controller", // | ||
"io.kubernetes.client.util.generic.GenericKubernetesApi$StatusPatch", // | ||
"io.kubernetes.client.util.Watch$Response" // | ||
}; | ||
for (String className : classNames) { | ||
LOGGER.info("registering " + className + " for reflection"); | ||
hints.reflection().registerType(TypeReference.of(className), allMemberCategories); | ||
} | ||
registerForPackage("io.kubernetes", hints); | ||
Collection<String> packages = AutoConfigurationPackages.get(beanFactory); | ||
for (String packageName : packages) { | ||
registerForPackage(packageName, hints); | ||
} | ||
}; | ||
} | ||
|
||
private void registerForPackage(String packageName, RuntimeHints hints) { | ||
Reflections reflections = new Reflections(packageName); | ||
Set<Class<?>> apiModels = reflections.getTypesAnnotatedWith(ApiModel.class); | ||
Set<Class<? extends Controller>> controllers = reflections.getSubTypesOf(Controller.class); | ||
Set<Class<?>> jsonAdapters = findJsonAdapters(reflections); | ||
Set<Class<?>> all = new HashSet<>(); | ||
all.addAll(jsonAdapters); | ||
all.addAll(controllers); | ||
all.addAll(apiModels); | ||
for (Class<?> clazz : all) { | ||
LOGGER.info("registering " + clazz.getName() + " for reflection"); | ||
hints.reflection().registerType(clazz, allMemberCategories); | ||
} | ||
} | ||
|
||
private <R extends Annotation> Set<Class<?>> findJsonAdapters(Reflections reflections) { | ||
Class<JsonAdapter> jsonAdapterClass = JsonAdapter.class; | ||
Set<Class<?>> classes = new HashSet<>(); | ||
for (Class<?> clazz : reflections.getTypesAnnotatedWith(jsonAdapterClass)) { | ||
JsonAdapter annotation = clazz.getAnnotation(jsonAdapterClass); | ||
if (null != annotation) { | ||
classes.add(annotation.value()); | ||
} | ||
classes.add(clazz); | ||
} | ||
return classes; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor=io.kubernetes.client.spring.aot.KubernetesBeanFactoryInitializationAotProcessor |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
io.kubernetes.client.spring.extended.controller.config.KubernetesInformerAutoConfiguration | ||
io.kubernetes.client.spring.extended.controller.config.KubernetesReconcilerAutoConfiguration | ||
io.kubernetes.client.spring.extended.manifests.config.KubernetesManifestsAutoConfiguration |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: move the versioned dependency to the parent pom and let the children modules inherit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea